I have backed out from a commit (commit#1), but after that I realized I needed the commit.
So I have checked out commit#1. Now, how can I put back this version to the end of the branch?
Asked
Active
Viewed 29 times
1

Barney Szabolcs
- 11,846
- 12
- 66
- 91
-
What do you mean by "back out"? i.e. what was the sequence of Git commands that you ran? – Oliver Charlesworth Apr 06 '14 at 17:55
-
@OliCharlesworth never mind, I've found a solution. here: http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit?rq=1 – Barney Szabolcs Apr 06 '14 at 17:59
1 Answers
2
If you haven't done any other work, you simply can reset your current branch on that commit.
git checkout yourBranch
git reset --hard <sha1>
The OP Barnabas Szabolcs also advices in the comments:
git reset --soft @^
(with recent git, @ is an alias for HEAD)
That would allow to keep the working tree and index (of the checked out old commit) intact, while resetting HEAD to the previous commit in order to make a new one (with the content of the checked out commit).
If you have done other commits, you need to cherry-pick that commit into your current branch:
git checkout yourBranch
git cherry-pick <sha1>
In any case, a checkout of the old commit itself is dangerous, as it leads to a detached HEAD situation (except when followed by a git reset ---soft HEAD^
).
-
@BarnabasSzabolcs indeed. I have included it in the answer for more visibility. – VonC Apr 06 '14 at 18:02