Moving files between commits

I have two consecutive commits, somewhere in the local history, and one file was mistakenly added to the second. I want to rectify the situation.

I do not understand how to use interactive reboot. I did git rebase -i HEAD~10 and decided to edit the commit with the file to check it there. I am using git guit but cannot see the files in the commit area. I can choose to change the previous commit, then I see the files. But I can not add the uninstalled file to the previous commit, since I do not see the file in the current commit to start with.

+6
source share
3 answers

If I'm not mistaken, then you need to move some kind of change that commit 2 was included to commit 1.

I find the easiest way to do this by doing two consecutive interactive permutations.

In the first, you divide commit 2 into two commits: the first only includes the change you want to move, and the second includes all the others. We have now completed 1, 2.1 and 2.2.

Then you reinstall again and this time select squash commit 2.1 to 1.

+8
source

So, when reinstalling, select for fixing both the commit, where you added the file by mistake, and the one you want to add to this order. If the file is in the last commit, but must be in an earlier commit, you will have to reorder the lines. For example, I start with

 pick 8de731b Commit with missing file. pick bbef925 Commit with too many files. pick 52490ce More history. 

I need to change it to

 edit bbef925 Commit with too many files. edit 8de731b Commit with missing file. pick 52490ce More history. 

Then

 # In the commit containing an extra file git reset HEAD^ badfile.c git commit --amend git rebase --continue # Now in the commit to add it to git add badfile.c git commit --amend git rebase --continue 

Unfortunately, when editing history in one branch, I don’t know how to avoid editing history in all branches. Reinstallation should be done as early as possible to avoid such problems. In my simple case, I can combine the master and the other branch, but the commits do not merge, then I need to reinstall the wizards and reorder and crush the commits, for example:

 pick 7cd915f Commit with missing file. fixup 8de731b Commit with missing file. #This was the higher of the two entries pick 8b92c5a Commit with too many files. fixup bbef925 Commit with too many files. #This was the higher of the two entries pick 94c3f7f More history. fixup 52490ce More history. #This was the higher of the two entries 

Later editing: I just noticed that I accidentally reordered the commit history as a carryover from my original answer. Rearranging the lines in rebase changes the order you do; after editing, you can reinstall and change them again to return to the original fixing order.

+16
source

As I often stumble upon this problem, I wrote a script for this. It works completely automatically. You can find it on Github . Copy it to the local file system, add it to PATH, and you can run it as:

 mv-changes <source-commit> <destination-commit> <path>... 

You can also run the script in the Git - Bash shell on Windows.

Note that if <path> changes in the gap are made between source-commit and destination-commit , this will not work.

More information is available here .

+5
source

Source: https://habr.com/ru/post/952971/


All Articles