There are a ton of very useful git cheat sheets around, on top of a great documentation (which is where alongside StackOverflow I find myself when things get fun). I started putting together this list ~3 years ago and finally decided to move from my notepad to here. I’ll be updating this over time.
Branches
Permanently remove commits from remote branch
git reset --hard <last.working.commit.id>
git push --force
Push local branch to remote
git push -u origin <branch.name>
Delete local branches for which remote tracking branches have been pruned
Source: StackOverflow
git checkout main
git fetch -p
git branch -vv | grep ': gone]'| grep -v "\*" | awk '{ print $1; }' | xargs -r git branch -d
Tags
Goal | Command |
---|---|
List | git tag -l |
Create annotated tag | git tag -a <name> -m "<tag.message>" |
Push tag to remote (git push doesn’t work) | git push origin --tags |
Delete tag local | git tag -d <name> |
Delete tag remote | git push origin --delete <name> |
Commits
Undo local, unpushed commit(s)
n
is number of commits to undo. --soft
keep changes, --hard
discards commits and doesn’t keep changes.
git reset --soft HEAD~n
Check out file from remote
git checkout origin/<branch.name> -- <path.to.file>
Rebase
NOTE: I’m not a rebase expert, create backup tag before forcing any changes & proceed with caution.
Rebase
git checkout <rebase-target-branch>
git checkout -b <intermediate-branch>
- Cherry-pick from
<work-branch>
into<intermediate-branch>
git checkout <work-branch>
git reset --hard <intermediate-branch>
git push --force
Rebase in squash-commit repo
There might be an easier approach, but this has worked quite well. In a repo where PR/MRs follow squash-commit policy, merges from main
to feature
branches aren’t desired, and feature
branch is n
merges behind main
. To perform a rebase closer to what one would expect (i.e., only feature
branch changes on top of main
, without existing commits re-hashed):
- Rebase
feature
onto updatedmain
- Run
git reflog
, find commit with most recentfeature
branch commit, note ref (i.e.,HEAD@{25}
) - Type
q
to exitreflog
- Run
git reset --hard HEAD@{n}
to discard duplicate changes - Run
git push --force
to update remote and discard duplicate changes in remote
Submodules
Import submodules recursively
git submodule update --init --remote --recursive
Undo submodule updates locally
git restore . --recurse-submodules
Source: StackOverflow.