为什么在提交和签出后将文件夹保留在本地git工作目录中 [英] Why are folders left in my local git working directory after commit and checkout

查看:108
本文介绍了为什么在提交和签出后将文件夹保留在本地git工作目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个文件夹,其中包含本地工作git结构中的文件.我使用git checkout -b创建了一个新分支,并使用了git add.和git commit -m"..."将这些文件添加到我的本地分支.但是,当我执行git checkout master时,我创建并提交的文件夹仍然存在.为什么?我以为git commit会将文件夹及其内容放入我的本地分支,在结帐master时将其切换出来.

I have created a folder containing files in my local working git structure. I created a new branch with git checkout -b and used git add . and git commit -m "..." to add those files to my local branch. But, when I do git checkout master the folder I created and committed is still there. Why? I thought git commit would put the folder and its contents into my local branch, switching it out when I checkout master.

推荐答案

如果将以前未跟踪的文件添加到新分支,然后签出当前未跟踪这些文件的另一个分支,则不会从文件中删除它们您的工作副本.

If you add previously untracked files to a new branch, and then you checkout another branch that doesn't currently track those files, it won't remove them from your working copy.

这里是一个例子,假设我目前在一个名为old_branch的干净分支上,并签出一个名为new_branch的新分支:

Here's an example—let's say I'm currently on a clean branch named old_branch, and I checkout a new branch named new_branch:

git checkout -b new_branch

然后,我在此分支中创建一个名为test.txt的新文件,并将其添加到存储库中:

Then, I create a new file named test.txt in this branch and add it to the repo:

touch test.txt  # creates a new file named test.txt
git add test.txt
git commit -m "Added test.txt"

test.txt文件当前由new_branch分支跟踪.但是,当我将分支切换回old_branch时:

The test.txt file is currently tracked by the new_branch branch. However, when I switch branches back to old_branch:

git checkout old_branch

由于test.txt没有被old_branch跟踪,因此将其保留在工作目录中并且不会覆盖它.这是预期的行为.如果此时执行git status,您会注意到test.txt文件当前未跟踪.

Since test.txt is not tracked by old_branch, it leaves it in the working directory and doesn't overwrite it. This is expected behavior. If you do git status at this point, you'll notice that the test.txt file is currently untracked.

为完成起见,如果您需要清除所有未跟踪文件的工作副本,则可以先执行以下操作:

For completion's sake, if you need to clean your working copy of all untracked files, you can first do:

git clean -n

这将列出所有将被删除的未跟踪文件.如果您对列表满意,则可以使用以下方法删除这些文件:

This will list all untracked files that will be removed. If you're satisfied with the list, you can them remove those files with:

git clean -f -d

这是一个破坏性命令,尤其是因为它会删除当前分支未跟踪的文件. (它不会从确实跟踪这些文件的分支中删除它们.)

This is a destructive command, especially since it deletes files that are not tracked by the current branch. (It won't delete them from branches that do track those files though.)

这篇关于为什么在提交和签出后将文件夹保留在本地git工作目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆