Git tutorial

Introduction

Git is a distributed version control system. It can be used by anyone (programmers, non-programmers) to manage their text files. E.g Homework, blogs, configuration files, source code, xml files etc.

1. Initialize

Go to your current project directory and run following command to initialize git.

$ git init

Once you have initialized a git repository, you can do all the things a version control system would do e.g add, commit, delete, branch, merge etc.

2. Add current changes to be versioned

Add, in git is adding the snapshot of changes. E.g text to be added, deleted etc. Run git add (often) to add changes to your project. You can run following command to add all the changes.

$ git add .

It recursively adds all the changes to be included in the next commit.

3. Committing a change

Once you have added a file, next thing you would do is commit the changes. Only after committing, your version control system will be aware of the changes. To commit, use following command.

$ git commit -m "Message about what you did on this version"

Commit message is very important. It helps you identify what changes you made in that version. If you were to come back to a known good state to restart your work, nothing could be more valuable than a good comment. Always use descriptive, clear comments.

4. Knowing the status

Often, you will want to know the status of your changes (are all files versioned, is there something I don’t want to be included, do I need to run commit to save the changes, or add the files first etc). You know the status of git by running following command.

$ git status

5. Branching and merging

This is the most important yet the most feared feature of traditional version control systems. Your work starts to shine, once you get a hang of branching and merging. Think of it like “knowing by doing”. One of the best ways to know something does not work is by doing it (at least in programming).

This is exactly what branching and merging is for. Even more, you use branching and merging with every feature of your project, moving forward (discarding errors after trying it out) and moving with good state, in the master branch.

workflow

Fig : Typical git work flow with branching and merging.

a. Creating a branch
$ git branch branchname
E.g
$ git branch addtheintro
$ git branch feature1
$ git branch errorcorrection
$ git branch testbuffer
b. List branches
$ git branch
addtheintro
errorcorrection
feature1
* master
testbuffer

Current branch is shown by * on its side. As you can see, we have 4 branches in our project.

c. Switch branches
$ git checkout errorcorrection

In that branch do your work, test the project and if you are sure it works, commit it, switch back to master (a name used by all to indicate main branch) merge it, and delete the branch, once you know you don’t need it anymore (you can get it back anytime you want later). Lets say, we are in a branch called errorcorrection, and we just did some changes on file6.

$ git add file6
$ git commit -m "Corrected the nasty error about the buffer overflow"
$ git checkout master
$ git merge errorcorrection
$ git branch -d errorcorrection
d. Get back your deleted branch

If you later found out, you needed to test something in that deleted branch, you will have to first find out sha1 key for that branch. In our case it is easy to identify, because of the message we used above in errorcorrection branch while commiting. To find out our commit history, we use “git log”.

$ git log
commit a2b28af2035c3f6ba4c0c15a8a2344975960890d
// Author, Date
Corrected the nasty error about the buffer overflow

We can also use “git reflog” to find out reference log compared to HEAD.

$ git reflog
a2b28af HEAD@{0}: merge errorcorrection: Fast-forward
//

Only 7 characters of the key is useful to us from first command (b2b28af). To checkout and switch to the branch you can use either of following two commands.

$ git checkout -b my-error-branch HEAD@{0}
$ git checkout -b mybranch a2b28af

6. Resolving conflicts

This is common while working with multiple people on a single project. In our case, on a single computer, it is rare, but there is nothing wrong to be prepared for that.

Lets say, you edited same file in multiple branches with same set of changes and tried to merge them all to master, and get something like this.

$ git merge testbuffer
Auto-merging file6
CONFLICT (add/add): Merge conflict in file6
Automatic merge failed; fix conflicts and then commit the result.

And the conflicting file looks like this

<<<<<<< HEAD
Hi this is error correction
This is cool
=======
Hi
>>>>>>> testbuffer

You need to fix the error in this file and remove the <<< HEAD, === and >>> testbuffer marker and add the changes again and commit.

$ git add file6
$ git commit -m "Fixed"
[master cebcc0a] Fixed

If you want to get the changes you just made in master to say, testbuffer branch (most logical way to solve a conflict would be to go to that particular branch than to solve it in master), you can use “git merge” in that branch.

$ git merge master
Updating abe22d9..cebcc0a

If this looks confusing, you can always install graphical software like gitk and see the project history.

gitk

Fig: Showing gitk output

7. Collaboration (Setting up repository)

You should use repository, even if it seems almost unnecessary because of things we have already done without using a repository. Working on repository is a good skill to have.

Create a directory where you want to setup a repository. Setup one repository per project. Don’t put everything inside a single repository. E.g you could have separate repositories each for you blog, homework, configuration etc. Lets say, we created a directory called “blogrepo.git” for our blog (You don’t have to use .git suffix, but it is good to indicate it is a git repository). Go inside that directory and initialize a git repository using following command.

$ git init --bare

Only thing you need to remember after creating a repository is, how to get to it. Lets say, you setup blogrepo.git in your home directory, then your url would be “~/blogrepo.git”. You just need a perfect URL to get to your repository, be it local or remote. In this case we will use local URL of directory.

After doing these two things (creating a repository, remembering the URL) go back to your project directory and run following commands to make it aware of this new repository and sync with the remote branch.

$ git remote add origin ~/blogrepo.git
$ git push -u origin master
//
Branch master set up to track remote branch master from origin.

All of these three commands were one time only commands. From now on you only have to type “git push” to push the changes (after you commit your project).

$ git push

Recap

1. Initialize

$ git init

2. Add Changes

$ git add .

3. Committing a change

$ git commit -m "Some message"

4. Knowing the status

$ git status

5. Working with branch

$ git branch branchname # Create a branch
$ git checkout branchname # Switch to a branch
$ git merge branchname # Merge a branch
$ git branch -d branchname # Delete a branch
$ git log # Find changes
$ git reflog # Log compared to HEAD
$ git checkout -b newbranch HEAD@{0} # Checkout previous deleted branch

6. Repository
$ git init --bare # Create a repo
$ git remote add origin ~/blogrepo.git # Make your project aware of remote URL (~/blogrepo.git)
$ git push -u origin master # Sync local branch with remote
$ git push # Upload changes in local repository

Your most used commands will be add (2), commit (3), working with branch (5) and push (6 last line).

Conclusion

I hope this tutorial has given you enough recipe to start working with git. As I said earlier, you don’t have to be a programmer to use git. It can and should be used to efficiently manage all text files. I hope I haven’t missed any essential commands for everyday git usage.

Cheers!!

About these ads

Leave a comment

Filed under Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s