Skip to content

Instantly share code, notes, and snippets.

@paulstuart
Forked from dhei/gitcheatsheet.md
Created April 25, 2024 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulstuart/2dd857a4700c141e4527fc19304e034c to your computer and use it in GitHub Desktop.
Save paulstuart/2dd857a4700c141e4527fc19304e034c to your computer and use it in GitHub Desktop.
Git cheetsheet
undo git merge
git reset --hard HEAD~1
delete a tag in remote
git tag -d mytag
git push origin :refs/tags/mytag
Remove local branches no longer on remote
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d

(from this StackOverflow link)

Delete all stale remote-tracking branches
git remote update --prune
List all branches of a remote
git branch -a
List all branches and references to a remote
git remote show origin
delete a remote branch
git push origin --delete <branchName> OR git push origin :<branchName>
squash multiple commits into one commit via interactive rebase
git rebase -i HEAD~3
Restore lost commits
git reflog
git merge <SHA1-of-your-commit>
Remove sensitive file from repo
git filter-branch --force --index-filter git rm --cached --ignore-unmatch file-to-be-removed.txt' --prune-empty --tag-name-filter cat -- --all
Find out which remote branch a local branch is tracking
git branch -vv
Delete ambiguous refname
$ git branch -u origin/develop
warning: refname 'origin/develop' is ambiguous.
fatal: Ambiguous object name: 'origin/develop'.
$ git show-ref origin/develop
03d710f2208eaaa0ecbbba3826ef112c509be9aa refs/heads/origin/develop
c1b822349ae2837198cdbd56e60f5518523d72ce refs/remotes/origin/develop

git update-ref refs/heads/origin/develop (adopted from https://stackoverflow.com/a/26047558/873234)
Delete invalid remote branch (prune remote branch)
git remote prune origin (use --dry-run flag to do a dry run)
Delete already-merged local branches except "develop" and "master"
git branch --merge | grep -v "develop" | grep -v "master" | xargs -n 1 git branch -d

Open .gitconfig file, add the following lines to make it a git alias git bclean:

[alias]
    bclean = “!f() { git branch --merge | grep -v “develop” | grep -v “master” | xargs -n 1 git branch -d; }; f”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment