Search code examples
gitgit-commit

Move files between commits


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

I do not understand how should I use the interactive rebase. I did git rebase -i HEAD~10 and choosen to edit the commit with file in order to check it out from there. I use git guit but, see no files in the commit area. I can choose to amend the previous commit then I see the files. But, I cannot add the misplaced file to that previous commit since I do not see the file in the current commit to start with.


Solution

  • If I don't get it wrong, what you want is move some change that was included commit 2 to commit 1.

    I find the easiest way to do that is doing two consecutive interactive rebases.

    In the first one, you split commit 2 into two commits: the first including just the change you want to move, and the second including all the rest. We have now commits 1, 2.1, and 2.2.

    Then you rebase again, and this time choose to squash commit 2.1 into 1.