Show Menu
Cheatography

Git & GitHub Cheat Sheet (DRAFT) by

Cheat sheet for Git and GitHub

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Starting A Repo

git init
Initia­lizes a Git repo in current directory.
git init <fo­lde­r>
Initia­lizes a Git repo in <fo­lde­r> in current directory. Creates <fo­lde­r> if it doesn't exist.

Staging And Committing

git add <fi­le>
Adds <fi­le> to staging area.
git add .
Adds all files in current directory to staging area.
git commit -m "­<commit messag­e>"
Commits files in staging area with a commit message.
git commit
Opens Git's default editor to write a commit message (can be multi-­line). Save and exit to commit files in staging area with the commit message.
Ctrl+W­=exit
git commit -am "­<commit messag­e>"
Adds modified files to staging area and commits them. (Omitting the -m flag will open default editor for commit message.)
git reset HEAD <fi­le>
Removes <fi­le> from staging area.
git checkout -- <fi­le>
Undoes changes made to <fi­le>, reverting back to last known state of <fi­le> in the repo.

Renaming And Deleting Files

git mv <fi­le> <new name>
Stages the renaming of <fi­le>. The renaming takes place once committed.
git add -u
Stages deletions.
git add -A
Stages both change­s/a­ddi­tions and deletions. In effect also stages renaming of files.
(git add . does almost the same thing, read more HERE and HERE.)
git rm <fi­le>
Stages the deletion of <fi­le>. Committing will complete the deletion.
Using Git to rename and delete files (as opposed to using the operating system GUI) gives the benefit of Git automa­tically tracking the renaming and deleting. If you rename a file outside of Git, it will see the file as being deleted along with a new untracked file with the new name.
 

Inform­ation

git status
Returns the status of file changes, as well as new files.
git log
Returns all commits that are a part of the repo.
git show
Returns the last commit with a diff of the changes made. (Press Q to get out of show command)
git ls-files
Returns what files Git is tracking.

Differ­ences

git diff
Shows the difference between what's recently changed in the working directory versus the HEAD position in the repo (which is usually the last commit on the branch).
git difftool
Opens the configured diff tool (e.g. P4Merge), and shows the difference between what's recently changed in the working directory versus the HEAD position in the repo (which is usually the last commit on the branch).
git diff <commit id A> <commit id B>
Shows the differ­ences between two commits (one of the commit id's can be HEAD).
git difftool <commit id A> <commit id B>
Opens the configured diff tool (e.g. P4Merge) and shows the differ­ences between two commits (one of the commit id's can be HEAD).
In P4Merge press Ctrl+Q to cycle to the next file involved in the diff.

Branching And Merging

git branch
Lists local branches and shows which one you're currently on.
git branch -a
Lists all branches, including remotes, and shows which one you're currently on.
git checkout <br­anc­h>
Switches to <br­anc­h>.
git checkout -b <br­anc­h>
Creates a new <br­anc­h> then switches to it.
git merge <br­anc­h>
Merges <br­anc­h> into the current branch. If there are conflicts, resolving them and committing will complete the merge.
git mergetool
While in a merging state, opens the configured merge tool (e.g. P4Merge).
 

Tags

git tag <tag name>
Creates a lightw­eight tag at the current commit.
git tag <tag name> <br­anc­h>
Creates a lightw­eight tag at the last commit on <br­anc­h>.
git tag -d <tag name>
Deletes the tag.
git tag -a <tag name> -m "­<an­not­ati­on>­"
Creates an annotated tag at the current commit.
git tag -a <tag name> -m "­<an­not­ati­on>­" <commit id>
Creates an annotated tag at the specified commit.
git tag --list
Lists tags.
git show <ta­g>
Shows the details of the commit associated with the <ta­g>, and the annotation if there is one.
git tag -f <ta­g> <commit id>
Updates <ta­g> to be associated with the specified commit. Omitting <commit id> will associate the tag with where HEAD is on the current branch (usually the last commit).
git push <remote ref> <ta­g>
Pushes the tag to the remote repo.
git push --force <remote ref> <ta­g>
Forces the push of the updated tag.
git push <remote ref> --tags
Pushes all the tags in the local repo to the remote repo.
git push <remote ref> :<t­ag>
Tells GitHub to delete the tag after the colon.

Remote Repos

git remote -v
Lists remote repos.
git remote add <remote ref> <remote url>
Adds reference to a remote repo. <remote ref> can be anything, but is conven­tio­nally called origin.
git remote set-url <remote ref> <remote url>
Updates the URL for the remote reference.
git remote show <remote ref>
Returns info about the remote repo.
git clone <remote url>
Clones the remote repo into the current directory inside a new folder that has the same name as the remote repo. (Git labels this remote as origin by default.)
git clone <remote url> <na­me>
Clones the remote repo into the current directory in a new folder named <na­me>. (Git labels this remote as origin by default.)
 

Fetching And Pulling

git fetch <remote url>
Git goes out to the remote repo and updates its local inform­ation about what's in the remote repo.
git fetch -p <remote url>
Git looks for any dead branches and removes those refere­nces. The -p stands for prune.
git pull <remote url> <local branch name>
Checks for changes in the remote repo, and pulls any changes to your local branch. It will open a commit window with a default commit message you can use. Will enter a merge state if there are conflicts.
git pull is actually a 2-in-1 command: fetch then merge. It first fetches the updates from the remote repo, then merges those changes into the local repo.