Git new file appears in all branches

自闭症网瘾萝莉.ら 提交于 2019-12-07 09:21:27

问题


I thought a file created on one branch will not appear in any other branches until I merge or rebase the branch?

Example:

I have two branches:

  1. master
  2. new_contact_page

I checkout the new_contact_page branch.

$ git checkout new_contact_page

Then I create a new file.

$ vi contact_page.html

Without doing any Git commands, I switch back to my Master branch.

$ git checkout master

Then I see that this contact_page.html file is also in my Master branch.

$ ls   (contact_page.html shows up in the list!)

Shouldn't the file only exist in new_contact_page branch?


回答1:


Git will never touch any files that aren't actually in your repository. (untracked files)

You need to git add and git commit the file (into one branch) first.




回答2:


This is where is is important to understand the index (or staging area).
As long as you don't stage (git add) files, they remain "untracked" (or "private"), and won't be modified by a "git checkout".

That is different from "unstaged" which references tracked file (previously committed in the local Git repo) with local modifications not yet added to the index.

You can read more in "You could have invented git (and maybe you already have!)"



来源:https://stackoverflow.com/questions/12341665/git-new-file-appears-in-all-branches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!