61

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
user2731584
  • 926
  • 1
  • 7
  • 17

5 Answers5

87
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.

Mr. Kaffe Kup
  • 50
  • 1
  • 6
jthill
  • 55,082
  • 5
  • 77
  • 137
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

michas
  • 25,361
  • 15
  • 76
  • 121
4

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>
Mir S Mehdi
  • 1,482
  • 3
  • 19
  • 32
  • 8
    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. – davidA Apr 07 '16 at 22:12
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>
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • 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. – balu Aug 16 '23 at 16:42
  • I haven't tested it, but it should. After all 3 are valid references to a commit. – Konrad Botor Aug 24 '23 at 17:47
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>)
mobibob
  • 8,670
  • 20
  • 82
  • 131