VS 中的 TFS/GIT 无法切换到 master,因为有未提交的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26885057/
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
TFS/GIT in VS Cannot switch to master because there are uncommitted changes
提问by rwkiii
I've setup a GIT repository for a VS 2013 solution on visualstudio.com. The repository worked great while I was becoming accustomed to using TFS (in GIT mode).
我已经在visualstudio.com 上为VS 2013 解决方案设置了一个GIT 存储库。当我习惯使用 TFS(在 GIT 模式下)时,存储库运行良好。
Then I decided to familiarize myself with Branches, so I created a branch off of the master. I've made quite a few changes in this branch. I've committed the changes over time and have performed a Sync to push my local commits to the visualstudio.com repository. This all works.
然后我决定让自己熟悉 Branches,所以我从 master 上创建了一个分支。我在这个分支中做了很多改变。我已经随着时间的推移提交了更改并执行了同步以将我的本地提交推送到 visualstudio.com 存储库。这一切都有效。
The problem I am having is that I somehow lost the ability of switching back to the master branch. I cannot merge my newly created branch into the master. Every time I try to do anything that involves the master I get the following error in VS:
我遇到的问题是我不知何故失去了切换回主分支的能力。我无法将新创建的分支合并到主分支。每次我尝试做任何涉及 master 的事情时,我都会在 VS 中收到以下错误:
Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details.
无法切换到 master,因为有未提交的更改。在切换分支之前提交或撤消您的更改。有关详细信息,请参阅输出窗口。
The Output window never contains any 'details'...
输出窗口从不包含任何“详细信息”...
What "uncommitted changes" does this message refer to? Since I cannot get into the master branch I have no way of committing any of its changes (nor am I sure that I want to?). And the current (only other) branch I am in has been committed and sync'ed.
此消息指的是什么“未提交的更改”?由于我无法进入主分支,我无法提交任何更改(我也不确定我想要?)。并且我所在的当前(仅其他)分支已提交并同步。
I'm really just learning TFS, GIT and source control. How do I safely recover from this problem?
我真的只是在学习 TFS、GIT 和源代码控制。我如何安全地从这个问题中恢复?
回答by Kushan
Okay, I had the same issue myself as rwkiii - this is a bug in Visual studio and is not an easy one to fix. Here are the symptoms:
好的,我自己也遇到了与 rwkiii 相同的问题——这是 Visual Studio 中的一个错误,不容易修复。以下是症状:
Visual Studio states that you cannot merge/switch braches/etc. because you have uncommitted changes - which is fine, except all your changes havebeen committed.
Visual Studio 声明您不能合并/切换分支/等。因为您有未提交的更改 - 这很好,除非您的所有更改都已提交。
Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details.
无法切换到 master,因为有未提交的更改。在切换分支之前提交或撤消您的更改。有关详细信息,请参阅输出窗口。
Here'sa screenshot to be clearer.
这是一个屏幕截图,更清楚。
If you look at the output window, it may appear empty but you have to change "Show output from" to "Source Control - Git". This will list the file that Visual Studio thinkshas changes.
如果您查看输出窗口,它可能会显示为空,但您必须将“显示输出”更改为“源代码控制 - Git”。这将列出 Visual Studio认为有更改的文件。
Now, things that I tried that didn'tfix it (so you don't have to):
现在,我尝试过的事情并没有解决它(所以你不必):
- Restarting Visual Studio
- Restarting the machine
- Switching Projects
- Using external git tools (All of which said "no changes")
- git reset
- Making arbitrary changes to the file and committing that. Commits ok, VS still thinks there's more changes
- 重新启动 Visual Studio
- 重新启动机器
- 切换项目
- 使用外部 git 工具(所有这些都说“没有变化”)
- 重置
- 对文件进行任意更改并提交。提交确定,VS 仍然认为还有更多变化
In the end, I had to go and deletethe file from the disk, then "undo" that change from the Team Explorer window:
最后,我不得不从磁盘中删除该文件,然后从“团队资源管理器”窗口“撤消”该更改:
Not the most ideal solution, but it does seem to fix it.
不是最理想的解决方案,但它似乎确实解决了这个问题。
回答by Baron
I used the git command prompt to resolve the issue. I can't guarantee that it's the best way or the only way, but it worked for me in Visual Studio 2013.
我使用 git 命令提示符来解决这个问题。我不能保证这是最好的方法或唯一的方法,但它在 Visual Studio 2013 中对我有用。
Under Changes in the Team Explorer, choose Open Command Prompt.
在 Team Explorer 中的 Changes 下,选择Open Command Prompt。
In the command prompt type:
在命令提示符中键入:
git status
状态
It will list the files that are open for change.
它将列出打开以进行更改的文件。
Copy off those files (just in case), then you can then discard the changes in the working directory in the command prompt.
复制这些文件(以防万一),然后您可以在命令提示符下放弃工作目录中的更改。
git checkout -- mysubdir\myfile.ext
git checkout -- mysubdir\myfile.ext
Don't attempt to change branches in Visual Studio, it still won't work!
不要尝试在 Visual Studio 中更改分支,它仍然不起作用!
Instead at the command prompt switch to the branch in question.
而是在命令提示符下切换到有问题的分支。
git checkout branchname
git checkout 分支名
When you go back to Visual Studio it will prompt you to reload the project. After that the branch will be updated correctly to the branch you chose from the command line.
当您返回 Visual Studio 时,它会提示您重新加载项目。之后,分支将正确更新到您从命令行中选择的分支。
hth
第
回答by javovo
I resolved a similar issue without having to delete a file or make new changes.
我解决了一个类似的问题,而无需删除文件或进行新的更改。
After committing changes on BranchA, merging with Master and pushing via Git bash, the next time I opened the project in VS and looked at Team Explorer, it showed that there were changes on the Master branch. No changes showed up when checking status via Git bash, but 10 files were listed in VS Output. Diffs on all files showed no changes. I tried a commit amend at this point, but it wouldn't let me amend the previous commit (see more about this below, since ultimately this will fix my issue).
在BranchA上提交修改后,与Master合并并通过Git bash推送,下次我在VS中打开项目,查看Team Explorer,发现Master分支有变化。通过 Git bash 检查状态时没有显示任何更改,但 VS 输出中列出了 10 个文件。所有文件的差异显示没有变化。我在这一点上尝试了提交修改,但它不会让我修改之前的提交(请参阅下面的更多信息,因为最终这将解决我的问题)。
Tried to switch to BranchA in VS and got the message that I can't switch due to uncommitted changes. Switch to BranchA via Git bash, and status shows no changes. Refresh VS Team Explorer so it is now on BranchA, shows 14 file changes, even more than Master, but again, diffs on all files showed no changes.
尝试在 VS 中切换到 BranchA 并收到由于未提交的更改而无法切换的消息。通过 Git bash 切换到 BranchA,状态显示没有变化。刷新 VS 团队资源管理器,使其现在位于 BranchA,显示 14 个文件更改,甚至比 Master 还要多,但同样,所有文件的差异均未显示任何更改。
While on BranchA in Team Explorer, I clicked on Actions->Amend Previous Commit. This time it let it happen and all the changes were cleared. It then showed a pending commit, but checking status on Git bash showed nothing pending, so going to Home in Team Explorer and coming back, cleared the pending commit message.
在 Team Explorer 中的 BranchA 上,我单击了操作->修改先前提交。这一次它让它发生了,所有的变化都被清除了。然后它显示了一个挂起的提交,但是在 Git bash 上检查状态没有显示任何挂起的东西,所以在 Team Explorer 中转到 Home 并回来,清除了挂起的提交消息。
Switched to Master in Team Explorer, no more changes showing there either. All is well.
在 Team Explorer 中切换到 Master,那里也没有显示更多更改。一切都很好。
回答by Asser
Uncommited changes means that you have modified one or more files which have already been added to gitand those changes have not been "saved" in a commit or in your git stash.
未提交的更改意味着您修改了一个或多个已添加到 git 的文件,并且这些更改尚未在提交或 git stash 中“保存”。
When you switch between branches you are not allowed to have uncommited changes (new files that have not been added to git are fine though) as changes in other branches might overwrite your uncommited changes.
当您在分支之间切换时,您不允许有未提交的更改(但尚未添加到 git 的新文件也可以),因为其他分支中的更改可能会覆盖您未提交的更改。
I'm unfamiliar with how Visual Studio integrates with git as I myself use PoshGitwith Powershell
我不熟悉 Visual Studio 如何与 git 集成,因为我自己使用PoshGit和 Powershell
The way to proceed is to:
继续的方法是:
- Undo your changes
- Commit your changes
- Or stash your changes(see the Interrupted workflow section).
- 撤消您的更改
- 提交您的更改
- 或者隐藏您的更改(请参阅中断的工作流程部分)。
回答by Jen
Hi this did the trick for me...
嗨,这对我有用...
Try using the command prompt Team Explorer > Unsynced commit > Actions > Open Command Prompt
尝试使用命令提示符 Team Explorer > Unsynced commit > Actions > Open Command Prompt
Do a git checkout branchName
做一个git checkout branchName
回答by Satendra Jindal
1: Open git bash
1:打开git bash
2: Point to your git repository
2:指向你的git仓库
3: check you branch status by using command "git status
"
3:使用命令“ git status
”检查你的分支状态
if you have any re usability of your changes for other branch then use "git stash save
",
(this will save your changes locally,you may face some conflict error, you can ignore
for now) if you see any un-merged file in git status result and you want to keep the changes use "git add <file path>
"
如果您对其他分支的更改有任何可重用性,则使用“ git stash save
”,(这将在本地保存您的更改,您可能会遇到一些冲突错误,您现在可以忽略)如果您在 git status 结果中看到任何未合并的文件并且您想保留更改,请使用“ git add <file path>
”
4: once your local changes are saved use command "git reset --merge
"
4:保存本地更改后,使用命令“ git reset --merge
”
this will reset your local branch to the original checkout, now you are ready to switch
the branch you may see the conflict files in visual studio Team Explorer>changes,
if you don't want to keep that file changes right click and select undo changes or use
"git add <file path >
" to add this file with other saved files.
这会将您的本地分支重置为原始结帐,现在您已准备好切换分支,您可能会在 Visual Studio Team Explorer>changes 中看到冲突文件,如果您不想保留该文件更改,请右键单击并选择撤消更改或使用“ git add <file path >
”将此文件与其他保存的文件一起添加。
5: Now your git branch is ready for switch, type "git checkout <branch path>
"
5:现在你的git分支可以切换了,输入“ git checkout <branch path>
”
6: to pop saved changes use "git stash pop
", this will merge the saved changes in you current branch.
6:使用“ git stash pop
”弹出保存的更改,这将合并您当前分支中保存的更改。
回答by WiseGuy
I've noticed a few times sln items are marked as changed but don't appear that way in Solution Explorer. What I have done as others suggested is in command prompt,
我注意到有几次 sln 项目被标记为已更改,但在解决方案资源管理器中却没有这样显示。我按照其他人的建议所做的是在命令提示符下,
git status (this should show you culprit files)
then add the individual files. Or what I personally do is run:
然后添加单个文件。或者我个人做的是运行:
git add -A
which will add all changed files to view nicely in Team Explorer, where you can undo/delete as you want. Haven't had a problem with this approach.
这将添加所有更改的文件,以便在团队资源管理器中很好地查看,您可以在其中根据需要撤消/删除。这种方法没有问题。
回答by Dib
We regularly run into this issue, because we have a particular file which must always be excluded from changes, but sometime gets changed.
我们经常遇到这个问题,因为我们有一个特定的文件,它必须始终从更改中排除,但有时会被更改。
What works for us when the error message appears is:
出现错误消息时对我们有用的是:
- In VS look in the Outputwindow to see which file is causing the issue.
- In Git bash run
git?update-index?--no-assume-unchanged?TheNameOfTheFileCausingTheIssue
- In VS Undo Checkouton this file
- In VS change the branch in Manage Branches.
- 在 VS 中查看输出窗口以查看导致问题的文件。
- 在 Git bash 中运行
git?update-index?--no-assume-unchanged?TheNameOfTheFileCausingTheIssue
- 在此文件上的VS Undo Checkout 中
- 在 VS 中更改Manage Branches 中的分支。
This may or may not work for your situation, but I have added it in case it puts someone in the right direction. Please be sure you understand the git command before running it!
这可能适用于您的情况,也可能不起作用,但我已经添加了它,以防它使某人朝着正确的方向前进。请确保您在运行之前理解 git 命令!
回答by rbansal
One thing that worked for me is :-
1) Right click on branch you want to checkout.
2) Click on the Reset option.
3) Click on the Delete Changes (--hard)
4) Right click on the branch and select checkout
对我有用的一件事是:-
1)右键单击要结帐的分支。
2) 点击重置选项。
3) 单击删除更改 (--hard)
4) 右键单击分支并选择 checkout
回答by tomRedox
I had this issue after changing a filename from starting with a lower case letter to an upper case letter after I'd already committed the file. I fixed it by changing the name of the file (I just added a '1' on the end) and then committing that change. I was then able to switch branches.
在我已经提交文件后,将文件名从小写字母开头更改为大写字母后,我遇到了这个问题。我通过更改文件名来修复它(我只是在末尾添加了一个“1”),然后提交该更改。然后我就可以切换分支了。
As mentioned above, I had to change the 'Show output from:' combo to 'Source Control - Git' in the Output window in order to see the name of the offending file, that then showed me that the file name VS/git was expecting wasn't capitalised, where as the name in the solution explorer/on the file system was capitalised.
如上所述,我必须在输出窗口中将“Show output from:”组合更改为“Source Control - Git”,以便查看违规文件的名称,然后向我显示文件名 VS/git 是期望没有大写,因为解决方案资源管理器/文件系统上的名称大写。