git - 来自其他分支的文件在 master 分支中可见

标签 git

我正在学习git。我从 master 分支创建了一个新的 NEW 分支,并在 NEW 中创建了一个新文件。现在,当我切换回 master 时,NEW 中新创建的文件在 master 分支中可见。正常吗?如果是这样,如何 stash master 分支中其他分支的文件。

最佳答案

在您添加它 ( git add <file> ) 并提交它 ( git commit -m 'My new file' ) 之前,您新创建的文件不会被跟踪。这意味着 git 不知道它的索引树中存在它。

所以你所描述的行为是完全可以预料的。这个schema可能会帮助您理解(source)。

尝试以下操作:

git checkout master          # start from the master branch
git checkout -b NEW          # create and switch onto the NEW branch
echo foo > bar               # create a new file
git add bar                  # track the file
git commit -m 'My new file'  # Commit the new file
git checkout master          # The file has disappeared from the working directory

关于git - 来自其他分支的文件在 master 分支中可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21453421/

相关文章:

带有可选参数的 git 别名

java - 使用 Java 将文件推送到现有的 GitLab 存储库?

git - 如果 merge 两个具有相同提交的分支会发生什么?

git - 如何对 git status 输出进​​行排序

linux - 可以让多个 git 用户以 root 身份登录吗?

java - 将 Git 消息保存在 java 变量中

javascript - 是否可以为存储库中的多个项目使用一个 node_module 文件夹?

git - 在 git merge upstream/master 之后使用 Sublime Text 中更新的上游和 stash 的更改解决 Git 冲突

node.js - 勒纳致命 : ambiguous argument 'origin/heads/origin/master...heads/origin/master' : unknown revision or path not in the working tree

GitLab:如何判断我对项目的访问级别?