One of git Checkout's head basic and detached statuses

Source: Internet
Author: User

1. Head Basics

Git checkout actually modifies the content of the head file and points it to a different branch.

The Branch pointed to by the head file is the current branch.

Generally, the head content is directed to the master file in the staging (temporary storage area.

ref: refs/heads/master

Of course, it can also point to other index files. In any case, the content of this index file is controlled by git reset.

The results shown by the GIT branch command are the same as those in the head file.

$ git branch -v* master 1aea8d9 [ahead 1] add test file x

2. Simple usage

Git checkout is the simplest method to display the differences between the workspace, staging area, and head:

$ git checkoutMxYour branch is ahead of 'origin/master' by 1 commit.

This means that my local warehouse is ahead of the remote warehouse for one commit operation. Git checkout head has the same functions.

If you use the-a parameter, you can see a lot of branch, including remote branch, for example:

git branch -a* master  remotes/origin/HEAD -> origin/master  remotes/origin/develop  remotes/origin/issue_193  remotes/origin/issue_210  remotes/origin/master

3. Detached head

If you point the head file to a commit ID, it becomes a detached head. Git checkout can achieve this effect. Run the following command:

git checkout 1aea8d9^

Laea8d9 is the latest commit ID, ^ refers to the previous one, so the above operation result is that the head file contains the last and second submitted IDs.

The following shows how to enter the datached head status and restore it.

$ git branch -v* master 89f8dae [ahead 2] update x$ git checkout 89f8dae^Note: checking out '89f8dae^'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example:  git checkout -b new_branch_nameHEAD is now at 1aea8d9... add test file x

Okay, now it's back.

$ git checkout masterPrevious HEAD position was 1aea8d9... add test file xSwitched to branch 'master'Your branch is ahead of 'origin/master' by 2 commits.

I don't know what the actual use of the detached head is. It is a function that allows the head to point to a commit ID without worrying about which branch it is.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.