How to transfer changes from one commit to another?

I have two commits on one branch, one after the other. I added the changes to file A to the first commit, and then made some changes to other files, and then made another commit. Now I want the changes in file A to be on the second commit, and not on the first. What is the most elegant way?

+6
source share
5 answers

If they are small commits, and commits should always be small in git, the easiest way is git reset HEAD^^ , and then just repeat them. Please note that any solution for this includes rewriting the history, and if you have already clicked on these commits, you should not do this if you do not know what you are doing.

+6
source

I wrote a script for this purpose. You can check it out here .

Using a script, it will be as simple as:

 mv-changes HEAD~ HEAD fileA 
+3
source

Pretty attractive answer, but mostly you are Squash and Rebase.

See here: http://davidwalsh.name/squash-commits-git

0
source

In the case of the likely unlikely event you just created, commit, undo the second commit with git reset HEAD^ , and then add the changes to the first commit with git commit --amend .

In the more likely case, when you moved and commits were at some point in the past, rebasing is your best bet. Not knowing your exact situation, linking to a link that explains it well is probably best.

In both the first and second cases, the Pro Git section of the book on the history of rewriting explains well the parameters, both when using them and when using caution.

6.4 Git Tools - Rewrite History

0
source

Another solution to this problem

  • Find a commit immediately before the two commits (this is probably the main branch)
  • Run git rebase -i <commit before commits to be reordered> ( git rebase -i master in most cases)
  • In the text editor, replace the order of commits (for vim, use the ddp sequence, and on the line, which should move down)
  • Save, exit, and git rebase do the rest
0
source

All Articles