Git Notes for Reference

The following are some bits and pieces I like to refer to from time to time. In particular, the alias shown below is really useful for understanding the state of the the repository.

# Useful alias for viewing the commit graph (David Mahler)
alias graph="git log --all --decorate --oneline --graph"

# Show remote repositories with details for fetch/pull
git remote -v

Branching and merging

# Create a branch, then checkout that branch
git branch mybranch
git checkout mybranch
git checkout -b mybranch  # shortcut for the above

# Fast-forward merge (e.g. just move master to the branch's commit) or the
# recursive strategy (e.g. 3-way merge starts from the common ancestor)
git merge <branch-name>

# Diff between two branches
git diff master..mybranch

# Show branches that are merged with the current branch
git branch --merged

# Show only remote tracking branches, or all tracking branches
git branch -r
git branch -a

# Show detailed branch info
git branch -vv

To set HEAD to a specific commit, use git reset. This does not affect the working directory, like checkout does (it's affecting the staging area aka index)

# Reset HEAD to a specific commit
git reset 6bc9c4d  # * 6bc9c4d (HEAD -> master) Log bind addresses on startup

# Reset HEAD to the master branch at origin.
git reset origin/master  # Reset current HEAD to the specified state

Stashing allows me to save my uncommitted files while I go off checking out elsewhere...

git stash        # create a stash of the unstaged files
git stash apply  # reapply the stashed files
git stash drop   # drop the stash

It's also possible to have multiple stashes, in which case the commands may need to specify which stash is being referred to.

Rebasing is like merging, except that it applies a diff to your current branch, which becomes the latest commit (as opposed to creating a new commit, as happens with a merge). You can then do a fast-forward merge of the target branch to bring it in sync with this latest commit.