Reference

Merge and rebase

Merges the given branch into the branch you are currently on.

git merge <branch>

Rebases the current branch onto the given branch (or commit/ref).

git rebase <branch>

Detaching HEAD/Relative refs

Checks out the HEAD to point to a specific commit as opposed to a branch.

git checkout <commit>

Checks out to the parent commit of the specified branch or the HEAD itself

git checkout <branch/HEAD>^<num>
  • <num> is used to specify which branch to use when there was a merge in the history, omitting num will result in the first branch from the left being used, including num will result in the branch from the left being used

Checks out to the parent of the specified branch or HEAD

git checkout <branch/HEAD>~<num>

Chaining modifiers

The relative ref modifiers can be chained together to quickly traverse the git history. For example the following command:

git checkout ~2^2^

Would move back 2 (~2), move up the second branch (^2) and then move up one (^), all on one command.

Moving branches around

Moves the specified branch to point to the location of the given relative ref (<branch> cannot equal your current branch).

git branch -f <branch> <relativeRef>

Reversing changes

Resets the current branch to the location of the given ref.

git reset <relativeRef>

Creates a new commit that resets the branch to the state of the given commit.

git revert <ref>

More commands

Cherry-pick

Creates new commits for the given refs on the current branch (in order from left to right). You can think of it as a more dynamic and powerful version of a git revert.

git cherry-pick <ref1> <ref2> ... <refN>

Interactive rebase

Rebases your current branch onto <ref> and gives you a text file to edit (pick/drop/reorder) for customization

git rebase -i <ref>

Tags

Creates a tag on the given ref with the name <tagName>.

git tag <tagName> <ref>

Describe

Describes the distance between the given ref and the nearest tag in the history

git describe <ref>

Remote branches

  • Remote branches reflect the state of a remote repository
  • When you checkout a remote branch, you are put into a detached HEAD state, so that any commits are not tracked by that branch
  • Remote branches are named <remoteName>/<branchName>, where remote name is usually origin

Fetch

Downloads commits that are not yet local and sets each branch pointer to the most recently downloaded commit (does not merge o/branch into branch).

git fetch

—rebase

Performs a git fetch and then a rebase of new commits instead of a merge.

git pull --rebase

Tracking

Creates and checks out to a new branch, set to track the given remote branch*

git checkout -b <branch> origin/<branch>