git clone and checkout in a single command
Asked Answered
J

5

65

The following is the command I use to checkout a specific commit.

git clone git://repo.git/repo123
git checkout <commitID>

I want to do the above in one step - using a git clone command only.

The reason why I want to do this is, repo123 is very huge. So checking out the commit I want will save me a lot of time.

I am aware of --depth option. But in this case, it is of no use. Can anyone tell me how to do it?

Julian answered 30/8, 2013 at 4:50 Comment(1)
What is the problem of --depth?Dagger
K
91
git clone u://r/l --branch x

still clones everything but sets the local HEAD to that branch so it's the one checked out.

Source:

--branch <name>
-b <name>
Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository’s HEAD, point to <name> branch instead. In a non-bare repository, this is the branch that will be checked out. --branch can also take tags and detaches the HEAD at that commit in the resulting repository.

Kreegar answered 10/9, 2013 at 23:23 Comment(1)
That is what I have been wanting to implement .. that is very helpful thanks,Renshaw
D
6

Is your problem the checkout being to large or the repository itself? As git clone, well, clones a repository you usually get the whole repository in its full size. (unless you are doing a shallow clone as you already suggested.)

If it's really about the checkout of the wrong branch git help clone says:

   --no-checkout, -n
       No checkout of HEAD is performed after the clone is complete.

After cloning with -n you can manually check out

Dagger answered 30/8, 2013 at 6:18 Comment(0)
V
3

I was running into a same situation and it worked well with the Git Clone Command with --depth. And specify the branch-name/commit/Tag-Name at the end of the command with -b parameter.

Syntax:

git clone --depth 1 github.com:ORG-NAME/Repo.git -b <Branch-Name/Commit-Number/TAG>
Vedette answered 30/8, 2013 at 5:15 Comment(1)
Does this really work for a commit SHA1? I've tried it with git 2.5.0 and although it accepts branch names and tags, it doesn't work with commits.Sapphire
E
1

This is an old question, but judging by dates of answers and comments it's still relevant, so I figured I'd add my few cents.

If the commit you want to clone is a tip of the branch or a tag, you can - as mentioned in other answers - do:

git clone --depth 1 --branch <branch/tag name> <repository URL>

If, however, you want to clone the commit by its SHA (as indicated in the question), you need to run several commands:

mkdir -p <local repository directory>
cd <local repository directory>
git init
git remote add origin <repository URL>
git fetch --depth 1 origin <commit SHA>
git checkout FETCH_HEAD

If you have to do it often, you could make it into a function/cmdlet.

For example in Bash:

git_clone_commit() {
    mkdir -p "$1"
    pushd "$1"
    git init
    git remote add origin "$3"
    git fetch --depth 1 origin "$2"
    git checkout FETCH_HEAD
    git submodule update --init --recursive
    popd
}

and then

git_clone_commit <local repository directory> <commit SHA> <repository URL>
Eloquent answered 17/11, 2022 at 10:5 Comment(2)
Just to be clear: Your second code snippet / the Bash function works for both commit SHAs and branches/tags, correct? After all, git fetch supports both.Rambow
I haven't tested it, but it should. After all 3 are valid references to a commit.Eloquent
S
0

I think you just want to be able to "walk away" and return when both steps have completed. I use this line for two long-running commands on a single line -- and I like to "time" the overall action.

The trick is the semi-colon between each command.

$ time (git clone git://repo.git/repo123 ; git checkout <commitID>)
Staging answered 7/1, 2021 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.