IQAndreas
IQAndreas

Reputation: 8468

Reset to the very first commit in Git?

Is there anything equivalent to the --root flag in the rebase command for the reset command?

git reset --root

Say I want to reset to the very first commit in my current branch: do I have to manually dig through the history and find the hash of that commit, or is there a simple way to reset to the first available commit?

Upvotes: 8

Views: 5584

Answers (3)

torek
torek

Reputation: 489323

A root commit (there can be more than one) is a commit with no parents.

This finds the root commit of the current commit (basically, "root of current branch" except that it works even with a detached HEAD):

git rev-list --max-parents=0 HEAD

This finds all root commits on all branches:

git rev-list --max-parents=0 --branches

and of course you can use --tags or --all instead of --branches.

Usually there's only one root in a repository so that all of these find the same revision, and rev-list prints revisions in a suitable order by default, so manojlds' answer will generally also work.

Edit: and of course, you have to provide the resulting SHA-1 to git reset.

Upvotes: 6

Schleis
Schleis

Reputation: 43760

I didn't find the a way within git reset but you would be able to reset to the initial commit of a repo with the following one-liner:

git log --pretty=format:%H | tail -1 | xargs git reset

Basically use git log to find the first commit and then using xargs you can reset back to it.

Upvotes: 1

manojlds
manojlds

Reputation: 301347

This is one way:

git reset --hard `git rev-list --all | tail -1`

Upvotes: 2

Related Questions