JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Reattaching HEAD in Git

Author:JIYIK Last Updated:2025/04/04 Views:

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.

commitProbably the state of the source code at a point in time. CommitsIncludes parents and children, complete with data about who created the commit and when. 提交Placed in the repository as an object in a branch.

HEADThe reference points to the latest commit in the current branch. HEADThe 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 HEADthe 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 HEADthe pointer and index pointer 分离 HEADto 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 HEADpermanently deleting the commit from that branch. However, this feature is only HEADavailable 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 HEADand 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, HEADdetach from the current branch. At the same time, we can use git checkoutthe command to change branches without updating the working directory to HEAD, so HEADdetach 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 HEADdetached, 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.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial