user2731584
user2731584

Reputation: 986

git clone and checkout in a single command

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?

Upvotes: 67

Views: 61735

Answers (5)

Konrad Botor
Konrad Botor

Reputation: 5033

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>

Upvotes: 1

mobibob
mobibob

Reputation: 8794

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>)

Upvotes: 0

jthill
jthill

Reputation: 60255

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.

Upvotes: 95

michas
michas

Reputation: 26505

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

Upvotes: 6

Mir S Mehdi
Mir S Mehdi

Reputation: 1530

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>

Upvotes: 3

Related Questions