git 分支中的修改文件溢出到另一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246275/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:05:42  来源:igfitidea点击:

Modified files in a git branch are spilling over into another branch

gitgit-branch

提问by Rajkumar S

I am working on a git repository with a master branch and another topic branch. I have switched to topic branch and modified a file. Now, if I switched to master branch that same file is shown as modified.

我正在使用一个主分支和另一个主题分支的 git 存储库。我已经切换到主题分支并修改了一个文件。现在,如果我切换到 master 分支,则同一文件显示为已修改。

For example:

例如:

git status in git-build branch:

git-build 分支中的 git 状态:

# On branch git-build
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   cvsup_current
#

Switch to master branch

切换到主分支

[root@redbull builder_scripts (git-build)]# git co master
M       builder_scripts/cvsup_current
Switched to branch "master"

git status in master branch

主分支中的git状态

[root@redbull builder_scripts (master)]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   cvsup_current
#

Why is that the file is shown as modified in master branch even though it was modified in git-build branch?

为什么文件在 master 分支中显示为已修改,即使它在 git-build 分支中被修改?

My understanding was that the branches are independent of each other and when I change from one branch to another the changes do not "spill over" from one branch to another. So I am obviously missing some thing here.

我的理解是分支彼此独立,当我从一个分支更改为另一个分支时,更改不会从一个分支“溢出”到另一个分支。所以我显然在这里遗漏了一些东西。

Anyone got a clue stick?

有大佬有线索吗?

采纳答案by Marko

This is the default behaviour of git.

这是 git 的默认行为。

You can use -fflag to checkout to do "clean checkout" if you like.

如果您愿意,您可以使用-f标志来结帐以进行“干净结帐”。

回答by Gareth

Why is that the file is shown as modified in master branch even though it was modified in git-build branch?

为什么文件在 master 分支中显示为已修改,即使它在 git-build 分支中被修改?

The key to remember is that the file was notmodified in the git-build branch. It was only modified in your working copy.

要记住的关键是该文件没有在 git-build 分支中被修改。它仅在您的工作副本中进行了修改。

Only when you commit are the changes put back into whichever branch you have checked out

只有当您提交时,更改才会放回您已签出的任何分支

回答by Peter Burns

If you want to temporarily store your changes to one branch while you go off to do work on another, you can use the git stashcommand. It's one of the amazing little unsung perks of using git. Example workflow:

如果您想在去另一个分支工作时暂时将更改存储到一个分支,您可以使用该git stash命令。这是使用 git 的惊人小无名特权之一。示例工作流程:

git stash #work saved
git checkout master
#edit files
git commit
git checkout git-build
git stash apply #restore earlier work

git stashstores a stack of changes, so you can safely store multiple checkpoints. You can also give them names/descriptions. Full usage info here.

git stash存储一堆更改,因此您可以安全地存储多个检查点。您还可以为他们提供名称/描述。完整的使用信息在这里

回答by Greg Hewgill

The modified files are not put in the repository until you add andcommit them. If you switch back to your topic branch and commit the file, then it won't appear on the master branch.

修改后的文件不会放入存储库,直到您添加提交它们。如果您切换回主题分支并提交文件,则它不会出现在主分支上。

回答by Raviteja

  • It is not like git branches are dependent on each other but also they do not have a complete code base separately for each branch either.
  • For each commit, Git stores an object that contains a pointer to the changes. So each branch points to its own latest commit and HEAD points to the branch currently you are in.
  • When you switch the branch, the HEAD pointer points to that particular commit of the branch. So if there are modified files, the default behavior is to copy over them.
  • 它不像 git 分支彼此依赖,而且它们也没有为每个分支单独提供完整的代码库。
  • 对于每次提交,Git 存储一个包含指向更改的指针的对象。所以每个分支都指向它自己的最新提交,而 HEAD 指向你当前所在的分支。
  • 当您切换分支时, HEAD 指针指向该分支的特定提交。所以如果有修改过的文件,默认行为是复制它们。

You can do the following things to overcome this issue.

您可以执行以下操作来解决此问题。

  1. Use -foption to ignore the changes.
  1. 使用-f选项忽略更改。

If you want to save the changes:

如果要保存更改:

  1. Commit the changes locally in the same branch and then switch the branch.
  2. Use git stash, switch the branch, do your work, switch back to the original branch and do git stash apply.
  1. 在同一分支中本地提交更改,然后切换分支。
  2. 使用git stash,切换分支,做你的工作,切换回原来的分支然后做git stash apply