brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git merge --squash turns a whole branch into one staged change

Git
~/tips/tip-git-merge-squash

git merge --squash feature

Applies all changes of a branch to your working tree without committing, so you can record them as one commit.

Some branches are a diary: fix typo, try again, revert that, fix for real. When the work is done, main does not need the diary, it needs the result. git merge --squash brings the result over as a single commit that you write yourself.

git switch main
git merge --squash feature
git commit -m "Add feature"

The merge itself does not commit anything. With a two-commit branch, Git answers with these lines and leaves the changes staged:

Automatic merge went well; stopped before committing as requested
Squash commit -- not updating HEAD

git status then shows the file as staged, and git log still looks exactly like before. After the commit, main has one new commit instead of two. Per the documentation, the state is produced as if a real merge happened, except that Git does not move HEAD and does not record MERGE_HEAD. That is why the next git commit is an ordinary commit and not a merge commit.

This has a side effect you should know about. Git has no record that feature was merged, so the cleanup command refuses:

git branch -d feature
error: the branch 'feature' is not fully merged.

Once you have checked that the squash commit is on main, git branch -D feature deletes it. Use the capital D only after that check, because it skips the safety net.

Another caveat: you lose the individual commits and their authors on main. If several people worked on the branch, mention them in the message yourself. The docs also say --commit is not allowed together with --squash and will fail.

Further reading: git-merge documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --abort git branch --edit-description [<branch-name>] All Git tips (144) All topics

Link