Pravar Agrawal Technology & Travel

Git Essentials

My last post was about Version Control and one of Distributed VCS tool, Git. I’ve been using Git for quite some time now in my daily office work, managing tasks as part of OBM and some other fiddling. Here are few commands which have helped me as a beginner in working with Git.

Begining with creating a source code repository where all the developers and other members in our team can collaborate we use,

git init <directory_name>

And if we are to initialize an already created directory then we can simply give git init and that will initialize an empty git repository. We must configure git when we are using it for the first time on our workstation. And the command to do it,

git config --global user.name "<Name>"

git config --global user.email "<email>"

Now, let’s put some files in our newely initialized repository and save those changes. To check the status of the recent changes we give git status. It’ll show that there are some recent files which are yet to be added so that git can track those. To do that give,

git add <file_name>

To put our recent changes into staging area we give,

git commit -m "here comes a commit msg"

Here -m specifies the commit message which is to be given along with the commit. Staging area is a place between the working directory and the local repository. Another git command which is very useful is

git diff

It is used to check differences between two files in the repository. This is very helpful when we are trying to understand the recent changes in files. To check the history of all our progress so far we use,

git log

Above command displays the logs in reverse chronological order. To see the difference in each commit we can use git log -p or --patch command. If we have added any file by mistake and want to discard the changes then we can use,

git rm --cached file_name

This command discards the changes made in the working directory and –cached option removes the file from git tracking without deleting it.

Branching in git is one of the most important feature. Whever we start working on a new feature/bug/fix, we should create a new branch. The master branch which is always there in our repository should not be touched and in most of the cases refer to state of production read code. To create a new branch,

git checkout -b <feature_branch_name>

The -b option creates a new branch if it’s not there. To simply checkout a branch so that we can start working on it we can give,

git checkout branch_name

Same command can be used to move between the branches as well. Now, once we are finished working on our feature we need to merge our branch with master so that others can see the recent changes as well. For that, first checkout the master branch and give,

git merge <feature_branch_name>

When there is an existing project and we decide to contribute to it then the best way to do that is by doing a fork of that same project. This happens when we don’t have push access to the project which we want to contribute to. When we fork a project, Github(a web based hosting service for Git based projects) creates a copy which belongs to us only. The fork by default is known as an origin and we add a remote so that we can pull latest changes from the project we are contributing to, also called upstream. Now to add new remote we use,

git remote add <remote_name> <url>

To add remote upstream url,

git remote add upstream <upstream_url>

So now, our origin points to upstream. Whenever we want to push our changes developed locally to upstream, we can give

git push upstream master

This would push master branch to the upstream repository.

Well, we just went through a whole plethora of widely used git commands. In my next post, I’ll be discussing on some more commands and general git issues I’ve faced while working on it.