Fix a Git detached head?

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

    This approach will potentially discard part of the commit history, but it is easier in case the merge of the old master branch and the current status is tricky, or you simply do not mind losing part of the commit history.

    To simply keep things as currently are, without merging, turning the current detached HEAD into the master branch:

    1. Manually back up the repository, in case things go unexpectedly wrong.
    2. Commit the last changes you would like to keep.
    3. Create a temporary branch (let's name it detached-head) that will contain the files in their current status:
    git checkout -b detached-head
    
    1. (a) Delete the master branch if you do not need to keep it
    git branch -D master
    
    1. (b) OR rename if you want to keep it
    git branch -M master old-master
    
    1. Rename the temporary branch as the new master branch
    git branch -M detached-head master
    

    Credit: adapted from this Medium article by Gary Lai.

提交回复
热议问题