Reputation: 3190
I did a dumb thing with git. I had (good) loocal changes to file_a, which I had committed but not pushed. My coworker made (bad) changes to file_a, which she then pushed. I pulled her changes, and rebased them in without fully realizing what was going on. Now file_a is hopeless borkered up, and I'm not sure how to fix it.
Obviously, in the future, pushing more often is one key. What I'd like to do now is undo the rebase, and redo it more thoughtfully. I want to make sure I don't lose my local commits, though, and I'm not sure how to do that.
If I do this command: git reset --hard ORIG_HEAD
, will that preserve my two local commits? Or should I do something else?
Update:
I ended up finding the good commits using the reflog. Then, I cherry-picked them in using another branch and verified that everything was working as expected. Then I used git revert fix_rebase_branch --hard
in my original branch.
Upvotes: 1
Views: 1445
Reputation: 489073
The easy way works only if you have not yet "undone" the rebase, nor done much else besides look around (so that your current branch is still just the result of the rebase, and there is no un-committed work lying around). This also requires that you not be using rebase's autostash feature, or if you are using autostash, that you manually git stash
before step 2, if you have any uncommitted work-tree changes.
git branch save-my-rebased-work
to create a new branch name pointing to current commit (pick a name that will be obvious to yourself; note that git branch
will create this new branch with no upstream set).git reset --hard ORIG_HEAD
to undo the rebase and make your current branch (and work-tree) look the way it did before the rebase.If you have already undone the rebase, use the reflogs to find the rebase's new commits. Remember that what rebase does is to copy your original commits to new copies. Those new copies are retained via the reflogs after a git reset --hard ORIG_HEAD
(until the reflog entries expire in a month or so, anyway). The reason to make a new branch name in step 1 is that the originals and copies look almost exactly the same, and are really hard to tell apart in the reflogs.
Upvotes: 0
Reputation: 17527
I would walk back through git reflog
and make a new branch (i.e. using git checkout -b <new-branch-name> <orphaned-commit-hash>
) from the commit that you want. The rebase will have rewritten your two recent commits, but the originals should still be there in the reference log.
Upvotes: 2