Is it possible to condense the commits on a branch into a single commit prior to merging with the main? I thought this would be a fairly common scenario, but maybe I am not using the right search terms.
I’ll explain the scenario in more detail. Often I would want to make many local commits while working on a change in a branch to ensure I have a comprehensive history of changes. But once through with the changes in the branch, when I am merging to main, I would like to reduce the commits on the branch to a single one and then merge it to main. I do understand that commits are inexpensive in Git, but in some situations, I might just prefer to do this.
* merge to main
|\
* | commit 2 on main
* | commit 1 on main
| * commit 2 on branch
| * commit 1 on branch
|/
* branch from main
to be made to look like
* merge to main
|\
* | commit 2 on main
* | commit 1 on main
| * commit on branch (branch commits flattened to one)
|/
* branch from main
I’m a novice when it comes to git. If I have erred in the use of terms, I do apologise.
Use interactive rebasing. Find the commit where your branch diverged from master (may using
git merge-base; let’s call the commit<diverge>), thenand an editor will pop up, allowing you to rewrite history interactively. You’ll want to squash commits.