无法切换分支 - “错误:未跟踪的工作树文件......”我应该做 git add 吗?

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

Can't switch branches - "Error: Untracked working tree files..." should I do git add?

git

提问by MEM

I have this on my .gitignorefile:

我的.gitignore文件中有这个:

/nbproject/
/app/runtime/
/app/runtime/application.log*
/app/runtime/error.log*
/app/config/localdev.php*
.DS_Store

1) I have checkout to master branchand then, checkout again, back to dev branch.

1)我已经结帐到master 分支,然后再次结帐,回到dev 分支

2) Once I got back to dev branch, I lost all /nbproject/ files and all /app/config/localdev.php as well !!! And perhaps the others, but since they are autogenerated by php framework uppon runtime, I can't really tell!

2) 回到dev 分支后,我丢失了所有 /nbproject/ 文件和所有 /app/config/localdev.php !!!也许还有其他的,但由于它们是在运行时由 php 框架自动生成的,我真的无法分辨!

3) I then switch back to the master branchand I got those files there.

3)然后我切换回主分支,我在那里得到了这些文件。

4) I've copy those files somewhere else, and I've switched to dev branchagain.

4)我已经将这些文件复制到其他地方,然后又切换到了dev 分支

5) I've place those missing files back inside /nbproject/ folder.

5) 我已将那些丢失的文件放回 /nbproject/ 文件夹中。

Now, if I try to switch to the Master branch, I'm getting this:

现在,如果我尝试切换到 Master 分支,我会得到这个:

Error: The following untracked working tree files would be overwritten by checkout:     
    nbproject/private/config.properties      
    nbproject/private/private.properties     
    nbproject/private/private.xml    
    nbproject/project.properties     
    nbproject/project.xml 

    Please move or remove them before you can switch branches.  

    Aborting

I'm aware that I should perhaps do a git add .to move those files on the repository, what I don't understand is:

我知道我也许应该git add .在存储库中移动这些文件,我不明白的是:

Why is git throwing this message IF I had nbproject/ ignored well before those git checkout commands being issued?

如果在发出这些 git checkout 命令之前我已经忽略了 nbproject/ ,为什么 git 会抛出此消息?

Question:

题:

The point is to ignore those files. Both on master and dev. My question is: how can I fix this in order to:

关键是忽略这些文件。在 master 和 dev 上。我的问题是:我该如何解决这个问题,以便:

a) First: get those files (the ones on gitignore) back to dev ?

a) 首先: 将那些文件(gitignore 上的文件)返回给 dev ?

b) Second: make steps to avoid this conflict again.

b) 第二: 采取措施再次避免这种冲突。

采纳答案by MEM

Steps taken to solve the problem. Credits go to: jszakmeister

为解决问题而采取的步骤。学分转到:jszakmeister

1)I have cloned the project to somewhere else.

1)我已将项目克隆到其他地方。

2)I've checkout master on that clone.

2)我在那个克隆上有结帐大师。

3)Placed the gitignore into the master as it should have been there from the beginning: git show dev:.gitignore > .gitignore

3)将 gitignore 放入 master 中,因为它应该从一开始就在那里: git show dev:.gitignore > .gitignore

4)I've removed the files from the tree by doing git rm "all files that where on gitignore one by one"

4)我通过执行 git rm “所有文件在 gitignore 上一一删除”

5)I've added the .gitignore file to the repo.

5)我已将 .gitignore 文件添加到 repo 中。

6)commit it

6)提交

7)switch back to the original repo and did:

7)切换回原始回购并执行:

8)git fetch /path/to/fix-project master:master

8)git fetch /path/to/fix-project master:master

9)git checkout master, and I got: Switched to branch 'master' Your branch is ahead of 'hub/master' by 1 commit. this is my remote master branch.

9)git checkout master,我得到:切换到分支“master”你的分支领先于“hub/master”1次提交。这是我的远程主分支。

10)Pushed to remote master.

10)推送到远程主站。

11)checkout dev again

11)再次结帐开发

回答by Adam Dymitruk

You can track them in the other branch. Or, you can add --forceto the checkout command to zap the files with what ever master has. Or, you can git clean -xdfbefore checking out master to zap the files from the working dir.

您可以在另一个分支中跟踪它们。或者,您可以添加--force到 checkout 命令中,以使用 master 拥有的内容来清除文件。或者,您可以git clean -xdf在检出 master 之前从工作目录中删除文件。