Skip to content

Instantly share code, notes, and snippets.

@dhei
Last active April 25, 2024 15:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dhei/733b4219255a1e322d17 to your computer and use it in GitHub Desktop.
Save dhei/733b4219255a1e322d17 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”
@Mashpy
Copy link

Mashpy commented Aug 28, 2021

Thanks for your cheatsheet.

I got this error -
warning refname 'origin/master' is ambiguous

Hope this tutorial will help.

@a8568730
Copy link

Thanks for your cheatsheet.

I got this error.

$ git merge origin/master
warning: refname 'origin/master' is ambiguous.

I check the refs, it contains two rather only one:

$ git show-ref origin/master
b73e1c8fd21c28926884c8ac61d0666172930fef refs/heads/origin/master (wrong one)
aea283f55e75242cf57134c824e05007df4fab06 refs/remotes/origin/master

The git update-ref in above section Delete ambiguous refname seems to lack arguments. I followed the adopted answer, add -d to delete the wrong refs and it works.

$ git update-ref -d refs/heads/origin/master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment