Fix a Git detached head?

前端 未结 23 1396
臣服心动
臣服心动 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 <hash1> <hash2> <hash3> …
      
    0 讨论(0)
  • 2020-11-22 06:27

    When you check out a specific commit in git, you end up in a detached head state...that is, your working copy no longer reflects the state of a named reference (like "master"). This is useful for examining the past state of the repository, but not what you want if you're actually trying to revert changes.

    If you have made changes to a particular file and you simply want to discard them, you can use the checkout command like this:

    git checkout myfile
    

    This will discard any uncommitted changes and revert the file to whatever state it has in the head of your current branch. If you want to discard changes that you have already committed, you may want to use the reset command. For example, this will reset the repository to the state of the previous commit, discarding any subsequent changes:

    git reset --hard HEAD^
    

    However, if you are sharing the repository with other people, a git reset can be disruptive (because it erases a portion of the repository history). If you have already shared changes with other people, you generally want to look at git revert instead, which generates an "anticommit" -- that is, it creates a new commit that "undoes" the changes in question.

    The Git Book has more details.

    0 讨论(0)
  • 2020-11-22 06:30

    When you're in a detached head situation and created new files, first make sure that these new files are added to the index, for example with:

    git add .
    

    But if you've only changed or deleted existing files, you can add (-a) and commit with a message (-m) at the the same time via:

    git commit -a -m "my adjustment message"
    

    Then you can simply create a new branch with your current state with:

    git checkout -b new_branch_name
    

    You'll have a new branch and all your adjustments will be there in that new branch. You can then continue to push to the remote and/or checkout/pull/merge as you please.

    0 讨论(0)
  • 2020-11-22 06:36

    Since "detached head state" has you on a temp branch, just use git checkout - which puts you on the last branch you were on.

    0 讨论(0)
  • 2020-11-22 06:37

    Git told me how to do it.

    if you typed:

    git checkout <some-commit_number>
    

    Save the status

    git add .
    git commit -m "some message"
    

    Then:

     git push origin HEAD:<name-of-remote-branch>
    
    0 讨论(0)
  • 2020-11-22 06:37

    The detached HEAD means that you are currently not on any branch. If you want to KEEP your current changes and simply create a new branch, this is what you do:

    git commit -m "your commit message"
    git checkout -b new_branch
    

    Afterwards, you potentially want to merge this new branch with other branches. Always helpful is the git "a dog" command:

    git log --all --decorate --oneline --graph
    
    0 讨论(0)
提交回复
热议问题