可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I was doing some work in my repository and noticed a file has local changes. I didn't want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I wanted to do the git equivalent of
svn up .
Using git pull didn't seem to work. Some random searching led me to a site where someone recommended doing
git checkout HEAD^ src/
(src is the directory containing the deleted file).
Now I find out I have a detached head. I have no idea what that is. How can I undo?
回答1:
Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).
You only need to checkout the branch you were on, e.g.
git checkout master
Next time you have changed a file and want to restore it to the state it is in the index, don't delete the file first, just do
git checkout -- path/to/foo
This will restore the file foo to the state it is in the index.
回答2:
If you have changed files you don't want to lose, you can push them. I have committed them in the detached mode and after that you can move to a temporary branch to integrate later in master.
git commit -m "....." git branch my-temporary-work git checkout master git merge my-temporary-work
Extracted from:
What to do with commit made in a detached head
回答3:
How to exit (“fix”) detached HEAD state when you already changed something in this mode and want to save your changes. (That part is optional though.)
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"
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.)
Check out your branch. Exit detached HEAD state by checking out the branch you worked on before, for example:
git checkout master
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 …
回答4:
Detached head means: (1) You are no longer on a branch, (2) You have checked out a single commit in the history
if you have no changes: you can switch to master by applying the following command
git checkout master
if you have changes that you want to keep:
git branch temp git checkout master git merge temp
回答5:
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.
回答6:
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.
回答7:
If you made some changes and then realized that you are on a detached head, there is a simple solution for that: stash -> checkout master -> stash pop:
git stash git checkout master # Fix the detached head state git stash pop # ... or for extra safety use 'stash apply' then later # after fixing everything do 'stash drop'
You will have your uncommited changes and normal "attached" HEAD, like nothing happened.
回答8:
Since "detached head state" has you on a temp branch, just use git checkout - which puts you on the last branch you were on.
回答9:
Addendum
If the branch to which you wish to return was the last checkout that you had made, you can simply use checkout @{-1}. This will take you back to your previous checkout.
Further, you can alias this command with, for example, git global --config alias.prev so that you just need to type git prev to toggle back to the previous checkout.
回答10:
To further clarify @Philippe Gerber's answer, here it is:

Before cherry-pick, a git checkout master is necessary in this case. Furthermore, it is only needed with a commit in detached head.
回答11:
git pull origin master
worked for me. It was just about giving remote and branch name explicitly.
回答12:
In my case, I run git status and I saw that I had a few untracked files on my working directory.
I just had to clean them (since I didn't need them) to run the rebase I wanted to perform.
回答13:
You may need to clean your branch first to checkout master. -f forces the action.
git clean -f
Then you will be able to checkout your mast branch like stated above
git checkout master