2013-03-09

Git

Introduction

Git is a distributed source code management system working with a complete repository copied and detached from some central server.

Git system stores complete file snapshots in it's database whereas traditional systems store initial file and differences made to them over time. Once the file is in Git's database, it stays there forever.

Git operates on top of a simple database containing project files as binaries. Every binary in this database is represented with a key.

Key is calculated by running SHA-1 hash function on file's content and that results in a 40 characters unique string key. Most of the time first 7 characters are enough to identify something in the project.

Every snapshot contains information about:

  • what binary objects are included (id's from internal database)
  • info about previous snapshot (pointer)
  • data describing the change

Global setting

Enable the Git to fill the data about the author of the change by providing this basic info:

   git config --global user.name "Your Name"
   git config --global user.email "email@example.com"

Creating a repository

  • new local repository (creates a folder with .git folder inside it):
         git init project_name
  • clone a repository from a remote system:
         git clone http://example.com/project.git

Basic workflow:

  1. edit files
  2. define files going in the next snapshot with specific file name or simply by "."
         git add .
  3. review changes
         git status
         git diff
  4. commit (make snapshot)
         git commit -m "message describing new snapshot"
Git doesn't track empty folders, if needed, users usually put empty .gitkeep file into it. If some files don't need to be under version control there is a special file .gitignore with a list of files and directories not under version control. One can use special character " * " and "." in this file.

Branches

Branches are lightweight pointers to snapshots (commits), to list branches use:

   git branch
Initially every project has one branch called master (created in the init). Nothing special about the name master, it's default. To make a new branch and start working in it use:
   git checkout -b newbranch master

If you want to switch to existing branch use:

    git checkout existingbranch

Merging

To merge something into a branch that you are currently working on use:

   git merge coolbranch
If there are same files changed in both branches, Git will inform us about a merge conflict, then we have to resolve the conflict manually and commit the changes. This results with a new snapshot and the branch pointer moves to this new snapshot.

Collaboration

In order to do the collaboration, we need location names of remote repositories. Usually we make a short alias for them. The most common alias is origin refering to some "central" repository, but we can add other physical repositories for instance by team member name.

   git remote add tom git://...project.git

Branches in other repositories are referred to as tom/master, origin/master... To get the remote snapshots and remote branching info we use fetch command.

Fetch doesn't change our branch pointers or changes our existing snapshot data, it only adds the information to the database from the remote system. This is why Git is called distributed. To get tom's Git database and merge our current branch to his superbranch we use:

   git fetch tom
   git merge tom/superbranch

Remote branch pointers are moved with push command. But, if the branch pointer on the server points to a newer snapshot than our local one, we'll get an error. Then we have to fetch the remote database, do the merge and then tell to remote to point the branch to a new snapshot:

   git push origin master

Log

To get the history of snapshots use the log command. The oneline option prints just the basic information, and the graph option shows snapshot connections.

   git log --oneline --graph

The log command above shows only the snapshots leading up to the current branch pointer. Some snapshots from other branches might not be visible.

If we would like to see what's in the master and not in the coolbranch we would use:

   git log master ^coolbranch

http://git-scm.com/


@msvaljek

1 comment:

Unknown said...

A very nice basic overview of git