Git Cheatsheet

Sat 25 October 2014
By alex

How to commit properly

Commit all files in the working tree that have been modified

git commit -am 'commit message'

Commit a file individually

git commit filename -m 'commit message'

Remove recently added files and unstage modifications

git reset HEAD

Revert modified files

git checkout -- [filename]

Remove files from tracking

git rm --cached [filename]

Delete last commit (STAGED FILES WILL BE LOST)

git reset --hard HEAD~1

List tracked files

git ls-files --full-name

List branches

git branch

Push local branch to remote repo

git push origin local-branch

Delete remote branch

git push origin :branch-name

Useful log formats

git log --oneline | --stat | --numstat
git show [commit]

Git history

git reflog

Prevent files from being added into git

vi $PROJECT_ROOT/.gitignore
    # add
    file_to_ignore
    directory_to_ignore

Referring to previous commits

^ and ~ both refer to previous commits. However appending numbers doesn't always work as expected. Use ~

  • HEAD^ == HEAD~
  • HEAD^1 == HEAD~1
  • HEAD^^ == HEAD~~
  • HEAD^2 != HEAD~2 || HEAD^^