Fix a Git detached head?

前端 未结 23 1755
臣服心动
臣服心动 2020-11-22 05:42

I was doing some work in my repository and noticed a file had local changes. I didn\'t want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I

23条回答
  •  借酒劲吻你
    2020-11-22 06:27

    A solution without creating a temporary branch.

    How to exit (“fix”) detached HEAD state when you already changed something in this mode and, optionally, want to save your changes:

    1. Commit changes you want to keep. If you want to take over any of the changes you made in detached HEAD state, commit them. Like:

      git commit -a -m "your commit message"
      
    2. Discard changes you do not want to keep. The hard reset will discard any uncommitted changes that you made in detached HEAD state:

      git reset --hard
      

      (Without this, step 3 would fail, complaining about modified uncommitted files in the detached HEAD.)

    3. Check out your branch. Exit detached HEAD state by checking out the branch you worked on before, for example:

      git checkout master
      
    4. Take over your commits. You can now take over the commits you made in detached HEAD state by cherry-picking, as shown in my answer to another question.

      git reflog
      git cherry-pick   

提交回复
热议问题