Are there any Git command to combine all our ugly commits together to one?
When I write the code in local, sometime I commit the code that isn't clean yet, or, with ugly message as temporary revision.
However, when I want my code to merge with the others, I would like only final snapshot that the other can see, (hidden the revisions that look ugly)
Ex. I fork 0 to my local repository, I make change and test and commit with unclean code
0->1->2->3->4->5->6 (Final code)
when the others pull the code, is it possible to make other see only final state? and not see the tree from 1..5
I want users will see like
0------------------->6
One way I can think of, is make a patch file, but it not good enough if there are some file have to be delete or create.
You can use reset --soft
to squash multiple commits into a single new clean commit. With your branch at the final code with no staged changes you can do:
git reset --soft <sha1-of-0>
git commit
When git commit
prompts, give a clear commit message describing the complete changes being introduced.
The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master
branch, and you want to rewrite and linearize all your commits that aren't in origin/master
(which typically means the work that you haven't pushed yet) you can do:
git rebase -i origin/master
That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick
to squash
. Or, if you want to amend that commit somehow, you can change pick
to edit
.
Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status
is clean, so that you're sure that your staging area represents the same state as HEAD
. You can then just do a "soft reset" back to 0
, which resets HEAD
to that commit, but leaves your working tree and index (ie the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6
, but with 0
as a parent. ie
git status # Check that your status is clean
git reset --soft 0
git commit -m 'Work that is totally flawless'
Yes, use git rebase -i
, and squash the commits. Check this for more info and some examples.
上一篇: 合并两个分支和Git历史