如何将新文件夹添加到git分支而不将其添加到master

时间:2021-05-28 16:25:19

How does one add a new folder to a branch in git without having that folder show up in the master branch?

如何在git中向分支添加新文件夹而不在主分支中显示该文件夹?

e.g. 
git branch myNewBranch
git checkout myNewBranch
mkdir foo

git checkout master
ls

the myNewBranch directory is also added to master

myNewBranch目录也被添加到master

2 个解决方案

#1


7  

Just because you created the folder git will not automatically start version controlling it. The folder you created is unknown to git so git will not touch it.

仅仅因为你创建了文件夹git就不会自动启动控制它的版本。您创建的文件夹对于git是未知的,因此git不会触及它。

git init #create repo
touch file1.txt
git add file1.txt
git commit -m 'initial commit'
git branch newBranch
git checkout newBranch
mkdir folder
touch folder/file2.txt
git add folder #tell git we want to track the folder
git commit -m 'second commit'
git checkout master
# folder/file2.txt is not available as expected.

Edit: To clarify the folder in your example was not added to master, it actually was not added to any branch.

编辑:为了澄清您的示例中的文件夹未添加到master,它实际上没有添加到任何分支。

#2


7  

It would seem you have 2 misconceptions that are confusing you.

看起来你有两个令你困惑的错误观念。

  1. Git does not overwrite unstaged changes when you switch branches. You need to add and commit a file before Git will do anything to that file (like modify or remove it when you checkout a different branch).
  2. 切换分支时,Git不会覆盖未分级的更改。您需要在Git对该文件执行任何操作之前添加并提交文件(例如,在签出其他分支时修改或删除它)。
  3. Git does not track directories, it only tracks files. You cannot version an empty directory in Git. If you want to commit an empty directory to Git, the conventional workaround is to add an empty .gitignore in that directory and commit that.
  4. Git不跟踪目录,它只跟踪文件。你不能在Git中对空目录进行版本控制。如果你想将一个空目录提交给Git,传统的解决方法是在该目录中添加一个空的.gitignore并提交它。

#1


7  

Just because you created the folder git will not automatically start version controlling it. The folder you created is unknown to git so git will not touch it.

仅仅因为你创建了文件夹git就不会自动启动控制它的版本。您创建的文件夹对于git是未知的,因此git不会触及它。

git init #create repo
touch file1.txt
git add file1.txt
git commit -m 'initial commit'
git branch newBranch
git checkout newBranch
mkdir folder
touch folder/file2.txt
git add folder #tell git we want to track the folder
git commit -m 'second commit'
git checkout master
# folder/file2.txt is not available as expected.

Edit: To clarify the folder in your example was not added to master, it actually was not added to any branch.

编辑:为了澄清您的示例中的文件夹未添加到master,它实际上没有添加到任何分支。

#2


7  

It would seem you have 2 misconceptions that are confusing you.

看起来你有两个令你困惑的错误观念。

  1. Git does not overwrite unstaged changes when you switch branches. You need to add and commit a file before Git will do anything to that file (like modify or remove it when you checkout a different branch).
  2. 切换分支时,Git不会覆盖未分级的更改。您需要在Git对该文件执行任何操作之前添加并提交文件(例如,在签出其他分支时修改或删除它)。
  3. Git does not track directories, it only tracks files. You cannot version an empty directory in Git. If you want to commit an empty directory to Git, the conventional workaround is to add an empty .gitignore in that directory and commit that.
  4. Git不跟踪目录,它只跟踪文件。你不能在Git中对空目录进行版本控制。如果你想将一个空目录提交给Git,传统的解决方法是在该目录中添加一个空的.gitignore并提交它。