git 为什么我不能切换分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6002732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can I not switch branches?
提问by lampShade
I'm trying to switch branches in git but I'm getting this error message:
我正在尝试在 git 中切换分支,但收到此错误消息:
error: you need to resolve your current index first
I'm using git under xcode4
我在 xcode4 下使用 git
git status
# On branch DateCode
# Unmerged paths:
# (use "git reset HEAD <file>..." to unstage)
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: Whereami.xcodeproj/project.xcworkspace/xcuserdatauser.xcuserdatad/UserInterfaceState.xcuserstate
#
no changes added to commit (use "git add" and/or "git commit -a")
Frappuccinos-MacBook-Pro:whereami
回答by jitendrapurohit
Try this if you don't want any of the merges listed in git status:
如果您不想在 git status 中列出任何合并,请尝试此操作:
git reset --merge
This resets the index and updates the files in the working tree that are different between <commit>
and HEAD
, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added).
这将重置索引并更新工作树中在<commit>
和之间不同的文件HEAD
,但保留索引和工作树之间不同的文件(即具有尚未添加的更改)。
If a file that is different between <commit>
and the index has unstaged changes -- reset is aborted.
如果<commit>
与索引不同的文件具有未暂存的更改 - 重置将中止。
More about this - https://www.techpurohit.com/list-some-useful-git-commands& Doc link - https://git-scm.com/docs/git-reset
更多相关信息 - https://www.techpurohit.com/list-some-useful-git-commands& Doc link - https://git-scm.com/docs/git-reset
回答by Mark Longair
You end up with both modified
in the output of git status
if there were conflicts produced by a merge. git isn't letting you change branch until you've resolved these conflicts. If you edit that file, you should see some conflict markers in it - there's a guide to resolving those conflicts in the git manual. (Since kernel.org is currently down, you can find that guide hereinstead.)
如果合并产生冲突,您最终会both modified
得到输出git status
。在您解决这些冲突之前,git 不会让您更改分支。如果您编辑该文件,您应该会在其中看到一些冲突标记 - git 手册中有解决这些冲突的指南。(由于 kernel.org 目前已关闭,您可以在此处找到该指南。)
Alternatively, if you think the merge was a mistake, you could undo it with: git reset --merge
或者,如果您认为合并是错误的,您可以使用以下命令撤消它: git reset --merge
回答by jyapx
If you don't care about the changes that git says are outstanding, then you can do a force checkout.
如果您不关心 git 所说的未完成的更改,那么您可以进行强制签出。
git checkout -f {{insert your branch name here}}
git checkout -f {{insert your branch name here}}
回答by jyapx
I got this message when updating new files from remote and index got out of whack. Tried to fix the index, but resolving via Xcode 4.5, GitHub.app (103), and GitX.app (0.7.1) failed. So, I did this:
从远程更新新文件时,我收到此消息,并且索引失灵了。尝试修复索引,但通过 Xcode 4.5、GitHub.app (103) 和 GitX.app (0.7.1) 解决失败。所以,我这样做了:
git commit -a -m "your commit message here"
which worked in bypassing the git index.
它可以绕过 git 索引。
Two blog posts that helped me understand about Git and Xcode are:
帮助我了解 Git 和 Xcode 的两篇博文是:
回答by Deepika Patel
you can reset your branch with HEAD
你可以用 HEAD 重置你的分支
git reset --hard branch_name
then fetch branches and delete branches which are not on remote from local,
然后获取分支并删除不在本地远程的分支,
git fetch -p
回答by Andrea
You need to commit or destroy any unsaved changes before you switch branch.
在切换分支之前,您需要提交或销毁任何未保存的更改。
Git won't let you switch branch if it means unsaved changes would be removed.
如果这意味着将删除未保存的更改,Git 不会让您切换分支。
回答by Koshik Raj
Since the file is modified by both, Either you need to add it by
由于文件被两者都修改过,因此您需要通过以下方式添加它
git add Whereami.xcodeproj/project.xcworkspace/xcuserdatauser.xcuserdatad/UserInterfaceState.xcuserstate
Or if you would like to ignore yoyr changes, then do
或者,如果您想忽略 yoyr 更改,请执行以下操作
git reset HEAD Whereami.xcodeproj/project.xcworkspace/xcuserdatauser.xcuserdatad/UserInterfaceState.xcuserstate
After that just switch your branch.This should do the trick.
之后只需切换您的分支。这应该可以解决问题。