Git: Merge in only one commit
Usually, I work with branches in Git, but I don't like to see hundreds of branches in my working tree (Git history). I'm wondering if there is a method in Git to "join" all commits in a branch in only one commit (ideally with a clear commit message).
Something like this:
git checkout -b bran开发者_开发技巧ch
<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 this, only "super commit" will exist in the git log.
It sounds like you're looking for the --squash
option of git-merge
:
git checkout master
git merge --squash branch -m "super commit"
This can be done using git rebase
and squash, or using git merge --squash
, see
Git merge flattening
and
git: squash/fixup earlier commit
If you are positive you want only a single commit and are fine with the branch never being marked as "merged" (perhaps because you're about to delete it with git branch -D my-squash-merged-branch
and never want to see it again), use this:
git checkout master
git merge --squash branch-to-merge
git commit -m "message for commit"
However, after much testing, I believe the best way to merge most branches is:
git checkout master
git merge --no-ff branch-to-merge -m "message for commit"
This avoids the "fast-forward" merge that disallows specifying a -m "message"
option for many merges. It doesn't actually provide a single commit as originally requested but at least makes it easy to see the begin/end of the branch and so make for easy reverts and the like. A git log
will show all the individual commits that were 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 a graph of the log (git log --graph
), you can see that git does indeed 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 activity happens on the master branch, the graph will show the merged branch starting at the correct place and joining at the current head but of course all commits will still be shown in the log with the commits from the branch being at the top.
精彩评论