3

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!

2

3 Answers 3

2

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
2
  • Thanks, I will need to follow Option 1 as I already committed my fix. (commit your fix, run: rebase -i HEAD~5 then swap your latest commit to be right after commit2, and mark it as "fixup".) I will rebase it, but how to swap the latest commit to be after commit 2?
    – user591410
    Aug 12, 2015 at 18:07
  • when you run rebase -i HEAD~5 it opens an editor (the one configured in your git settings) with your 5 latest commits. cut and paste the latest commit to the relevant line
    – avim
    Aug 12, 2015 at 18:10
2

If you did it all on your local repository, with git checkout <commit_id> you must be on a detached state. If you run git branch it should show that you are on (HEAD detached at <commit_id>).

To be safe first checkout your commits on a new branch:

git checkout -b <bug_fix>

Then go on the branch you had the 5 commits (assuming master) and rebase the fix:

git checkout master
git rebase <bug_fix>

EDIT: If you have left the detached state after committing the fixes by checking out to another branch, you have to find your commit_id with git reflog and then rebase them on the desired branch: git rebase <commit_id>. You can also merge, but that will add the fix_commit on top of commit5. Rebase will generate a better history ...,commit2,fix_commit,commit3, ...,commit5.

4
  • I did git checkout <commit_id> .
    – user591410
    Aug 12, 2015 at 18:14
  • If you run git branch what is the output?
    – Razvan
    Aug 12, 2015 at 18:14
  • If I do a git branch, I dont see that "HEAD is detached at commit_id"
    – user591410
    Aug 12, 2015 at 18:14
  • I believe you have git checkout master after committing the fixes. If so, then this is the question you should ask: how-do-i-get-my-detached-head-commits-back-into-master
    – Razvan
    Aug 12, 2015 at 18:21
0

You are on a detached branch. Do a git log to get your current commit id, go back to your working branch (git checkout yourBranchName), and then git merge YourCommitId, which will merge those changes on top of your branch (or you could try to rebase then onto your current branch).

2
  • I did these on the master branch. I don't have a separate branch
    – user591410
    Aug 12, 2015 at 18:04
  • When you did a "git checkout <commit_id>" you are put onto what is called a detached branch by git since you are off on a commit without a branch name to follow this work. Aug 12, 2015 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.