-1

I was working on my project and made a few commits. After that i was stuck in a problem so i did git checkout 2y72aa and made many changes and finally performed git add . and git commit.

But now when i performed git status it shows me HEAD detached from 2y72aa where 2y72aa commit is my older commit and not the latest one.

how do i commit my changes and shift at the latest change ?

Please note that the project is important so i dont want anything to be deleted permanentely.

Raj
  • 1,928
  • 3
  • 29
  • 53
  • 1
    Possible duplicate of [Why did my Git repo enter a detached HEAD state?](https://stackoverflow.com/questions/3965676/why-did-my-git-repo-enter-a-detached-head-state) – phd Dec 30 '17 at 19:51
  • @phd I also wanted a way to commit my changes and come back with latest changes on the master branch which was answered well by TriskalJM – Raj Dec 31 '17 at 05:49

3 Answers3

4

There is no need to worry about data loss because nothing bad has happened - the "detached head" warning is worded to make the situation appear scary so the user is alerted to action.

There is nothing really wrong with the commit you created, and it won't go away any time soon. You can create other commits on top of it. If you write the hash (or its several characters) to e.g. a piece of paper, you will be able to checkout to the commit later. You can switch to a different branch and git cherry-pick the commit by ID. However, the commit is unnamed: no branch (or other named reference such as a tag) leads to the commit. This means that you will not be able to easily retrieve it if you lose the commit ID. It also means that, given enough time - on the order of a month - the commit will be recycled.

To "fix" the problem, simply create a branch from the commit using git checkout -b branch-name. From then on, the commit will be on the branch you created, which you can manage as usual.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • The "danger time" for recycling is / garbage collecting is 30 days by default. Reflog entries that are reachable from the current value of the reference get the longer (default 90 days) expiry, but this particular commit will almost certainly be unreachable-from-`HEAD` if and when the OP moves to another commit. So, better to say "a month" than "months" (but otherwise right and upvoted). – torek Dec 30 '17 at 18:02
  • @torek Updated, thanks. I remember the three months mark, but that obviously applied to the entries reachable through a reflog. – user4815162342 Dec 30 '17 at 18:14
1

HEAD detached simply means that your working copy is pointing to a commit without a branch. This happened because you did a git checkout to a commit hash.

In order to reset the master branch to your current commit, execute the following commands (cribbed from here):

git branch my-temporary-work
git checkout master
git merge my-temporary-work
git checkout master
git merge my-temporary-work

This will get you out of detached HEAD mode, with your changes on master branch.

TriskalJM
  • 2,393
  • 1
  • 19
  • 20
0

When you checked out a commit using its hash (2y72aa) you have moved to detached head mode.

It basically means that there have no branch pointing to this hash.

Git allows you to checkout any commit in history, but in detached head mode you cannot commit new changes.

If you want to continue working on some historic commit you need to create a branch there.

git branch <branch name>

This will allow you to commit new changes.

If you were working your feature branch before, and wanted to go back to 2y72aa, you should reset your branch to this commit, instead of just checking it out.

Igal S.
  • 13,146
  • 5
  • 30
  • 48