简体   繁体   English

Git:从分支中删除不需要的未跟踪文件

[英]Git: remove unwanted untracked files from branch

I updated my branch from master and have a few files that were deleted on master, but remain on my local and appear as untracked files. 我从master更新了我的分支,并且有一些在master上删除的文件,但仍保留在我的本地并显示为未跟踪的文件。 I want to remove these files - ie confirm I don't want them on my local anymore. 我想删除这些文件 - 即确认我不再希望它们在我的本地。

I've tried git rm <file> but this does nothing - no message and no changes when I run git status . 我已经尝试了git rm <file>但这没有任何作用 - 当我运行git status时没有消息也没有变化。

I've tried checking out the files from master ( git checkout -- <file> ) but of course I get a message saying the file doesn't exist on master. 我已经尝试从master( git checkout -- <file>git checkout -- <file>但当然我收到一条消息,说主文件上不存在该文件。

I've tried running git clean -n but I get a lot of messages saying Would not remove <file> . 我试过运行git clean -n但是我收到很多消息说Would not remove <file>

I also tried deleting the branch git branch -D <branch-name> and pulling in a fresh version, but still get the same merge conflicts. 我也尝试删除分支git branch -D <branch-name>并引入一个新版本,但仍然得到相同的合并冲突。

Would anyone know what I can do? 有谁知道我能做什么?

TL;DR TL; DR

This question does not seem to make sense. 这个问题似乎没有意义。 Untracked files are already not there, so there is nothing to remove. 未经跟踪的文件已经存在,因此无需删除。 More precisely, an untracked file is a file that is in the work-tree, but not in the index. 更确切地说, 未跟踪文件是工作树中的文件,但不在索引中。 As such, Git just lets it sit there, and mostly ignores it, except for two things: 因此,Git只是让它坐在那里,除了两件事之外, 大多数都忽略了它:

  • Git keeps whining about it being untracked, in git status output for instance. 例如,Git在git status输出中一直抱怨它未被跟踪。
  • Running git add with any option that means "add everything", such as git add . 使用任何意味着“添加所有内容”的选项运行git add ,例如git add . , will add it to the index, so that it is now tracked. ,将它添加索引,以便现在跟踪它。

To stop both of these annoying cases, people list particular untracked file names or name-patterns in .gitignore files. 为了阻止这两种令人烦恼的情况,人们列出.gitignore文件中特定的未跟踪文件名或名称模式。 Note that .gitignore does not cause a file to become untracked; 注意.gitignore不会导致文件变得未跟踪; it just shuts up Git about the file clutttering up the work-tree, and makes sure that "add all" skips that file. 它只是关闭了Git关于工作树的文件,并确保“添加所有”跳过该文件。

Long

An untracked file is a file that is not in the index. 未跟踪文件是不在索引中的文件。 The index is its own independent thing: it's not a branch and it's not a work-tree. 索引是它自己独立的东西:它不是一个分支,它不是一个工作树。 Instead, it's the place where you build the next commit that you intend to make, by staging updated files. 相反,它是您通过暂存更新的文件来构建您打算进行的下一次提交的地方。 This is why it is sometimes called the staging area . 这就是为什么它有时被称为临时区域 It also has the effect of indexing and caching the work-tree, hence its name index , and a third name, the cache . 它还具有索引和缓存工作树的效果,因此它的名称索引和第三个名称, 缓存

Note that the index is a sort of live thing: its contents change over time, especially as you check out particular commits, or go to create new ones. 请注意,索引是一种现实的东西:它的内容随着时间的推移而变化,特别是在您检查特定提交时,或者去创建新的提交。 In any case, though, the index is not part of a branch, so "remove untracked files from a branch" makes no immediate sense. 但是,在任何情况下,索引都不是分支的一部分,因此“从分支中删除未跟踪的文件”没有立即意义。 But we also need to pin down what you mean by "branch" here: see What exactly do we mean by "branch"? 但是,我们也必须去拖住所说的“分支”在这里的意思:看我们究竟由“分支”是什么意思?

Note that a branch name points to one specific commit. 请注意, 分支名称指向一个特定的提交。 If you run git checkout name , you are checking out the specific commit to which the name name points. 如果运行git checkout name ,则表示您正在检查name指向的特定提交。 The act of checking out a commit fills in the index, so now the index contains some set of files, and now we can talk about whether some file is or is not in the index, and hence is or is not untracked. 签出提交的行为填写了索引,所以现在索引包含一些文件集, 现在我们可以讨论某个文件是否在索引中,因此是否未跟踪。 (The word untracked appears more often than tracked , but it seems clear enough that a tracked file is one that is in the index.) 未经跟踪的词出现往往比跟踪 ,但它似乎很清楚,一个跟踪文件是一个在索引中。)

If, after git checkout name , some path P is tracked, running git rm P followed by git commit will make a new commit, and change name so that it identifies this new commit. 如果在git checkout name后跟踪了一些路径P ,则运行git rm P后跟git commit将进行新的提交,并更改name以便识别此新提交。 This new commit will be one that, when checked out, leaves P out of the index, removing it from the index if necessary. 这个新的提交将是一个,当签出时,将P 索引中删除,必要时将其从索引中删除。 So now name identifies a commit that has path P untracked. 所以现在name标识了一个路径为P未跟踪的提交。 If that's what you meant by "branch" above, then this is your answer. 如果你的意思是上面的“分支”,那么这就是你的答案。

On the other hand, if you meant the word "branch" to encompass all commits reachable from the name, or something along those lines, you have a much tougher problem. 另一方面,如果你的意思是“分支”这个词包含所有可以从名称中获得提交 ,或者沿着这些行提供的东西,那么你就会遇到更棘手的问题。 You cannot change any existing commit at all. 您根本无法更改任何现有提交。 You can make a series of new commits, that you consider somehow "better" than the originals: for instance, you can make a series of new commits, none of which have path P in them, and then stop using all the old commits and begin using only these new commits instead. 您可以进行一系列新提交,您认为它们比原始文件“更好”:例如,您可以进行一系列新提交,其中没有一个具有路径P ,然后停止使用所有旧提交和开始只使用这些新提交。 This is what many call rewriting history: changing the set of commits identified by some branch name(s) so that those new-and-improved commits are different from all the original commits. 这就是许多调用重写历史记录的内容:更改由某些分支名称标识的提交集,以便这些新的和改进的提交与所有原始提交不同。 The downside to this kind of history rewriting is that you must get all users of clones of these repositories to switch all their usage to the new commits; 这种历史重写的缺点是你必须让所有这些存储库的克隆用户将他们的所有用法切换到新的提交; otherwise the old commits just keep coming back, like a bad case of some sort of terrible disease. 否则旧的承诺会继续回来,就像某种可怕疾病的坏情况一样。

I've tried running git clean -n but I get a lot of messages saying Would not remove . 我已经尝试过运行git clean -n但是我收到很多消息说不会删除。

-n means dry-run , ie Don't actually remove anything, just show what would be done. -n表示dry-run ,即实际上不删除任何东西,只显示将要执行的操作。 , The message just tell you this fact. ,这条消息只是告诉你这个事实。

If you want to delete untracked file, you should use command git clean -dxf 如果要删除未跟踪的文件,则应使用命令git clean -dxf

Quatation from manpage of git clean git clean manpage的quatation

-d -d

Remove untracked directories in addition to untracked files. 除了未跟踪的文件之外,删除未跟踪的目录。 If an untracked directory is managed by a different git repository, it is not removed by default. 如果未跟踪的目录由不同的git存储库管理,则默认情况下不会删除它。 Use -f option twice if you really want to remove such a directory. 如果您确实要删除此类目录,请使用-f选项两次。

-f, --force -f, - force

If the git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f or -n. 如果git配置变量clean.requireForce未设置为false,则除非给定-f或-n,否则git clean将拒绝运行。

-x -X

Don't use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. 不要使用从.gitignore(每个目录)和$ GIT_DIR / info / exclude读取的标准忽略规则,但仍然使用-e选项给出的忽略规则。 This allows removing all untracked files, including build products. 这允许删除所有未跟踪的文件,包括构建产品。 This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build. 这可以使用(可能与git reset一起使用)来创建一个pristine工作目录来测试一个干净的构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM