Does anybody know how to easily undo a git rebase?
The only way that comes to mind is to go at it manually:
What I usually do is
git reset #commit_hash
to the last commit where I think rebase had no effect.
then git pull
Now your branch should match exactly like master and rebased commits should not be in it.
Now one can just cherry-pick the commits on this branch.
Using reflog
didn't work for me.
What worked for me was similar to as described here. Open the file in .git/logs/refs named after the branch that was rebased and find the line that contains "rebase finsihed", something like:
5fce6b51 88552c8f Kris Leech <me@example.com> 1329744625 +0000 rebase finished: refs/heads/integrate onto 9e460878
Checkout the second commit listed on the line.
git checkout 88552c8f
Once confirmed this contained my lost changes I branched and let out a sigh of relief.
git log
git checkout -b lost_changes
For multiple commits, remember that any commit references all the history leading up to that commit. So in Charles' answer, read "the old commit" as "the newest of the old commits". If you reset to that commit, then all the history leading up to that commit will reappear. This should do what you want.
git reset --hard origin/{branchName}
is the correct solution to reset all your local changes done by rebase.
I tried all suggestions with reset and reflog without any success. Restoring local history of IntelliJ resolved the problem of lost files
The easiest way would be to find the head commit of the branch as it was immediately before the rebase started in the reflog...
git reflog
and to reset the current branch to it (with the usual caveats about being absolutely sure before reseting with the --hard
option).
Suppose the old commit was HEAD@{5}
in the ref log:
git reset --hard HEAD@{5}
In Windows, you may need to quote the reference:
git reset --hard "HEAD@{5}"
You can check the history of the candidate old head by just doing a git log HEAD@{5}
(Windows: git log "HEAD@{5}"
).
If you've not disabled per branch reflogs you should be able to simply do git reflog branchname@{1}
as a rebase detaches the branch head before reattaching to the final head. I would double check this, though as I haven't verified this recently.
Per default, all reflogs are activated for non-bare repositories:
[core]
logAllRefUpdates = true