GIT:检查替代分支时,我想清除忽略的文件

发布于 2024-10-13 05:52:51 字数 767 浏览 7 评论 0原文

假设我在 master 中有一个结构。

d - src 
      - main ...
      - resources ...
  - target
      - xyz

文件 xyz 不应该被跟踪,所以我添加到 .gitignore target/* 并提交结构

  • git commit -m '初始结构'

我创建 2 个分支

  • gitbranch t1
  • gitbranch t2

并切换到 t1

  • git checkout t1

我开始做一些工作,编译东西,这样目标就被填充了。 我将更改提交给 t1。

  • git commit -a 't1 具体更改'

然后我切换到 t2

  • git checkout t2

当我查看目标时,它仍然填充有创建的文件 当分支 t1 处于活动状态时,虽然我希望将这些“清除”,但

我发现了一个类似的讨论,指出 git 的行为应该与我看到的不同。 当我切换分支时,Git 正在删除忽略的文件

我缺少什么?

弗朗西斯

Assume I have a structure in master.

d - src 
      - main ...
      - resources ...
  - target
      - xyz

Files xyz should not be tracked, so I added into .gitignore target/*
and commit the structure

  • git commit -m 'initial structure'

I create 2 branches

  • git branch t1
  • git branch t2

and switch to t1

  • git checkout t1

I start doing some work, compile stuff, such that target is populated.
I commit my changes to t1.

  • git commit -a 't1 specific changes'

then I switch to t2

  • git checkout t2

When I look into target, it is still populated with the files created
when branch t1 was active, while I would like to have these 'cleared out'

I found a similar discussion stating that git should behave differently than I see. Git is deleting an ignored file when i switch branches

What am I missing ?

Francis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2024-10-20 05:52:51

这是设计使然。 Git 不会触及工作文件夹中未跟踪的文件。如果你想清理未跟踪的文件,你可以使用

git clean -xdf

更常见的方式,我想扔掉未跟踪但未忽略的更改。这可能是因为我正在试验一些代码并添加了一个新的类文件。在这种情况下,我将使用:

git clean -df

这将使我的输出目录保持原样。我会依靠我的开发工具来清理这些文件夹。

您可以使用该钩子,但这不是一个好的解决方案,因为它更难在团队中共享。

This is by design. Git will not touch files that it is not tracking in the working folder. If you want to clean the untracked files you can use

git clean -xdf

More commonly, I want to throw away changes that are not tracked but not ignored. This may be because I was experimenting with some code and added a new class file. In this case I would use:

git clean -df

This would leave my output directories as is. I would rely on my development tools to clean these folders instead.

You could use the hook, but that's not a good solution as it is harder to share across a team.

温柔戏命师 2024-10-20 05:52:51

您可以添加一个 post-checkout 挂钩来清理被忽略的文件:

git clean -f -X

You can add a post-checkout hook which cleans ignored files:

git clean -f -X
蝶…霜飞 2024-10-20 05:52:51

您链接的问题正在讨论在一个分支中跟踪并在另一个分支中忽略的文件。您正在谈论未在任何分支中跟踪的生成文件。由于 git 不跟踪它们,因此不会对它们做任何事情。

The question you linked is talking about a file that's tracked in one branch and ignored in another. You're talking about generated files that aren't tracked in any branches. Since git isn't tracking them, it won't do anything to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文