Reattaching HEAD in Git
A Git repository can be defined as a set of objects and references. Branches are the objects we use to represent our work. Git also handles tags that refer to commits in a specific branch.
commit
Probably the state of the source code at a point in time. Commits
Includes parents and children, complete with data about who created the commit and when. 提交
Placed in the repository as an object in a branch.
HEAD
The reference points to the latest commit in the current branch. HEAD
The pointer is a reference to the currently checked out branch, and it points to the top of the branch.
However, you can go back in time without checking out a branch. You can use HEAD
the pointer to grab any commit in a branch, and then you can use the index to easily grab any version of a file.
You can use HEAD
the pointer and index pointer 分离 HEAD
to perform changes to a specific commit, which is called a state in Git 签出
. In addition, you can check out a specific commit and create a new branch based on a specific commit in a branch.
If we only do this occasionally, this won't be a problem. However, if we do this many times, we'll quickly start to wonder how to get back to the branch we were working on.
The solution is simple, before we check out a branch, we should use the command git checkout master
. This command takes us back to the branch we were working on before the commit was made, but it does not affect the commit we are checking out.
A clean solution would be to set up a new Git repository dedicated to holding the patch series, and make it available for others to pull the latest branch at any time.
These situations are so rare that the usability and performance costs of implementing a detached HEAD outweigh their benefits, so Git currently lacks this feature. Git can amend commits. However, it is not possible to amend the last commit on a detached HEAD.
Git has a way to permanently delete commits by creating a secret branch, recording the commit data in that branch, and then HEAD
permanently deleting the commit from that branch. However, this feature is only HEAD
available when a single commit is detached from . If a commit has multiple parents, it is impossible to delete it from that branch.
Detaching HEAD in Git
However, when we switch to another branch, the situation is different. If we check out our working directory, it will be updated HEAD
and we can no longer modify the files in it, or we can create some new changes if they do not conflict with the branch we switched to.
In this case, HEAD
detach from the current branch. At the same time, we can use git checkout
the command to change branches without updating the working directory to HEAD
, so HEAD
detach can be attached at the same time.
This is confusing, but it comes in handy if we want to switch between branches without quickly touching the working directory or switch to a different branch and checkout a new working directory at the same time. We can use git checkout -b <newbranchname> <commit>
to do this.
We can do this like this; just check out the branch we want.
$ git checkout <branch>
For example:
$ git checkout master
If we want to keep the changes we are working on, we should create a new branch or store our changes in a branch. Anything that is not the most recent commit of our branch name 签出
will give us a detached HEAD.
When HEAD
detached, commits look normal, except that the named branch is not updated. It's like an unknown branch. For example, we can say that if we check out a remote branch first, and then track it; eventually, we will end up with a detached HEAD
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Switch remote Git branch
Publish Date:2025/04/04 Views:182 Category:Git
-
This article will show you how to switch remote Git branches using checkout the command. The version control tool that allows you to support and manage variable versions of your application in a team of members is the Git version control to
Head in Git
Publish Date:2025/04/04 Views:166 Category:Git
-
Most of the time, in our Git documentation, head refers to the top of a Git repository, called the repository's head . But the question is what exactly is head HEAD in Git ? HEAD In this article, we will learn about that Git HEAD , but befo
Stop tracking a remote branch in Git
Publish Date:2025/04/04 Views:123 Category:Git
-
This article explains how we can stop tracking a remote branch in Git. By now, you must be very familiar with the concept of tracking remote branches. This session will cover the various methods you can use to stop tracking a remote branch.
Creating a branch from a tag in Git
Publish Date:2025/04/04 Views:93 Category:Git
-
This article introduces how we can create a new branch based on a tag in Git. If you are a regular Git user, you must know the purpose of Git tags. These tags are just pointers to meaningful Git commits. The question is: how do you create a
Git shows remote tracking branches
Publish Date:2025/04/03 Views:126 Category:Git
-
Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot
View merged and unmerged branches in Git
Publish Date:2025/04/03 Views:97 Category:Git
-
This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig
The difference between Fork and Branch on GitHub
Publish Date:2025/04/03 Views:157 Category:Git
-
This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar
How to determine the current branch in Git
Publish Date:2025/04/03 Views:165 Category:Git
-
Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams
Difference between Git Merge Origin/Master and Git Pull
Publish Date:2025/04/03 Views:196 Category:Git
-
This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha