Quick Git Reference =================== Configuring ----------- git config --global user.name "" git config --global user.email git config --global core.editor git config --global color.ui true Starting -------- git init [] # Start! Creates the .git directory inside or in the current directory if not specified These are the files created on a newly init'ed git repository:
.git
├── HEAD
├── branches/
├── config
├── description
├── hooks/
│   └── ... ( Sample hooks, see http://git-scm.com/book/ch7-3.html )
├── info/
│   └── exclude
├── objects/
│   ├── info
│   └── pack
└── refs/
    ├── heads/
    └── tags/
Cloning ------- git clone [] # Clone a repo and optionally give it an alternative name Adding Files ------------ git add # Stage a single file git add . # Stage everything git add -u # Stage updated tracked files git add -p [] # Stage hunks of file(s) interactively Committing ---------- 50/72-Rule: 50 or fewer characters for summary and wrap description at 72 characters. Separate the summary and description with an empty line. The description should explain the motivation behind the change and how it is different from the previous implementation. git commit -v # Commit by opening an editor which displays a unified diff git commit --amend # Modify the last commit with the current index changes - http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html - http://ablogaboutcode.com/2011/03/23/proper-git-commit-messages-and-an-elegant-git-history/ - http://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message Branching --------- git checkout # Switch between branches git checkout -b # Create and switch to a branch git branch -d # Remove local branch git branch -avv # Show extra verbose information about branches and their remotes Displaying Logs --------------- Use the `--all` option to show all branches, not only the current one. git log .. # List changes that will be merged into current branch. git log --graph --all # Show log graph for all branches git log --graph --abbrev-commit # Show log graph git log --graph --decorate --oneline # Show condensed log graph git log --graph --pretty=format':%C(yellow)%h%Cblue%d%Creset %s %C(white) %an, %ar%Creset' # Show log graph including commiter information Undoing ------- `[]` defaults to HEAD. More Examples: http://git-scm.com/docs/git-reset#_examples git reset [] # Unstage changes git reset --hard [] # Undo commits permanently git reset --soft 'HEAD^' # http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit/927386#927386 git reset HEAD@{1} # Undo last pull Diff'ing -------- Use `--stat` to show summary of changes instead of full diff. `[]` defaults to HEAD if omitted. `` can be a branch name. git diff # Show unstaged changes git diff --staged # Show staged changes git diff HEAD # Show staged and unstaged changes git diff # Show changes in your working tree relative to git diff []..[] # Show diffs between two commits. Whitespace can be used instead of '..' when using both 's. git diff []...[] # Show diff in second from common ancestor (the point the two branches split). Note the three dots. Working with Remotes -------------------- git remote add # Add remote git remote -v # Show remote urls after their names Tracking -------- git branch -u origin/ # Start tracking remote branch from current branch. http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch/2286030#2286030 git branch -u origin/ [] # Start tracking remote branch from or current branch if not specified. git branch -d -r origin/ # Stop tracking remote branch. NOTE: Remote branch is not removed. git push -u # Create a remote branch from local branch and track it Stashing -------- Stash references have the form stash@{0}. WIP means Work In Progress. An empty `[]` defaults to the latest stash. git stash list # Show stash list git stash [save] [] # Stash current changes with optional message git stash show [-p] [] # Show stash diffstat or '-p' to show patch git stash apply [] # Apply stash git stash pop [] # Apply stash and drop it git stash drop [] # Drop stash git stash branch [] # Create and switch to a new branch with the applied to it git stash clear # Remove all stashes Rebasing -------- Rule of Rebasing: "Only rebase local branches" git checkout ; git rebase master # Apply changes on top of master - http://git-scm.com/book/en/Git-Branching-Rebasing - http://blogs.atlassian.com/2013/10/git-team-workflows-merge-or-rebase/ - http://stackoverflow.com/questions/16336014/git-merge-vs-rebase - http://blog.sourcetreeapp.com/2012/08/21/merge-or-rebase/ - http://www.derekgourlay.com/archives/428 Using Additional Packages ------------------------- gem install git-smart # https://github.com/geelen/git-smart gem install git-up # https://github.com/aanand/git-up gem install omglog # https://github.com/benhoskings/omglog while true; do clear; git --no-pager log --decorate --oneline --graph --all -n 10; sleep 3; done # Quickly display git repository's commit graph with live update. See https://github.com/benhoskings/omglog for a more permanent solution. Using Shell Aliases ------------------- ga='git add' gb='git branch' gci='git commit -v' gco='git checkout' gd='git diff' gdc='git diff --cached' gl='git log --pretty=oneline --abbrev-commit --graph --decorate' glog='git log --graph --pretty=format":%C(yellow)%h%Cblue%d%Creset %s %C(white) %an, %ar%Creset"' gloga='glog --all' gf='git fetch' gm='git merge' gp='git push' grh='git reset HEAD' grhh='git reset HEAD --hard' gss='git status -s' References ---------- - http://git-scm.com/book - http://git-scm.com/docs - http://www.ndpsoftware.com/git-cheatsheet.html - http://gitref.org/