The introduction of Git equally a source-code management system in 2005 fundamentally transformed the procedure of software development. Git allows developers to maintain a history of changes, or commits, to their code and revert to previous commits in seconds if something goes wrong. It makes collaboration easier by allowing branching to keep code for unlike features carve up and seamlessly merging commits by various people. It is fast, scalable, and has more than commands and flexibility than older version control tools similar Apache Subversion (SVN) and Concurrent Versions System (CVS).

However, learning Git is more complicated than learning SVN or CVS. Complex commands and a less intuitive user interface can sometimes lead to unwanted states, including a state called detached Head. In this article, we will explore what the Git detached Head state is and some situations that cause it. Then, we will demonstrate how to salvage or discard changes in a discrete head so you can quickly recover from the situation.

What does discrete HEAD mean?

In Git, HEAD refers to the currently checked-out branch's latest commit. However, in a detached HEAD country, the HEAD does non point to whatsoever co-operative, but a specific commit or the remote repository.

Below is a diagram of the Git Caput in a normal country, pointing to the latest commit in the main branch.

Git HEAD pointing to the latest commit in main

In this paradigm, the Caput points to the latest commit and electric current checked-out branch on every commit in the electric current co-operative.

HEAD is also a common state when working with multiple branches.

Git HEAD pointing to the latest commit in feature branch

In this situation, y'all have 2 branches, the main branch and a feature co-operative. Since you are checked out to the feature branch, Head points there. You lot can create this scenario with the following Git commands:

          git co-operative feature git checkout feature git commit -1000 "checked out to feature" git checkout Main git commit git commit -yard "Latest commit" git checkout feature                  

In both of the diagrams higher up, HEAD points to the almost recent commit in the currently checked out co-operative, which is its normal state. In Git, yous can also check out a particular commit, which results in the detached HEAD state. Continuing from the previous scenario, if you check out to the showtime commit on the primary branch, yous will see that Head is now detached.

Git detached HEAD pointing to a previous commit

In this case, HEAD does not point to whatsoever co-operative—it references a commit.

Scenarios that tin cause a detached Head state

You can observe yourself in a detached HEAD state primarily through ii scenarios:

  1. Checking out a specific Secure Hash Algorithm one (SHA-i) commit hash
  2. Checking out to a remote branch without fetching information technology first

We already demonstrated that if you check out the SHA-ane commit hash, you will be in the detached Head state. Another state of affairs that causes a detached HEAD is checking out the remote branch. If y'all cheque out to the origin (main) branch, which is read-simply, y'all will be in the detached Head country.

Some other scenarios can cause a detached Caput as well. For example, checking out to a specific tag proper name or adding ^0 on any given co-operative causes the discrete Head state.

How to save changes in a detached Caput

If you discover yourself a detached HEAD state and realize it quickly, yous can quickly recover past checking out the previous branch. But what if you lot are in the detached Head country by error and then perform commits over commits? If you are committing in the discrete Caput state, does that mean your changes are not saved?

Not at all. Information technology but ways you are non currently attached to any branch, and as a result, your HEAD is detached. If you want to continue the changes y'all made while in the detached HEAD land, yous tin solve this problem with iii elementary steps: creating a new branch, committing the changes, and merging the changes.

Create a new co-operative

To save changes committed in a discrete Caput state, y'all get-go need to create a new branch.

Create and check out to a new branch

Continuing from the scenario described above, y'all create a new branch called temp-branch. Every bit soon as you make the branch and bank check out to information technology, the HEAD is no longer detached.

Commit the changes

After checking out to the new co-operative, you can commit the changes, which Git will preserve.

Commit changes to the new branch

Merge the changes

Now, you check out to the co-operative where you want the changes. In this instance, yous want to merge the changes to the chief branch. So, you showtime need to check out the main branch, then merge the changes from temp-branch and add the concluding commit message.

Merge changes to the main branch

With these uncomplicated steps, you have successfully preserved your changes and recovered from the Git detached HEAD state.

How to discard changes in a detached HEAD

If you desire to discard the changes in the discrete Caput state, you simply need to bank check out to the existing or previous co-operative. The commits on the discrete Caput state will non affect your existing co-operative, and Git will annal them.

The diagram below shows a situation in which, after going into the discrete HEAD state, you lot make two commits that you practice not desire to go along. Then, y'all check out to the chief co-operative. The dotted circles betoken that these commits are no longer role of whatever branch, and Git will delete them.

Discard changes in detached HEAD

Note that once Git prunes your detached HEAD state commits, there is no way to get them back. All the same, if they take not been deleted, yous can check out to that SHA-ane commit hash, create a co-operative, and merge information technology to the desired co-operative to preserve the changes.

Decision

Git is a valuable developer tool and more than popular than older versioning tools such as CVS and Subversion. That said, information technology can be more complex and challenging to master, and tin sometimes atomic number 82 to confusing situations such as the detached HEAD state.

If yous find yourself in the detached Head state, call up that you can always preserve your changes past creating and checking out to a new branch, then committing and merging the changes in the desired branch. If y'all practise non desire to salve the changes, yous can simply bank check out to any branch, and Git removes those commits.

Also, Git ii.23 has a new control, git switch. This is not a new feature but an alternative command to git checkout so you tin switch between the branches and create a new co-operative. To alter from one branch to another, use git switch branchName to create a new branch, then switch to it using the git switch -c branchName command.

Although finding your code in the detached HEAD state is not platonic, you lot tin can utilize these methods to motility or remove your commits and quickly get your projection dorsum on runway.