Ankit Ahuja

Git Cheatsheet

When you’re new to git, it can get difficult to wrap your head around it at first. This is especially true if you’re migrating from subversion or when you’d rather spend more time writing your code than trying to learn git.

Git requires some learning, and you might as well spend the time doing it rather than wondering what just happened after running a command. Learn.github is a good place to start to learn the basics.

But even after spending some time learning git, there are going to be times that you’ve to painfully search the web/manual for basic stuff like working with remote branches or more complex stuff like handling a failed merge.

For the same reason, I’m creating this post as a reference point for myself to avoid repetitive searches. I’ll keep adding new commands here if and when I use them.

This is no way near to a complete reference for git and there might be better ways to do the things listed here. These are just the commands I use the most, in the manner I prefer to use them.

Basics

  • Commit and Stage all changes
1
git commit -am "log message"
  • Push Changes to remote repo
1
git push
  • Pull Changes to local repo
1
git pull
  • Check current status of working tree
1
git status
  • View diff
1
git diff
  • Discard local uncommitted changes to a file (in this case, abc.txt)
1
git checkout -- abc.txt

Branches (Branch name is assumed to be abc)

Working with local branches

  • Create a branch
1
git branch abc
  • Switch to a branch
1
git checkout abc
  • View all existing branches
1
git branch
  • Remove a branch
1
git branch -d abc
  • Merge a branch
1
2
3
# In this case, merging abc into master
git checkout master
git merge abc

Working with remote branches

  • Push a local branch to remote repo
1
git push origin abc
  • Delete a remote branch
1
git push origin :abc
  • Setup a local branch to track a remote branch
1
git branch --track abc origin/abc
  • Create a local branch, push it to remote repo. and then pull changes from it
1
2
3
4
5
6
7
8
9
10
11
12
13
# Creating the new branch and switching to it
git checkout -b abc;

# Pushing the changes
git push

# Meanwhile someone else made changes to the remote version
# Pulling those changes
git pull origin abc

# if you want to simply use 'git pull' to get the changes, do the following
git config branch.abc.remote origin
git config branch.abc.merge refs/heads/abc

Alternative methods listed here.

Tags (Tag name is assumed to be v1.0 here)

  • View all tags
1
git tag
  • Create a tag referencing a particular commit using its checksum (annotated)
1
2
3
4
5
6
# here commit checksum is assumed to be 98eda02.

# to view the checksum of a commit, you can do
git log --pretty=oneline

git tag -am 'v1.0' 98eda02
  • Push tags to remote repo
1
git push --tags
  • Delete a local tag
1
git tag -d v1.0
  • Rename a local tag
1
2
git tag NEW_TAG OLD_TAG
git tag -d OLD_TAG[/bash]
  • Delete a remote tag
1
git push origin :refs/tags/v1.0

Useful Git References