Skip to content

Instantly share code, notes, and snippets.

@kianw
Last active February 10, 2023 18:34
Show Gist options
  • Save kianw/392d7e7c2227dc65c3d1 to your computer and use it in GitHub Desktop.
Save kianw/392d7e7c2227dc65c3d1 to your computer and use it in GitHub Desktop.
Git Cheat Sheet
Git Cheatsheet
=== ==========
--- BASICS, PUSH, PULL, MERGE, REBASE --
git init (create a new, empty git repo in current directory)
git clone <url_or_path> (clone a new repo from the given repo)
git pull origin <full-branch-path>
git push origin <full-branch-path>
git fetch (git fetch + git merge is basically == to git pull)
git merge origin/<remote-for-current-local-wc-branch>
git merge <branch-to-merge-into-current-branch>
git merge --no-commit --no-ff ... (don't commit or fast-forward; good for checking conflicts first)
git rebase <branch-to-rebase-off-of> (rebase the current branch off the head of the specified branch)
git rebase -i HEAD~2 (rebase last two commits interactively, allowing you to reorder them)
git status
--- DIFFS AND LOGS ---
git diff --cached (what is queued for commit)
git diff --name-status
git diff remotes/origin/<branch-path> <branch-path> (show difference between remote-tracking and local branches)
git diff <branchname>:<filepath> <filepath> (show difference between current branch/filepath and branchname/filepath)
git log <remotebranch>..HEAD (show commits in local but not in remote)
git log ..<otherbranch> (show commits in otherbranch that would be merged into HEAD if
we did a git merge <otherbranch>)
git diff ...<otherbranch> (diff from common ancestor (merge base) to the head of what will be merged)
git stash show -p <stashref> (diff of stash, in patch format)
git branch --contains <branch> (list branches which contain all commits in <branch>)
git log <branch> ^dev --no-merges (show all commits in <branch> which are not in dev)
git log -2 (only last 2 entries in log)
git log --grep=something (with commit message matching 'something')
git log -p (show differences in patch format)
git log --follow -- <pathname> (show all commits affecting pathname)
git show <revision>:<pathname> (show a file at a specific revision/commit)
git stash save --keep-index (or -k) (save all in stash except queued for commit)
git stash save -u (save all including untracked)
--- BRANCHES ---
git checkout <branchname> (switch to branch)
git branch (view branch names, including which one is active)
git branch -r (view list of remote branches)
git branch -d <branchname> (delete the specified local branch)
git branch <branchname> (create local branch <branchname> from the current local branch)
git branch --track <localname> origin/<remotename> (create a local branch that tracks a remote branch)
git branch -f <targetbranch> <mergefrombranch> (fast forward <targetbranch> to <mergefrombranch>; useful for
deployment to bring local branch up to date without checking
it out: git branch -f branchname origin/branchname)
git branch -m <new_name> (rename current branch)
git branch -m <old_branch> <new_name> (rename branch)
git push -u origin <remotename> (create a new remote branch and (from the active branch?) track it;
sets an upstream tracking reference)
git checkout -b <localbranch> (create a new local branch and switch to it)
git checkout --track -b <localbranch> origin/<remotebranch>
(create a local remote-tracking branch and switch to it;
for instance, to create a local version of an existing remote branch)
git checkout -b <branchname> <commit> (create new local branch from the specified commit and switch to it)
--- COMMITTING ---
git commit -a (automatically stage files that have been modified or deleted, but
not new files, and commit)
git cherry-pick <commit> (cherry-pick a commit, usually from a different branch)
git cherry-pick -n <commit> (cherry-pick a commit, but only apply as a patch, don't commit)
git add <files>
git add --interactive
git add --all
--- REVERTING and RESETTING ---
git checkout -- <filename> (revert working copy a single file)
git checkout -f HEAD (revert working copy to HEAD)
git reset --hard (reset working copy back to HEAD; doesn't remove untracked
files, so check with git status afterwards)
git reset --hard HEAD~1 (revert last commit; working copy reset too)
or git reset --hard HEAD^
git reset --mixed HEAD~1 (revert last commit; working copy not reset, but index is reset)
git reset --soft HEAD~1 (revert last commit; working copy not reset; index not reset)
git reset --merge ()
git clean -df (clean untracked files and directories)
git push -f origin HEAD^:<branchname> (remove the HEAD^ commit from the remote for the branch)
git rebase --onto <branch>~2 <branch>~1 <branch> (remove the commit before the last one (~1) from the history
of this branch. removes commits after <branch>~2 up to and including
<branch>~1. obviously you can vary the numbers to modify the range
of commits that are deleted.)
git stash show -p | git apply --reverse (reverse changes that are in a stash; keeps stash)
--- CLEAN UP ---
After pushing a branch and merging, clean up local branches and remotes via:
git checkout master
git pull
git branch -d <branchname>
git remote prune origin
--- NOTES ---
Dots -- The two vs. three dots have slightly different meaning for diff than for the commands that list revisions
(log, gitk etc.). For log and others two dots (a..b) means everything that is in b but not a and three dots (a...b)
means everything that is in only one of a or b. But diff works with two revisions and there the simpler case represented
by two dots (a..b) is simple difference from a to b and three dots (a...b) mean difference between common ancestor
and b (git diff $(git merge-base a b)..b).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment