Simple git references

A list of a few commonly used git commands.

Getting help

  • git help command or git command --help: Show help for a command

Repository creation

  • git init: Create a repository in the current directory
  • git clone url: Clone a remote repository into a subdirectory

File operations

  • git add [dir] [file1] [file2]: Add file or files in directory recursively
  • git rm path: Remove file from the working tree
    • -f: Force deletion of file(s) from disk
  • git mv path destination: Move file or directory to new location
    • -f: Overwrite existing destination files
  • git checkout [rev] file: Restore file from current branch or revision
    • -f: Overwrite uncommitted local changes

Working tree

  • git status -s: Show status of the working tree
  • git diff [path]: Show diff of changes in the working tree
  • git diff HEAD path: Show diff of stages and unstaged changes
  • git diff --cached --stat: Show diff of staged changes
  • git add path: Stage file for commit
  • git reset HEAD path: Unstage file for commit
  • git commit [-m 'msg']: Commit file that has been staged
    • -a: Automatically stage all modified files
    • --amend: Replace last commit for a new one
  • git reset --soft HEAD^: Undo commit and keep changes in the workingtree
  • git reset --head HEAD^: Reset the working tree to the last commit
  • git clean: Clean unknown files from the working tree
  • git revert [rev]: Reverse commit specified by

Examining history

  • git log [path]: View commit log, optionally for specific path
    • ^branch: View commit log without branch
  • git log: [from [...to]]: View commit log for a given revision range
    • --stat: List diffstat for each revision
    • --oneline --graph --decorate: Show recent commits with decoration
  • git blame [file]: Show who authored each line
  • git ls-files: List all files in the index
  • git whatchanged: Show logs with difference

Remote repositories

  • git fetch [repo]: Fetch changes from a remote repository
  • git pull [repo]: Fetch and merge changes from a remote repo
  • git push [repo] [branch]: Push changes to a remote repository
  • git remote: List remote repositories
  • git remote add remote url: Add remote to list of tracked repositories

Branches

  • git checkout branch: Switch working tree to branch
    • -b branch: Create branch before switching to it
  • git branch: List local branches
    • -d branch: Delete the branch
  • git branch -f branch rev: Overwrite existing branch start from revision
  • git merge branch: Merge changes from branch to actual branch
  • git mergetool: Working through conflicted files

Exporting and importing

  • git apply - < file: Apply patch from stdin
  • git format-patch from [...to]: Format a patch with log message and diffstat
  • git archive rev > file: Export snapshot of revision to file
    • --prefix=dir/: Nest all files in the snapshot in directory
    • --format=[tar/zip]: Specify archive format to use, tar or zip

Tags

  • git tag name [revision]: Create tag for a given revision
    • -s: Sign tag with your private key using GPG
    • -l [pattern]: List tags, optionally matching pattern

File status flags

  • Modified: File has been modified
  • Copy-edit: File has been copied and modified
  • Rename-edit: File has been renamed and modified
  • Added: File has been added
  • Deleted: File has been deleted
  • Unmerged: File has conflicts after a merge

Configuring

  • git config --global user.name 'name'
  • git config --global user.email 'name@email.com'

Use Git as a magic time machine

1
2
3
4
5
6
7
git reflog
# you will see a list of every thing you've
# done in git, across all branches!
# each one has an index HEAD@{index}
# find the one before you broke everything
git reset HEAD@{index}
# magic time machine

Make the last commit including the new changes

1
2
3
4
5
# make your change
git add . # or add individual files
git commit --amend --no-edit
# now your last commit contains that change!
# WARNING: never amend public commits

Change message of the last commit

1
2
git commit --amend
# follow prompts to change the commit message

Saving when committed to the wrong branch

1
2
3
4
5
6
git checkout name-of-the-correct-branch
# grab the last commit to master
git cherry-pick master
# delete it from master
git checkout master
git reset HEAD~ --hard

Undo a change to a file

1
2
3
4
5
6
7
# find a hash for a commit before the file was changed
git log
# use the arrow keys to scroll up and down in history
# once you've found your commit, save the hash
git checkout [saved hash] -- path/to/file
# the old version of the file will be in your index
git commit -m "Wow, you don't have to copy-paste to undo"

References

  1. Git cheat sheet

  2. Oh Shit, Git!?!