Git cheatsheet

Reference commands for the git version-control system.

Working with commits

Abort a merge.

git merge --abort

Unstage a directory.

git rm --cached -r dir

Amend a commit message.

git commit --amend

Working with branches

Fetch remotes. This will update the list of remote branches available.

git fetch --all

Set the default upstream.

git push --set-upstream origin master

Set a remote upstream (e.g. for a fork).

git remote add upstream <ssh>

Check out a remote branch locally.

git checkout -b master origin/master

Rename a branch locally and remotely.

# Rename branch locally.
git branch -m old_branch new_branch

# Delete the old branch.
git push origin :old_branch

# Push the new branch, set local branch to track the new remote.
git push --set-upstream origin new_branch

Delete a local branch.

git branch -D develop

Update local branch information.

git remote update origin --prune
git branch --all

Add a remote.

git remote add private <ssh>

Change remote URL.

git remote set-url private <ssh>

Rename a remote.

git remote rename origin private

Remove a remote.

git remote rm private

Create a branch from tag.

# Go to the starting point of the project.
git checkout origin master

# Fetch all objects.
git fetch origin

# Make the branch from the tag.
git branch new_branch tag_name

# Checkout the branch.
git checkout new_branch

# Push the branch up.
git push origin new_branch

Track binary data using large file storage

Git Large File Storage (LFS) replaces large files (i.e. binary files) with text pointers inside Git, and enables versioned storage of the files on a remote server. This enables offloading of versioned binary data to GitHub or an Amazon S3 bucket, for example. Note that this costs money when used on GitHub.

git lfs track "data/**"