Git: merge only one commit

I usually work with branches in Git, but I don't like to see hundreds of branches in a working tree (Git history). I am wondering if there is a method in Git to β€œmerge” all commits into branches in only one commit (ideally with an explicit commit message).

Something like that:

git checkout -b branch <some work> git commit -a -m "commit 1" <some work> git commit -a -m "commit 2" <some work> git commit -a -m "commit 3" git checkout master git SUPER-JOIN branch -m "super commit" 

After that, only "super-commit" will exist in the Git log.

+43
git branch
Jan 05 2018-11-11T00:
source share
3 answers

This can be done using git rebase and squash, or using git merge --squash , see

Git drain flattening

and

git: squash / fixup previously committed

+41
Jan 05 '11 at 11:00
source share

It looks like you are looking for the --squash git-merge option:

 git checkout master git merge --squash branch -m "super commit" 
+65
Jan 05 2018-11-11T00:
source share

If you are sure that you want only one commit and excellent with a branch that will never be marked as "merged" (perhaps because you are going to delete it with git branch -D my-squash-merged-branch and no longer want see her), use this:

 git checkout master git merge --squash branch-to-merge git commit -m "message for commit" 

However, after much testing, I find the best way to merge most branches:

 git checkout master git merge --no-ff branch-to-merge -m "message for commit" 

This avoids the fast-forward merge, which prohibits the -m "message" option for many merges. In fact, it does not provide a separate commit, as originally requested, but at least makes it easy to see the beginning / end of a branch and thus simplifies the return process, etc. A git log display all the individual commits that have been merged ...

 commit a6672a4c3d90c35d5f39c45f307ef6b385660196 Merge: 015f8d6 f84e029 Author: Brian White <bcwhite@example.com> Date: Wed Jan 15 20:47:35 2014 -0500 merged something trivial commit f84e02915faa02afc9a31b8c93a6e7712420687d Author: Brian White <bcwhite@example.com> Date: Wed Jan 15 20:47:12 2014 -0500 added something also trivial commit 904d5b5ff00d691d63104a77d2e2ca484732a5fb Author: Brian White <bcwhite@example.com> Date: Wed Jan 15 20:46:26 2014 -0500 added something trivial commit 015f8d681bdaf65725067ee8058215cedb529dd6 Author: Brian White <bcwhite@example.com> Date: Wed Jan 15 20:23:31 2014 -0500 optimizations to MyThing ... 

... but if you look at the log graph ( git log --graph ), you will see that git does recognize it as a single merge.

 * commit a6672a4c3d90c35d5f39c45f307ef6b385660196 |\ Merge: 015f8d6 f84e029 | | Author: Brian White <bcwhite@example.com> | | Date: Wed Jan 15 20:47:35 2014 -0500 | | | | merged something trivial | | | * commit f84e02915faa02afc9a31b8c93a6e7712420687d | | Author: Brian White <bcwhite@example.com> | | Date: Wed Jan 15 20:47:12 2014 -0500 | | | | added something also trivial | | | * commit 904d5b5ff00d691d63104a77d2e2ca484732a5fb |/ Author: Brian White <bcwhite@example.com> | Date: Wed Jan 15 20:46:26 2014 -0500 | | added something trivial | * commit 015f8d681bdaf65725067ee8058215cedb529dd6 | Author: Brian White <bcwhite@example.com> | Date: Wed Jan 15 20:23:31 2014 -0500 | | optimizations to MyThing ... 

If commits or other actions occur on the main branch, the combined branch will be displayed on the chart, starting from the right place and connecting to the current head, but, of course, all commits will still be displayed in the log with the commit from the branch being at the top.

+12
Jan 16 '14 at 2:23
source share



All Articles