开发者

Are there any Git command to combine all our ugly commits together to one?

开发者 https://www.devze.com 2023-04-02 22:06 出处:网络
When I write the code in local, sometimes I commit the code that isn\'t clean yet, or, with an ugly message as temporary revision.

When I write the code in local, sometimes I commit the code that isn't clean yet, or, with an ugly message as temporary revision.

However, when I want my code to merge with the others, I would like the 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 fin开发者_开发百科al state? and not see the tree from 1..5

I want users will see like

0------------------->6

One way I can think of is making a patch file, but it not good enough if there is some file have to be deleted 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 (i.e. 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. i.e.

 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.


Assuming you haven't already pushed your commits to the remote then there are several ways to do this.

One option is to use

git rebase -i 

which gives you the interactive rebase editor. You can then mark all the commits except the last as 'squash'


Git squash merge is designed for this purpose. The way you would do this is to create a branch, make all your changes, and then merge back using squash merge.

How to use git merge --squash?

However, this isn't usually recommended because you will lose history this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号