Useful Git commands


HEAD

git checkout {sha1} checkout (navigate) to the code in a specific sha1 (commit)
git checkout HEAD checkout (navigate) to the code in the HEAD
git checkout - checkout (navigate) to previous branch
git revert {sha1} create a new commit undoing an specific commit (sha1)
git revert HEAD create a new commit undoing the last commit
git reset {sha1} reset current HEAD to the specified state {sha1}
git reset --merge {sha1} reset current HEAD to the specified state {sha1}, keeping current changes on the working directory
git reset HEAD~1 --soft undo the last commit, keeping changes on the INDEX
git reset HEAD~1 undo the last commit, keeping changes on the working directory
git reset HEAD~1 --hard undo the last commit, discarding all changes
git reset HEAD --hard undo all changes over the INDEX and working directory
git reflog List all {sha} and recover from reset --hard
git bisect start start binary search the change that introduced a bug
git bisect bad HEAD mark the HEAD as bad
git bisect good {sha1} mark the last commit as good
git bisect {good|bad} mark an intermediate commit as good or bad
git bisect reset return to the commit that was checked out before git bisect start
git remote [add|rm] {repo_alias} {url} manage the set of repositories whose branches you track
git remote add {repo_alias} -f {url} Add and fetch the remote repository
git remote [-v] list remote repositories
git push Updates remote repo using local commits
git rebase {branch} add on the current branch commits from the specified branch
git rebase -i HEAD~3 make a group of last three commits
git clone {repo url} clone a repository into a new directory
git clone -b {branch} {repo url} clone a repository branch
git fetch download objects and refs from another repository (.git/FETCH_HEAD)
git pull {alias} [master] fetch from another repository and merge with the master
git pull [-s strategy] [-X options] [alias] [branch] fetch from and merge with another respository using custom options
git merge {branch} merge current branch with another specified branch
git merge {branch} --squash merge current branch with another specified branch, keeping changes on INDEX
git branch -a list remote and local branches
git branch {new_branch} create a new local branch
git branch -m {old-name} {new-name} Renaming a local branch
git branch -rd origin/{branch} Delete a branch locally
git push origin --delete bugfix Delete the same branch remotely
git checkout -b {new_branch} create and change to the new local branch
git checkout {branch} change to another existing branch
git push -u {repo_alias} {branch} push local branch as a remote branch
git branch -d {branch} delete a local branch
git push {repo_alias} --delete {branch} delete a remote branch
git tag v1.0 create a new tag
git push origin v1.0 update remote repository with the new tag
git format-patch master --stdout > bugfix.patch create patches against the master (with commits not in the master)
git apply --stat bugfix.patch list changes over an patch (without apply it)
git am --signoff --ignore-whitespace ‹ bugfix.patch apply patch on the current branch
git merge second_repo/master -s recursive -X ours Merge the current repo (origin) - keeping all files, with the second_repo

INDEX

git commit -m 'comment...' create an commit with changes added on INDEX
git commit -am 'comment...' add (tracked files) and create an commit
git commit --amend ['comment...'] add changes to the last commit
git reset HEAD undo add changes, keeping changes on the working directory
git reset {file} undo add changes, keeping changed file on the working directory
git reset {branch} {file} undo add changes, restoring file as it is on the specified branch

WORKING DIRECTORY

git add . add all changes (tracked files) to the INDEX (for new commit)
git add {file or folder} add specific change to INDEX
git rm {file or folder} remove files from the working directory (for new commit)
git add -A . add all changes and remove to the INDEX
git clean -d -f discard changes (clean the working directory)
git checkout {file} restore file content (as it is on HEAD)
git stash Save your unfinished work (local changes)
git stash pop Bring your saved unfinished work back
git stash list list the stashes that you currently have
git stash pop [stash] remove a single stashed state and apply it on the current working
git stash apply [stash] like pop, but do not remove the state from the stash list
git stash -U [stash] store changes adding untracked files
git log [--since="1 weeks ago" ] show current commits
git log [-p] {file} show changes over specific file
git log {branch..master} show changes over the master (the difference between two branches)
git log -S 'text' show changes (over current commits) matching with the text
git log {directory} show changes over the specific directory
git grep {something} List commits that matche with something
git status show changes on the working directory and INDEX
git status -sb show changes too
git reflog like git log, but show up deleted commits (see git reset)
gitk [--all] [file] like git log, but is GUI (need apt-get)

Useful links

http://www.ndpsoftware.com/git-cheatsheet.html
http://nvie.com/posts/a-successful-git-branching-model/
git rebase
http://ibrokegit.com/

Links and references

http://git-scm.com/book
http://rogerdudler.github.com/git-guide
http://sixrevisions.com/resources/git-tutorials-beginners/
https://github.com/tiimgreen/github-cheat-sheet#readme