Skip to content

Instantly share code, notes, and snippets.

@okor
Last active August 17, 2021 09:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save okor/4236979 to your computer and use it in GitHub Desktop.
Save okor/4236979 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Delete the last commit, if it hasn't been pushed.

git reset --soft HEAD~1

Delete the last commit, if it has been pushed.

git reset --hard HEAD~1
git push origin HEAD --force

Temporarily ignore local changes to a file

git update-index --assume-unchanged <file>

Create a new branch

git branch <branch>

Delete an existing, local branch

git branch -D <branch>

Push a local branch

git push origin <branch>

Checkout a local copy of a remote branch

git checkout -b <local-branch> origin/<remote-branch>

Delete an existing, remote branch

git push origin --delete <branch>

Delete all stashes

git stash clear

Clean up references to obsolete branches

git remote prune origin; git gc --prune --aggressive

#!! DANGER !! Completely remove a file from git history (per branch) -- /usr/bin/true on osx, /bin/true on linux

git filter-branch -f --force --index-filter 'git rm -rf --cached --ignore-unmatch path/somefile' --prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git push origin <branch> --force

#!! DANGER !! Remove history for a password/secret string for a specific file (per branch) -- /usr/bin/true on osx, /bin/true on linux

git filter-branch --force --tree-filter "[ -f <path>/<somefile> ] && sed -i 's/<super secrect password>//g' <path>/<somefile> || /usr/bin/true" -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git push origin <branch> --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment