Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 930
user2731584 Avatar asked Aug 30 '13 04:08

user2731584


People also ask

How do I create a branch and checkout in one command?

The easiest way to create a Git branch is to use the “git checkout” command with the “-b” option for a new branch. Next, you just have to specify the name for the branch you want to create. To achieve that, you will run the “git checkout” command with the “-b” option and add “feature” as the branch name.

Does git clone do a checkout?

The git checkout command may occasionally be confused with git clone . The difference between the two commands is that clone works to fetch code from a remote repository, alternatively checkout works to switch between versions of code already on the local system.

Does git clone checkout master?

While you can clone repositories with the git clone command, keep in mind that this clones the branch and the remote HEAD . This is usually master by default and includes all other branches in the repository. So when you clone a repository, you clone the master and all other branches.


4 Answers

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.

like image 170
jthill Avatar answered Oct 10 '22 18:10

jthill


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

like image 45
michas Avatar answered Oct 10 '22 20:10

michas


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>
like image 3
Mir S Mehdi Avatar answered Oct 10 '22 18:10

Mir S Mehdi


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>)
like image 1
mobibob Avatar answered Oct 10 '22 19:10

mobibob