6

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?

IQAndreas
  • 8,060
  • 8
  • 39
  • 74
  • And it seems silly running `git reset HEAD^` in a `while` loop. – IQAndreas May 19 '14 at 18:28
  • Does these help you: http://stackoverflow.com/questions/18407526/git-how-to-find-first-commit-of-specific-branch and http://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git – Henrik Andersson May 19 '14 at 18:30
  • @limelights Not quite the same thing, I'm looking for the **entirely** first commit, not the first commit since the branch diverged from the other branches. – IQAndreas May 19 '14 at 18:34
  • @limelights However, using `rev-list` may be the answer (although the arguments would need to be tweaked greatly), so links were helpful, although not right on-topic. – IQAndreas May 19 '14 at 18:36
  • 1
    great, they were meant as a pointer in the right direction :) – Henrik Andersson May 19 '14 at 18:38

3 Answers3

5

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.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
2

This is one way:

git reset --hard `git rev-list --all | tail -1`
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 1
    This could pick the wrong root in a multi-rooted repository (e.g., git's own git repo). (Note: it actually works there, at least if you're on any of the normal source chains; or did for me. The roots of all other trees come out before the root of the main tree.) – torek May 19 '14 at 18:44
1

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.

Schleis
  • 41,516
  • 7
  • 68
  • 87