Search code examples
gitmergegit-commitrevert

Merge an old git commit to the top of the current branch


I made few git commits (commit 1, commit 2...commit 5) in my project. I had to fix a bug, so I reverted to commit 2 using the below command -

git checkout <commit_id> .

I made some code changes and committed the changes. Now, I want to merge these changes and go back to the latest version of the code (that was in commit 5). What should I do?

Thanks in advance!


Solution

  • If you already pushed, then just commit your fix at the top (i.e. after commit 5).

    If you didn't, and you want to fix commit 2, you can either:

    • commit your fix, run: rebase -i HEAD~5 then swap your latest commit to be right after commit2, and mark it as "fixup".

    or:

    • run: rebase -i HEAD~4, then mark commit2 for edit, put your fix, commit and then rebase --continue