Why git checkout after git clone?

14,275

Solution 1

Note: I started this as a comment but it's long enough, and really can use some formatting, that I moved it to an answer. It is a reply to your comment to Tim Beigeleisen:

If you refer to my example link, checking out "stereo_image_proc" works but I can't see it being one of the branches, how come?

The reason git checkout stereo_image_proc does not complain (and seems not to do anything, at first blush) is that git checkout itself is really two different commands combined into one. This is a feature, or misfeature in some people's opinion (including mine): git checkout's argument can be a branch name, or a path name.

Specifically:

  • git checkout branch asks Git to switch to, or sometimes even create and then switch to, a branch whose name you supply on the command line.

    Switching to a branch is a surprisingly complicated process, in the end, but it starts out simple enough: it changes Git's notion of HEAD so that you're on the named branch. It has another very useful feature: before Git actually switches to (and/or creates) this branch, Git makes sure that this won't clobber any work you accidentally started on the wrong branch.

  • git checkout name1 name2 ... nameN, on the other hand, asks Git to extract particular files from some named or implied commit. This is often best written out as git checkout -- file, where the -- tells Git that the name should not be treated as a branch name. That is, suppose you have a file named master and you want to extract it: then git checkout master would not work because that's the branch named master, but you want the file named master. So git checkout -- master tells Git: not the branch, the file.

    When you use this kind of git checkout, you're telling Git: I know I started editing some file or files, but I have now decided that editing this file, or all of these files, was a mistake. Put them all back the way they were, turning them back into a previous version of each file. For instance, suppose you have a file named README.txt and you started editing it and then realized that you should be creating a new documentation file. You copy the new stuff you added to the new file, but now you want README.txt to go back to the way it was before you started editing it. So you run git checkout README.txt, and that wipes out your changes to the file.

    But as far as Git is concerned, naming a directory here (or a folder if you prefer that term) means every file in the directory, including any sub-directories recursively. Since stereo_image_proc is a directory, and is not a branch name, you are getting this second form of git checkout.

The bottom line is that git checkout stereo_image_proc tells Git to wipe out any changes you made to any files within that directory. If you have not made any changes, well, no problem! But if you have, this can be pretty disastrous.

Since git checkout does have these two modes—the safe switch branches mode, and the unsafe clobber all my work mode—you have to take note of which one you are invoking, every time you run git checkout.

Solution 2

The first two steps just tell you to git clone, which will clone into some folder, and then to cd into that folder. The third step would only be necessary if you didn't want to start working with the master or default branch of that repo. Typically, after git cloning, the master branch would be selected by default. So, if your instructions wanted you working on some other yyy branch, then the third step would make sense.

Running git checkout yyy will automatically create a local branch named yyy if there is a tracking branch in your repo by the same name. In this case, it would also switch to that newly created local branch.

Share:
14,275

Related videos on Youtube

Shibalicious
Author by

Shibalicious

Updated on June 04, 2022

Comments

  • Shibalicious
    Shibalicious almost 2 years

    I am new to git. I understand git basics as well as development procedures using git, however there is one thing that confuses me.

    Whenever I have to pull something from a git repo with multiple projects inside (example), I see these instruction steps:

    1. git clone xxx
    2. cd xxx
    3. git checkout yyy

    This is a bit puzzling to me. As I already have the entire repo, why would I want to checkout the project I am interested in if I can just copy the folder and do whatever I like with it?

  • Shibalicious
    Shibalicious over 5 years
    Excellent answer, thank you. If you refer to my example link, checking out "stereo_image_proc" works but I can't see it being one of the branches, how come?
  • Tim Biegeleisen
    Tim Biegeleisen over 5 years
    Define can't see. When you run git branch -a, are you saying you don't see a local/remote branch by the name stereo_image_proc ?
  • Shibalicious
    Shibalicious over 5 years
    I do but not exactly "stereo_image_proc", I can see many versions of them. If you go to the github page and look in the "branches" tab you will see what I mean.
  • Tim Biegeleisen
    Tim Biegeleisen over 5 years
    Then it sounds like the issue is you're not sure about your branch names.
  • Shibalicious
    Shibalicious over 5 years
    Just to reiterate, there is no exact match for "stereo_image_proc" branch, on the other hand there is a branch called "stereo_image_proc_new_point_cloud". However, checking out "stereo_image_proc" works just fine.
  • Tim Biegeleisen
    Tim Biegeleisen over 5 years
    This is a departure from your initial question. You weren't asking about how to find the right branch.
  • Shibalicious
    Shibalicious over 5 years
    My apologies, you have already answered my original question. Would you mind joining the chat for a brief moment?
  • Shibalicious
    Shibalicious over 5 years
  • torek
    torek over 5 years
    @TimBiegeleisen: he asked you, in his own first comment on your answer: If you refer to my example link, checking out "stereo_image_proc" works but I can't see it being one of the branches, how come?
  • Tim Biegeleisen
    Tim Biegeleisen over 5 years
    You must have picked up on something I missed +1.
  • Shibalicious
    Shibalicious over 5 years
    The more detailed answer I was looking for, excellent, really appreciate this!