384

What is the difference between git clone and git checkout?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117
  • 1
    Related: http://stackoverflow.com/questions/3329943/difference-between-a-branch-fork-and-clone-in-git – Mat Sep 04 '11 at 10:38

5 Answers5

373

The man page for checkout: http://git-scm.com/docs/git-checkout

The man page for clone: http://git-scm.com/docs/git-clone

To sum it up, clone is for fetching repositories you don't have, checkout is for switching between branches in a repository you already have.

Note: for those who have a SVN/CVS background and new to Git, the equivalent of git clone in SVN/CVS is checkout. The same wording of different terms is often confusing.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
  • 42
    `checkout` can be used to other things too, like overwriting your a file in your working copy with a version of that file from another revision. – svick Sep 04 '11 at 10:44
  • 8
    and when do you use "pull" vs "checkout" ? – Kokodoko May 25 '15 at 11:16
  • 28
    pull is a fetch plus merge, checkout is a local operation that only operates on data that's already fetched. So it's not like svn checkout at all. – August Lilleaas May 26 '15 at 12:02
  • 14
    Coming from SVN world, I was also confused. Wow.. what a mess. In an ideal world I would enforce every source control system manufacturer to use the same terms. – Zoltán Tamási Nov 20 '15 at 08:46
  • 1
    What will the difference then be between a fork and a clone? – Werner Nov 24 '15 at 08:03
  • 6
    Fork is a github term and is not related to git itself. – August Lilleaas Nov 24 '15 at 18:08
  • 1
    Having come from TFS and SVN, i still find this confusing. Perhaps I am using it wrong to "checkout" different branches into separate root folders. Is there some documentation on best practices for git? – tatmanblue Jun 21 '17 at 15:49
  • 4
    quote some one's comment: git makes difficult things possible, make every thing else difficult – Charlie May 24 '18 at 23:53
  • And what if when learning git my first operation using SourceTree was to "clone" the master but SourceTree automatically "checked out" the master branch? This had nothing to do with "switching" between branches since there is only one branch right now. I simply wanted to fetch (i.e. clone) the repository files without altering any state on the server, but to me "checkout" implies that I've now altered something on the server. The simple explanation in the answer still does not help distinguish between the two operations in my simple case. – C Perkins Feb 19 '20 at 22:14
130

git clone is to fetch your repositories from the remote git server.

git checkout is to checkout your desired status of your repository (like branches or particular files).

E.g., you are currently on master branch and you want to switch into develop branch.

git checkout develop_branch

E.g., you want to checkout to a particular status of a particular file

git checkout commit_point_A -- <filename>

Here is a good reference for you to learn Git, lets you understand much more easily.

Ali
  • 2,228
  • 1
  • 21
  • 21
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • 20
    "from the remote git server" - it is not necessary for server to be remote. `git clone` will also works with local repos. – SET001 Nov 28 '12 at 01:29
  • 1
    Thanks for the link to a visual reference to git! – David Pointer Aug 10 '16 at 14:45
  • @Kit Ho: The link is fine as a reference, but its not much help for someone with basic questions about git like the person above. As the article itself says, "Once you know a bit about how git works, this site may solidify your understanding" – Motorhead Oct 24 '16 at 22:04
  • 1
    Circular definitions aren't so helpful. The word "fetch" used to describe clone is useful and adds meaning, but the phrase "checkout is to checkout..." doesn't add meaning, nor does it actually help to distinguish the difference between the two operations. – C Perkins Feb 19 '20 at 22:10
  • 1
    @Kit ho, your `good reference` link is broken, might be you are referring to this - https://marklodato.github.io/visual-git-guide/index-en.html – Indrajeet Gour Dec 15 '21 at 07:56
11

One thing to notice is the lack of any "Copyout" within git. That's because you already have a full copy in your local repo - your local repo being a clone of your chosen upstream repo. So you have effectively a personal checkout of everything, without putting some 'lock' on those files in the reference repo.

Git provides the SHA1 hash values as the mechanism for verifying that the copy you have of a file / directory tree / commit / repo is exactly the same as that used by whoever is able to declare things as "Master" within the hierarchy of trust. This avoids all those 'locks' that cause most SCM systems to choke (with the usual problems of private copies, big merges, and no real control or management of source code ;-) !

Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
  • 4
    The question doesn't mention locks and it should be assumed by default in these days that a person is not familiar with this concept so these differences from old VCSes should be explained only if asked explicitly. – wRAR Sep 04 '11 at 13:27
8

Simply git checkout have 2 uses

  1. Switching between existing local branches like git checkout <existing_local_branch_name>
  2. Create a new branch from current branch using flag -b. Suppose if you are at master branch then git checkout -b <new_feature_branch_name> will create a new branch with the contents of master and switch to newly created branch

You can find more options at the official site

Khader M A
  • 5,423
  • 3
  • 19
  • 19
  • Nice. This `-b` option is awesome which creates a new local branch and checks it out as well at the same time in single command. Loved it! – RBT Jan 16 '18 at 06:29
3

checkout can be use for many case :

1st case : switch between branch in local repository For instance : git checkout exists_branch_to_switch

You can also create new branch and switch out in throught this case with -b

git checkout -b new_branch_to_switch

2nd case : restore file from x rev

git checkout rev file_to_restore ...

Goms
  • 2,424
  • 4
  • 19
  • 36