Git: Cheat Sheet

July 2nd, 2009 by jeremychone

Create and Switch branch

git checkout -b my_new_branch

List local and remote branches

git branch -a

Fetch remote branch

git fetch --all
#get the remote branches
git branch -a
#fetch a remote branch as local (assuming remote branch remotes/origin/dev)
git branch dev remotes/origin/dev

Git reset -hard (good to undo merge)

git reset --hard

abandon everything since your last commit; this command can be DANGEROUS. If
merging has resulted in conflicts and you’d like to just forget about the
merge, this command will do that.

Git reset –hard HEAD^ (remove completely last commit)

git reset --hard HEAD^

Git revert last commit (this will add another commit to remove the old one)

git revert HEAD

Git clean (remove untrack files) source

git clean -f

clean directory as well:

git clean -fd

If you just want to remove ignored files, run “git clean -f -X”. If you want to remove ignored as well as non-ignored files, run “git clean -f -x”

Change filename case

git mv -f name.java Name.java

Git Abort when git am breaks

git am --abort

Git Tag (see Git tag cheat sheet)

Tag a specific branch

git tag stable-1 1b2e1d63ff

Viewing available tags is done with -l:

git tag -l

Annotated tag:

git tag -a -m "Tagging release 1.0" v1.0

Viewing tags with annotation:

git tag -l -n1

Delete a tag:

git tag -d tag_name

Push tags:

git push origin --tags

Git log with tags

git log --decorate=short

Checkout tag

git checkout -f -b [new branch name] [tag]/[sha1]

Rebase

git rebase -i HEAD~4

Git –update-index

Prevent to change a file

git update-index --assume-unchanged full/file/path.ext

Undo

git update-index --no-assume-unchanged full/file/path.ext

git ignoring changes

See also

Leave a Reply