patterngitMinor
What is the exact use of DETACHED HEAD state in Git?
Viewed 0 times
exactthewhatheadstategitusedetached
Problem
When I checkout a specific commit hash, it goes to detached mode where HEAD POINTER can't point to a recent commit.
What are the things we can do and what is the use of going detached head state in Git?
What are the things we can do and what is the use of going detached head state in Git?
Solution
The "normal" way of working with GIT is by checking out branches. HEAD is a "pointer" to the latest commit in that branch, and GIT will allow you to commit new changes after that pointer.
However, as you figured out, it is possible to check out a specific commit hash, if you want to. In this situation, if you make changes and try to commit them, you will be unable to do so, since you are not at HEAD, and GIT will not allow you to commit your changes.
Most of the time, detached HEAD (checking out an older commit) is used if you want to test how your project ran at a certain point in time.
If you need to make changes to the code or need to build it using Jenkins MultiBranch Pipelines, for example, it is also possible to create a new branch from that specific commit by running:
or
... where
However, as you figured out, it is possible to check out a specific commit hash, if you want to. In this situation, if you make changes and try to commit them, you will be unable to do so, since you are not at HEAD, and GIT will not allow you to commit your changes.
Most of the time, detached HEAD (checking out an older commit) is used if you want to test how your project ran at a certain point in time.
If you need to make changes to the code or need to build it using Jenkins MultiBranch Pipelines, for example, it is also possible to create a new branch from that specific commit by running:
git branch my_new_fancy_branch 7y8u33iior
git checkout -b my_new_fancy_branch 7y8u33ii... where
7y8u33ii is the hash you provided in the question. For this new branch, 7y8u33ii will be HEAD, so you will be able to commit new changes. If you need to get your code into the branch from which you created this new branch from, you will need to do a merge, but this is another discussion altogether.Context
StackExchange DevOps Q#2282, answer score: 8
Revisions (0)
No revisions yet.