Git frequently used commands

Git is very popular source control system today. As a developer we are more or less engaged with Git. Here I am summarized most commonly used commands which may be helpful to you.

To clone a new repository to your dev system:

1git clone <git repo url>

To update your dev repository from remote repository:

1git pull

To check which branch you are:

1git branch

Create a new branch from another branch:

Make sure the current branch is up-to-date. Then run following:

1git branch <new branch name>

Switch to another branch:

1git checkout <branch name>

check current status:

1git status

Stage or add file for commit:

1git add <relative path of the file>

Revert changes of a file:

1git checkout <relative path of the file>

Make a commit:

1git commit -m <your commit message>

Push your commits to remote branch:

1git push origin <remote branch name>

Merge changes from another branch:

First make sure the source branch is up-to-date. To do this you can checkout to the source branch and run git pull Or you can run git fetch from any branch. Then run:

1git merge <source branch>

List recent commits on current branch:

1git log

Save git credentials:

While working with git, it is very convenient to store the credential i.e git user name and password so that we do not need to enter the same each time. There are several ways to do this. One of the way I like most is to use git credential helper cache. It should work almost all the platforms like Linux, Mac OS X and Windows.

To set the cache we need to execute following command:

1git config --global credential.helper "cache --timeout=2592000"

The timeout is in seconds. Here I put 2592000 seconds which is 30 days.

Once the command is executed, we need to run any git command which need credential such as git pull. It will ask for the credential for the first time and then it would be cached for 30 days.




Of course this is a small list. Please feel free to comment or ask about more such commands. I would be very happy to answer and/or include those in the list.


comments powered by Disqus