Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten commits on a branch

Tags:

git

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.

like image 495
Coffee And Keyboard Avatar asked Jul 30 '12 15:07

Coffee And Keyboard


People also ask

How do I merge commits to a commit?

To specify a new commit message you should use "reword" instead of "pick" on the first commit. Or, if you want to keep just the "picked" commit message, you can used "fixup" instead of "squash" for the others, which will do the same as squash but discard the commit's message.

How would you squash multiple commits together without using git merge?

You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.


3 Answers

I would recommend learning to use the interactive rebase, but in case it seems too complicated to you, you can simply use

git reset --soft <diverging-commit>

to undo all commits up to the diverging point without changing the index, and

git commit -s

to make a single commit of all the changes.

like image 56
thameera Avatar answered Oct 06 '22 15:10

thameera


Use interactive rebasing. Find the commit where your branch diverged from master (may using git merge-base; let's call the commit <diverge>), then

git rebase -i <diverge>

and an editor will pop up, allowing you to rewrite history interactively. You'll want to squash commits.

like image 17
Fred Foo Avatar answered Oct 06 '22 15:10

Fred Foo


You can also use git merge --squash <branch> to merge a branch without performing any commits:

$ git checkout master
$ git merge --squash mybranch
...
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested
$ git commit

(Update: Removed link to article advocating avoidance of history rewriting. I now think squashing small feature branches prior to review is a good practice.)

like image 13
Trevor Robinson Avatar answered Oct 06 '22 15:10

Trevor Robinson