Barney Szabolcs
Barney Szabolcs

Reputation: 12524

How to back out from back out?

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?

Upvotes: 1

Views: 32

Answers (1)

VonC
VonC

Reputation: 1327004

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^).

Upvotes: 2

Related Questions