Git 允许我在不提交更改的情况下切换分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16113240/
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
Git allowing me to switch branches without committing changes
提问by burgerB
I am just learning Git, going through a tutorial. I am in branch seo_title and I have uncommitted changes to file mission.html. I did git checkout master
expecting to get the warning about Changes not staged for commit, no changes added, etc, however instead it went ahead and switched branches with the message:
我正在学习 Git,正在阅读教程。我在分支 seo_title 中,我对文件mission.html 进行了未提交的更改。我确实git checkout master
希望收到有关未为提交暂存更改、未添加更改等的警告,但是它继续使用消息切换分支:
M mission.html
Switched to branch 'master'
Then when I did git diff mission.html
it showed me that the working directory still contains the changes I made while I had the other branch checked out.
What am I missing?
For what it's worth, I am using Git Bash on Windows.
然后当我这样做时,git diff mission.html
它告诉我工作目录仍然包含我在检查另一个分支时所做的更改。我错过了什么?对于它的价值,我在 Windows 上使用 Git Bash。
EDIT: the changes to mission.html have not been added to staging index either.
编辑: 对mission.html 的更改也未添加到暂存索引中。
EDIT 2: I thought the top voted answer was correct, but upon further investigation it doesn't match the behavior I am seeing. Here is a fuller description of what I am doing:
编辑 2:我认为投票最高的答案是正确的,但经过进一步调查,它与我所看到的行为不符。这是对我正在做的事情的更完整描述:
top_directory(master) > git branch new_branch_1
top_directory(master) > git branch new_branch_2
top_directory(master) > git checkout new_branch_1
(open notepad++ and modify resources.html, save)
(打开notepad++,修改resources.html,保存)
top_directory(master) > git status
# On branch new_branch_1
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed
# (use "git checkout -- <file>..." to discard changes in wo
#
# modified: resources.html
#
no changes added to commit (use "git add" and/or "git commit
top_directory(new_branch_1) > git checkout new_branch_2
This is where I expect git to object and tell me to stash or commit since new_branch_1 and new_branch_2 have different versions of resources.html, but it just switches to the new branch without a warning and it brings the uncommitted changes along:
这是我希望 git 反对并告诉我存储或提交的地方,因为 new_branch_1 和 new_branch_2 具有不同版本的 resources.html,但它只是在没有警告的情况下切换到新分支,并带来了未提交的更改:
M resources.html
Switched to branch 'new_branch_2'
top_directory(new_branch_2) > git status
# On branch new_branch_2
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: resources.html
#
no changes added to commit (use "git add" and/or "git commit -a")
Is there a mode or a setting that would make it behave this way instead of warning? Or am I still misunderstanding the scenario?
是否有一种模式或设置可以使其行为而不是警告?还是我仍然误解了场景?
EDIT 3: I get it now. The top rated answer was right, see my last comment on that answer.
编辑 3:我现在明白了。评分最高的答案是正确的,请参阅我对该答案的最后评论。
回答by Nabeelah Ali
The different behaviour you saw from the last time you tried to switch branches with local changes and now is due to different file changes.
您上次尝试使用本地更改切换分支时看到的不同行为,现在是由于不同的文件更改造成的。
So, let's say we have a branch called 'readme' where you have committed some changes to a file, let's say README.md.
因此,假设我们有一个名为“readme”的分支,您在其中提交了对文件的一些更改,例如 README.md。
Now, you have switched back to master. You do some work on other files (not README.md). Now you have local changes. If you try to switch back to the 'readme' branch without committing your changes, it will let you. Why? Because switching to the 'readme' branch won't override any of your local changes.
现在,您已切换回 master。您对其他文件(不是 README.md)做了一些工作。现在您有本地更改。如果您尝试在不提交更改的情况下切换回“自述”分支,它将允许您。为什么?因为切换到“自述”分支不会覆盖您的任何本地更改。
If, however you make a modification to the README.md file on the master branch, then when you try to do a
但是,如果您对 master 分支上的 README.md 文件进行了修改,那么当您尝试执行
git checkout readme
you will encounter
你会遇到
error: Your local changes to the following files would be overwritten by checkout: README.md
Please, commit your changes or stash them before you can switch branches.
because you have changes to README.md that would require a merge.
因为您对 README.md 进行了需要合并的更改。
回答by GoZoner
This is the normal behavior. If you don't want the modified files on your newly checked out branch, then stash them. Like this.
这是正常行为。如果您不希望新签出的分支上有修改过的文件,则将它们藏起来。像这样。
# on branch dev
$ git stash
$ git checkout master
# do stuff on master
# back to dev
$ git checkout dev
$ git stash pop
回答by Tuxdude
Git will let you checkout other branches (or tags or SHA1 hashes) as long as the commit you're changing your work-tree to does NOTclobber your local uncommitted changes.
只要您将工作树更改为的提交不会破坏本地未提交的更改,Git 就会让您检出其他分支(或标签或 SHA1 哈希)。
In your case the branch master
's work-tree would have had the same version of missing.html
as that currently exists in the tip of your current branch you were switching from. Git does not need to touch the working copy (for missing.html
at least) when changing branches here, and hence lets you keep your local modifications.
在您的情况下, branchmaster
的工作树将具有与missing.html
您正在切换的当前分支尖端中当前存在的版本相同的版本。missing.html
在此处更改分支时,Git 不需要(至少)触及工作副本,因此可以保留本地修改。
If you indeed tried to checkout
a commit where there was a different version of missing.html
in the work-tree (than the one committed in your current branch), git would show an error message similar to this:
如果您确实尝试在工作树checkout
中有不同版本的提交missing.html
(与当前分支中提交的版本不同),则 git 将显示类似于以下内容的错误消息:
$ git checkout some-other-branch
error: Your local changes to the following files would be overwritten by checkout:
missing.html
Please, commit your changes or stash them before you can switch branches.
Aborting
回答by kan
Git - the stupid content tracker. It just operates with working tree snapshots. The main requirement - you should be able to reconstruct working tree. It does update of course. But if the modified file is the same in both branches - why not to allow switch? If you done it by mistake, you could switch back reconstructing previous state, no big deal. But if the file is differ in the branches, you need merge content, that's why it insists to commit changes, otherwise it will be impossible to go back.
Git - 愚蠢的内容跟踪器。它只与工作树快照一起运行。主要要求 - 您应该能够重建工作树。它当然会更新。但是如果修改后的文件在两个分支中是相同的——为什么不允许切换?如果你做错了,你可以切换回来重建以前的状态,没什么大不了的。但是如果文件在分支上不同,则需要合并内容,这就是为什么它坚持提交更改的原因,否则将无法返回。
回答by Eugene z. Von
this is obvious, if you change a file which is not changed in the branch you want to checkout into, because the syntactic of checkout is to bring all the index and working directory to your new branch, you will have them appeared in your new branch. While if there r conflicts, chances r u need to merge those files, which means once after merge, if you checkout back to your old branch then it is already the merged version not your previous one, you lose it. As a result, to make sure you keep the old change it won't let you do that.
这是显而易见的,如果你更改了一个在你想要结帐的分支中没有改变的文件,因为结帐的语法是将所有索引和工作目录带到你的新分支,你会让它们出现在你的新分支中. 如果存在冲突,则可能需要合并这些文件,这意味着合并后一次,如果您检出回旧分支,那么它已经是合并版本而不是您的前一个版本,您将丢失它。因此,为了确保您保留旧的更改,它不会让您这样做。