Fix a Git detached head?

前端 未结 23 1703
臣服心动
臣服心动 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:40

    Here's what I just did after I realized I was on a detached head and had already made some changes.

    I committed the changes.

    $ git commit -m "..."
    [detached HEAD 1fe56ad] ...
    

    I remembered the hash (1fe56ad) of the commit. Then I checked out the branch I should have been on.

    $ git checkout master
    Switched to branch 'master'
    

    Finally I applied the changes of the commit to the branch.

    $ git cherry-pick 1fe56ad
    [master 0b05f1e] ...
    

    I think this is a bit easier than creating a temporary branch.

提交回复
热议问题