1

I was working on a branch and by accident I've done git reset HEAD~1 because I thought I'm resetting my last commit.

The problem is that I didn't even commit my changes so I've done reset to commit which was done by someone else. A lot of changes in that commit were done on files on which I was also working on so I didn't notice and continued my work. After I've committed and pushed my changes, I've noticed missing commit.

develop branch: commitA -> commitB -> commitC

my branch: commitA -> commitB -> myCommit

Is there any way to revert those changes and insert commitC before my commit?

1
  • git commit -a -s -c ORIG_HEAD, but it will not include added files, you have to run git add ...<list of files>... beforehand.
    – 0andriy
    Commented Jan 9, 2017 at 13:02

2 Answers 2

4

You can see your working tree by git reflog. First, back to commitC and pick your myCommit top of git log stack. Then just update remote.

$ git reflog
# copy the commit-hash of 'commitC'

$ git checkout <commitC-hash>
$ git reflog
# copy the 'myCommit-hash'

$ git cherry-pick  <myCommit-hash>         # take the 'commitC' top
$ git checkout -b 'new-my-branch'          # create 'new-my-branch' from current stage

# Replace 'my-branch' with 'new-my-branch'
$ git branch -D my-branch                  # delete 'my-branch'
$ git checkout -b my-branch                # create new 'my-branch' from 'new-y-branch'
$ git push -f origin HEAD                  # replace remote/my-branch by local/my-branch 
1
  • It's too way far from simple solution you may offer.
    – 0andriy
    Commented Jan 9, 2017 at 23:33
0

You don't need to revert your commit, because you can rebase your local branch on the remote one, and cleanly bring in commitC before reapplying your commit on top of that.

I don't know exactly what you have done locally to end up with the branch diagrams you showed us, but I think that you can simply rebase your branch on the remove develop to pull in commitC, and then replay your myCommit on top of it:

git fetch origin           # bring origin/develop up to date
git checkout develop       # checkout local develop branch
git rebase origin/develop  # rebase your local branch on the remote one

Now you should be able to simply fast-forward the remote branch via:

git push origin develop
2
  • Too complicated. git rebase --onto origin/develop HEAD~1
    – 0andriy
    Commented Jan 9, 2017 at 13:03
  • @0andriy You say that my answer is too complicated when the one receiving the upvotes is way more complex. Also, I don't think we need rebase --onto here. Commented Jan 9, 2017 at 13:26

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