Skip to content

Instantly share code, notes, and snippets.

@peacefixation
Last active December 3, 2025 22:27
Show Gist options
  • Select an option

  • Save peacefixation/28d9be399a123d480798204bc9c2d05d to your computer and use it in GitHub Desktop.

Select an option

Save peacefixation/28d9be399a123d480798204bc9c2d05d to your computer and use it in GitHub Desktop.
Git cheat sheet
# ...
[alias]
cm = commit -m
co = checkout
cof = "!checkout_fzf() { \
local branches branch; \
branches=$(git --no-pager branch | grep -v HEAD | sed 's/.* //' | sed 's/release.*//' | grep -v '^$' | sort -u) && \
branch=$(echo \"$branches\" | fzf +m) && \
git checkout $branch; \
}; checkout_fzf"
cob = checkout -b
fe = fetch
last = log -1 HEAD
pl = pull
prn = "!rm_branches_no_origin() { \
git remote prune origin; \
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs --no-run-if-empty git branch -D; \
}; rm_branches_no_origin"
pushu = !git push -u origin HEAD
st = status
stv = status -v
unstage = reset HEAD --

Git Cheat Sheet

Checkout / Pull / Push

# checkout a local copy of a remote branch and switch to it
git checkout -b feature/branch-name origin/feature/branch-name

# set the url of a remote
git remote set-url origin http://user@remote-url:port/path/repo.git

# push to a remote and update the remote tracking branch
git push -u origin <branch name>

Pull Request

# checkout a github pull request locally (pull request #1234 into local branch pr-1234)
# see https://blog.scottlowe.org/2015/09/04/checking-out-github-pull-requests-locally/
git fetch origin pull/1234/head:pr-1234
git checkout pr-1234

# pull updates to github pull request into local branch pr-1234
git pull origin pull/1234/head:pr-1234

Log

# show changes to a file (-p patch)
git log -p filename

Clean up / reset

# reset a file to last commit
git checkout path/to/file

# clean up working copy (reset changes to all files (f) and directories (d))
git clean -fd

# reset a branch to a commit (delete commits after that)
git reset --hard <sha>

Delete

# prune stale remote tracking branches
git remote prune origin

# delete local branch
git branch -d <branch name>

# delete local branch irrespective of merged status
git branch -D <branch name>

# delete remote branch
git push origin --delete <branch name>

# delete local branches with no remote tracking branch
# see http://erikaybar.name/git-deleting-old-local-branches/  
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs --no-run-if-empty git branch -d # or -D to force

Tags

# create an annotated tag**  
git tag -a v1.2 -m "Version 1.2"
  
# push the tag  
git push v1.2
  
# push all tags  
git push origin --tags

# create a tag on an old commit with the date of the commit
git checkout XXXX
GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a v1.2 -m "Version 1.2"

Squash

# squash commits on a branch
git checkout -b testbranch

# make many commits
git reset --soft master

# changes from those commits are staged
git add .
git commit -m 'One commit.'

Stash

Drop

# drop the most recent stash
git stash drop

# drop the nth stash
git stash drop stash@{n}

# drop all stashes
git stash clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment