Skip to content

Instantly share code, notes, and snippets.

@turingbirds
Last active January 10, 2023 22:10
Embed
What would you like to do?
Git cheatsheet

Git cheatsheet

Set up Git

git config --global user.email "john_doe@example.com"
git config --global user.name "John Doe"

Generate and upload SSH keys

Generate an SSH key; you may already have one in ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub

  1. Go to your Account Settings
  2. Click "SSH Keys" in the left sidebar
  3. Click "Add SSH key"
  4. Paste your PUBLIC key (*.pub) into the "Key" field

Get a repository from GitHub

git clone git@github.com:github_username/name_of_the_repository

Syncing a Forked git Repository With a Master Repository’s Changes

Make sure git status shows all local changes are committed (this is to prevent conflicts).

git remote -v
git remote add upstream git@github.com:github_username/name_of_the_repository
git fetch upstream
git merge upstream/master

Create a new branch

You can do this at any point in time, also after having made local changes.

Make sure you are in the right branch to begin with! You might want to first go back to master and create the new branch from there.

git checkout master
git checkout -b new_branch_name

Listing all branches

Branches that git knows about:

git branch -a

All remote branches:

git ls-remote origin refs/heads/*

Staging updated files

git add name/of/the/file.txt
git add -A			# use to add all files from .
git add -u			# use to tell git to track deleted files

Undo an add:

git reset HEAD name/of/the/file.txt

Remove file from repository and local filesystem:

git rm name/of/the/file.txt

Remove from repository but keep the local copy:

git rm --cached name/of/the/file.txt

Reviewing what has changed

If the file is not yet staged:

git diff name/of/the/file.txt

If the file is already staged:

git diff --staged name/of/the/file.txt

Reviewing what has changed betweeen branches:

git diff --stat master..branch_name

for a summary, or

git diff master..branch_name

for a full diff. To limit this to a single file, use:

git diff master..branch_name -- name/of/the/file.txt

Commit your changes

git commit -m "Lots of cool new features"

Undo a commit

N.B. this is before a push. Reset the working directory to the state before the git commit.

git reset --soft HEAD~1

Amending commits

git commit -m 'initial commit'
git add forgotten_file
git commit --amend

This also works for changing commit messages:

git commit --amend -m "New commit message"

Reset all local changes

If you want to reset changes made to your working copy, do this (cannot be undone):

git checkout -- .

or reset a single local file (cannot be undone):

git checkout -- file_to_reset

You can also reset to a particular hash (cannot be undone):

git checkout 53f32ff -- file_to_reset

To go back to the state just before the given commit, use the history shortcut notation (53f32ff~1).

Start again from scratch: let's say you're on the master branch, and the remote repository is called "upstream". First fetch upstream, and then:

git reset --hard upstream/master

Before doing this, consider the less radical rebasing (git rebase upstream/master), and/or stash your local changes or place them in their own feature branch before resetting master.

Revert all local changes

"git-revert" creates a new commit (or commits) that precisely undoes the actions of a previous commit (or commits). Say that 14aa4d9 is the "bad" commit:

git revert 14aa4d9

Use --no-commit no avoid creating a single undo commit for each commit to be undone.

Push changes to GitHub/BitBucket/etc

Push the state of a local branch (e.g. "master") to a remote repository (e.g. "origin").

git push -u origin master

Merging branches

Let's say you want to merge the branch myfeature into master, and then remove the branch myfeature:

git checkout master
git merge myfeature
git branch -d myfeature		# delete the now redundant branch (master points at the same location)
git push origin :myfeature	# delete the now redundant branch on the remote

(Temporarily) reverting to earlier work

git checkout 7529083f7f88787affc6e58bffbad305ba4860e3

You are now in "detached head mode". Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).

Restore head via:

git checkout master

Merge conflicts

git checkout --ours /path/to/file

to keep the remote file, and:

git checkout --theirs /path/to/file

to keep the local file.

Then git add.

Check out a specific file from a specific branch

E.g. check out file1 and file2 from the master branch:

git checkout master -- file1 file2

Undo a rebase

Use the reflog.

git reflog

Find the entry just before the start of the rebase. Then reset to that.

git reset --hard "HEAD@{2}"

View changes made in a particular commit

git diff COMMIT~ COMMIT

where COMMIT is the hash, will show you the difference between that COMMIT's ancestor and the COMMIT.

Solve issues with pushing large files

git config --global ssh.postBuffer 524288000
git config --global http.postBuffer 524288000

Do not wrap long lines when viewing a diff

GIT_PAGER='less -eiFRSX' git diff ...

Stashing

Stash files:

git stash

Stash all files, including untracked files, using:

git stash --all

Later on, pop from the stash:

# (do stuff, e.g. a git pull)
git stash apply   # prefer "apply" to "pop"!

There might be merge conflicts.

In case the apply or pop fails (This will discard any unsaved local changes so be careful):

# backup your entire directory, just in case
git clean -xfd
git stash apply

Or, alternatively:

# backup your entire directory, just in case
git checkout stash -- .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment