HEAD in Git

1. Git HEAD storage location

HEAD refers to .git/HEADthe file, which stores the name of the current branch. We can open this file to take a look:

ref: refs/heads/master

From this, we can know that we are currently on the master branch.

If we continue going down: open refs/heads/masterthe file, a string of passwords comes into view:

7e136f508b982790db5686482075c60ee3ee4fed

This is the commit id of the latest commit on the master branch.

In fact, we can look at the diagram below and our understanding of the header will be clear at a glance:

Insert image description here

2. The role of Git HEAD

2.1. Function 1 of Git HEAD: Show which branch you are currently on

When we check the git commit log, we often encounter head, as shown below:

$ git log
commit c4f9d71863ab78cfca754c78e9f0f2bf66a2bd77 (HEAD -> master)

As shown above, HEAD -> master tells us that we are currently on the master branch.

2.2. Function 2 of Git HEAD: refers to the commit id of the latest submission of the current branch

Sometimes, after we commit the code, we find that the content of the commit is wrong. There are two solutions to this situation:

解决方法1:修改错误内容,再次commit一次

解决方法2:使用git reset命令撤销这一次错误的commit

At this point, the head appears, which is often used in conjunction with reset, as shown below:

$ git reset HEAD <file>

Equivalent to:

$ git reset commit_id_latest <file>   //commit_id_latest 需替换为实际的最新的commit it 

Because head represents the commit id of the latest commit of the current branch, the purpose of the above command is to restore the file file to the specified commit id.

In addition, we can view the latest submitted changes through:

git diff  HEAD^ HEAD

HEAD^ represents the penultimate submission, that is, the most recent submission.
HEAD^ represents the penultimate submission.
HEAD^^ represents the third to last submission.

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/132581163