Assortment of useful, tiny git commands
Git commands, that might be handy
Rebase
git pull origin {branchName} --rebase
Basically rewriting history. It’s a form of “merge”, but not merge.
Despite what I thought earlier, it can also be reverted:
git reflog , then find the commit hash before the rebase started and revert to it using git reset --hard (hash). Pretty neat.
Submodule update workflow
We use(d) submodules at work. They’re not as bad as the first seem and do have a few advantages. The worflow took some time getting used to, though.
- checkout main
- (make branch) if breaking change
- make changes, commit
- push to main
- in case the commit fails, do
git commit [...] --no-verify(to bypass certain checks)
Get latest main for all submodules:
git submodule foreach git pull origin main
Submodule init
git submodule update --init --recursive
Delete merged local branches
Credits go to this stackoverflow post.
Step 1: List all merged local branches
git branch --merged
Step 2: You can exclude main and develop branches or whatever else you want to keep using grep
git branch --merged| grep -Ev "(^\*|main|develop)"
Step 3: Delete all branches listed by the previous command
git branch --merged| grep -Ev "(^\*|main|develop)" | xargs git branch -d
Get a quick history of merges
At work we have a shared npm package. Each Version has a publish merge request (= pull request) that follows the naming convention “chore/publish_
$(git log --grep="chore/publish_" --merges -n 1 --format=%H) will give you the commit hash of the last publish merge request.
Using that in with ..HEAD will give you the history of the last publish for a cheap changelog.
git log $(git log --grep="chore/publish_" --merges -n 1 --format=%H)..HEAD --merges --format="- %s"