Skip to content

Instantly share code, notes, and snippets.

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.

Stashing

git stash
# (do stuff, e.g. a git pull)
git stash pop

There might be merge conflicts.

Stash all files, including untracked files, using:

git stash --include-untracked

Q: How to stash unmerged code in git?

I did a git pull and found that one of my files needs to be merged.

I don't really want to merge that file right now - I have another branch that I need to work on, and I will get back to this branch later to resolve the merge.

What is the best course of action for me to "undo" this pull? Or how can I hold off on this merge until I'm ready to deal with it? What do I need to do to be able to change branches, but then return to this unmerged state in my code?

A: This is a good example of why I reckon doing a fetch instead of a pull is a good idea.

What you can do is simply reset your branch to it's previous state..

git reset --hard HEAD~1

or if you are already in the middle of the merge (resolving conflicts), just abort it..

git merge --abort

finish your changes and then...

git fetch

...have a look at what's changed

git merge origin/branch 

An easy way to see what has changed is to do

git cherry branch origin/branch -v --abbrev=6

That will give you a list of commits that are in the origin branch but not in your branch...if you need the details of what changed in that commit then

git show commit --name-status

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.