What do git checkouts really mean?

后端 未结 3 1036
执笔经年
执笔经年 2020-12-02 07:25

What are checkouts in git?

I know once you do checkout to a particular branch, the HEAD points to that branch. But what does

3条回答
  •  广开言路
    2020-12-02 08:11

    As you noted, HEAD is a label noting where you are in the commit tree. It moves with you when you move from one commit to another. git checkout is the basic mechanism for moving around in the commit tree, moving your focus (HEAD) to the specified commit.

    The commit can be specified by any of a number of ways, commit hash, branch name, tag name, the relative syntax (HEAD^, HEAD~1, etc.) and so on. It is often useful to consider a checkout to be changing branches, and there are some options that work from that perspective, but they all reference commits.

    To checkout a commit has some side affects other than moving HEAD around.

    • The working directory is updated to the state of the checked out commit.
    • if a branch name is specified, checkout makes that branch active. The active branch will move along with any new commits that are added.
      • with the -b option a new branch will be created based on the current commit and then made active.
      • with the --track option the checked out branch can be made aware of a remote branch
      • with the --orphan option a new branch is created (like with -b) but will not be based on any existing commit.

    There are a few more options, which you can read about in the git checkout man-page, all of which revolve around moving from one commit to another -- just varying in what effect that move has in addition to moving HEAD.

提交回复
热议问题