Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to clone the first commit?

Tags:

git

Could anyone tell me how to clone the first commit? I don't want to delete recent commits, just make a clone copy of the initial state so I can grab some of the files.

like image 258
igniteflow Avatar asked Jun 22 '10 12:06

igniteflow


4 Answers

If by "first commit", you mean the parent commit of a branch that does not have any parents itself, then you want to use the git-rev-list to do that. Just use

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

That will get your "Initial commit" for the current HEAD. Use master if you list, or any other child revision specification. wrap that in $(...) in order to use that in an expression, such as:

git checkout $(git rev-list --max-parents=0 HEAD)

like image 96
Michael Erickson Avatar answered Oct 11 '22 06:10

Michael Erickson


Updated 2018 answer: Use your own symbolic ref, ie. tag. And start using describe and annotated tags.

git tag -a -m "Initial version" root $( git rev-list --all | tail -n 1 )
git describe

Output description for a repo that never received a tag but occasionally committed to since 2011 with above tag:

root-1090-g88a80e93

If you want to discard all other history on the other hand I've never needed that, it is easy enough to copy the initial set. I can see the use-case if you wanted to refer to the original 'root' commit and stamps though. Maybe look at how ie. BFG work, --depth or GIT 2.19 its new clone --filter. [3]


I think I want to do the same thing. Checkout the initial commit, and branch from there. I'm not sure with the answer accepted. But I guess it could (partly) fullfil the original question. After reading this thread, I however will go with some bash scripting around

git log --pretty=oneline  master | tail -1

I hoped there would be some commit- or tree-ish reference for this. Maybe to list nodes marked as root, but I suppose GIT by itself does not track this. So this method only works on a repository with only one root commit.

The 2017 and 2018 updates are that there is a better option to git log, using git rev-list but that suffer from the same problem. They simply use the trailing item of a list of commits still.

You can checkout the 'root commit' for a repository with only one root with the following, and it works for most of the repositories. But it is a hack, and it would be better to review your system why and where it needs to mark the root commits. And simply tag it or record it.

You can checkout the first commit in a repo with one root with the following command:

git checkout $( git rev-list --all | tail -n 1 )

or (GIT <1.8.1):

git checkout $( git log --pretty=oneline | tail -n 1 | sed 's/ .*$//' )

See git-revlist and also Using git, how do I go back to first commit then go through the history?.

[3]:

like image 41
mpe Avatar answered Oct 11 '22 06:10

mpe


To answer the question I think you meant to ask:

You can get your entire repository into the state of the first commit with:

git checkout <commit SHA1>

After you're done messing around, you can do git checkout master to get back to where you were.

And you can get individual files into their state from the first commit with:

git checkout <commit SHA1> <file or directory> ...

Again, after you're done, you can do git checkout master <file or directory> to get back to where you were.

There's no need to "clone" a commit (by which I assume you mean clone the repository, and check out the first commit?). Of course, if for some reason you couldn't bear to modify any files in your repository (e.g. don't want to make your build out of date) you could of course clone it, then do the exact same thing in the cloned repo.

like image 43
Cascabel Avatar answered Oct 11 '22 07:10

Cascabel


Use command:

git fetch --depth=1 ../testrepo/.git $SHA1

This will only clone the certain commit. You can checkout this answer for more details.

like image 33
ramwin Avatar answered Oct 11 '22 07:10

ramwin