imranal
imranal

Reputation: 656

Undo git checkout --orphan

I "mistakenly" checked-out my git repo as an orphan branch. Is there a way to revert this command ? I have not made any commits or even changed the repo any other way.

Upvotes: 4

Views: 2146

Answers (3)

VonC
VonC

Reputation: 1324178

Since the branch has been created (without any parent or commit), I would reset it to the current branch it should have started from:

# check that your current branch is the orphan one
git branch

git reset <anExistingBranch>
# or
git checkout -B <anExistingBranch>

A commit done from there will be done in the new branch, with the HEAD from <anExistingBranch> as parent.
That would "un-orphan" your orphan branch.

(I assume this is about a new branch, since a git checkout --orphan on an existing branch would not work "fatal: A branch named 'xxx' already exists")

Upvotes: 4

Gregg
Gregg

Reputation: 2594

git reflog -3 && echo see your previous commits

git checkout head@{1} && echo to checkout your previous commit

Upvotes: -1

Vampire
Vampire

Reputation: 38639

Just check out the branch you want to be with a normal checkout.

Upvotes: 2

Related Questions