Is it possible to combine commits in git?

Sometimes (often), I make mistakes and make a “quick fix” after clicking.

but for some clarity of fixation, sometimes I do re fork (if it was in my fork) and do, commit all the changes again.

Is there a way to merge commits?

+4
source share
2 answers

Yes, there is a way, and this is called crushing.

git rebase -i sha1_of_the_first_concerened_commit 

Then replace all changes to the commits below the corresponding commit with squash (or s), save the file and continue reloading with git rebase --continue .

The next time you press, your push will probably be rejected, you will need git push -f .


But in your particular case, there is no need to go through all this, you can use commit --amend .

Say you made a deal and then quickly fixed file1. You just need to add the file to the staging area:

 git add file1 

Then:

 git ci --amend 

And finally, force click:

 git push -f 

Be careful when rewriting a story, it is great when you are the only one who works on a repo, or you are sure that no one pulled after you, otherwise it is a pain for others.

+3
source

Yes. What you want to learn is rebase . try

 $ git help rebase # and read it 

another git commit --amend option is git commit --amend . If the commit you are about to commit must be merged with the current HEAD, you can change it.

Both of these options rewrite history. So you need git push -f to git push -f your changes upstream if the affected commits are already at the remote end.


for an example using the simpe rebase example, suppose you make 5 commits,

 A--B--C--D--E ^ initial ^ HEAD 

and commit B is a quick fix to commit A,
and commit E is a quick fix for fixing C.
So you want to combine B in and E in C.

 $ git rebase -i HEAD~5 

you start with this:

 pick A msg pick B msg pick C msg pick D msg pick E msg 

You rebuild commits so that the merge is lower than those that need to be merged. i.e. B below A (already there) and E below C.

 pick A msg pick B msg pick C msg pick E msg pick D msg 

and you will tell git not to select a commit, but to crush it; that is, combine it with the above - or fix it - see the man page.

 pick A msg squash B msg pick C msg squah E msg pick D msg 

everything is done, save the file, and git will reorder and push commits.
so now you have:

 A'--C'--D 
+1
source

All Articles