Git command reference - list of top git commands

Every programmer should know about VCS(Version Control System) especially GIT. This post serves as a short reference for most popular GIT commands. They are grouped by functionality and has a short explanation above them.To use them copy any command to your favorite command line tool.And if you wish to learn more about GIT check this out.

Table of Contents

First Steps

First thing which we have to do after installing GIT is to setup a name with an email and editor --global option tells to set this variable in the~/.gitconfigrather locally in .git/config. Default is --local which sets options for specific repository.

1
2
3
git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"
git config --global core.editor vim

And if you would like to check already existing settings.

1
git config --list

To get specific value.

1
git config <option>

To start track specific folder.

1
git init

To clone remote repository.

1
git clone <url>

If you need any help, these commands will open man page. verb is command’s name.

1
2
3
git help <verb>
git <verb> --help
man git-<verb>

Information

GIT Commands used for getting any kind of information about repository.

STATUS

To check status of files

1
git status

Short version of status. Left column indicates status of the staging area and the right - working tree. M stands for modified, A stands for added, ? - not tracked, ! - ignored files.

1
2
git status -s
git status --short

To show ignored files.

1
git status --ignored

COMPARING

To compare files in working directory with staging area. Tells you what you changed but not yet staged.

1
git diff

To see what you staged which goes into your next commit. –staged and –cached are synonyms.

1
2
git diff --staged
git diff --cached

HISTORY

Outputs commit history.

1
git log

Show difference in each commit.

1
git log -p

Limit output to last entries.

1
git log -2

Shows abbreviated stats.

1
git log --stat

Formats output. oneline - single line, short - adds extra info, full - more info, fulller - even more.

1
git log --pretty=oneline

You can even format output.

1
git log --pretty=format:"%h - %an, %ar : %s

Show output since 2 weeks.

1
git log --since=2.weeks

INSPECTING

Show all blobs and trees where tree points to.

1
git ls-tree master^{tree}

Show all blobs and trees recursively. t makes show the SHA-1s of the subtrees themselves, rather than just all the blobs.

1
git ls-tree -r -t master^{tree}

Show what type object is. SHA-1 is an object hash.

1
git cat-file -t <SHA-1>

Staging

Stage a file/files for commit.

1
2
git add <file>
git add .

To remove staged files, but keep it in the working directory.

1
git rm --cached <file>

To unstage file. Undoes any changes since last commit.

1
git reset head <file>

Interactive staging

1
git add -i

Committing

To commit staged files.

1
git commit -m "Commit message"

To stage and commit shorthand.

1
git commit -a -m "Commit message"

Ammend last commit. For example change commit message or add extra files. It will overrides your last commit.

1
git commit --amend

Branching

Create new branch. And switch to it.

1
2
git branch <branchName>
git checkout <branchName>

Create new branch. And switch to it.

1
git checkout -b <branchName>

Create new branch and track to origin branch.And switch to it.

1
git checkout -b <branchName> origin/<branchName>

List all branches.

1
git branch

Delete a branch.

1
git branch -d <branchName>

Delete remote branch.

1
git push origin :<branchName>

Combining

Commands for merging and rebasing

MERGE

Merge a branch into current one.

1
git merge <branchName>