Git:切换分支时保留忽略的文件 [英] Git: keep ignored files when switching branches

查看:118
本文介绍了Git:切换分支时保留忽略的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这似乎是 GIT:如何复制的副本以便在切换分支时保留被忽略的文件?,但是我在那里找不到答案.

I know it seems like a duplicate of GIT: How to keep ignored files when switching branches?, but I couldn't find my answer there.

因此,我使用-orphan 标志创建了一个新分支,以删除提交历史记录.然后,我对所有文件做了 git rm --cached ,修改了 .gitignore 以忽略其中的一些文件,然后添加并提交.与预期的一样,被忽略的文件不在新的存储库中(也不在其任何历史记录中).但是,它们在我切换分支时消失(即,当我签出旧分支,然后再次签出新分支时).我该如何避免呢?

So I created a new branch with the --orphan flag, to delete the commit history. Then I did git rm --cached on all the files, modified .gitignore to ignore some of them, then added and committed. As expected, the ignored files are not in the new repo (nor in any of its history). However, they disappear when I switch branches (that is, when I check out an old branch, then check out the new one again). How can I avoid that?

整个操作的目的是准备一个要在GitHub上发布的项目,因此我想隐藏一些包含私人信息(例如密码,密钥等)的配置文件.这就是为什么我通过创建删除提交历史记录的原因一个新的孤儿分支.我知道在将文件添加到 .gitignore 之前提交文件可能会解决消失的问题,但这最终会使文件在公共历史记录中可见.我试图先做 git add -f ,然后再做 git rm --cached ,但这并不能解决问题.

The purpose of the whole operation was to prepare a project to be published on GitHub, so I wanted to hide some config files that contain private information like passwords, keys, etc. That's why I deleted the commit history by creating a new orphan branch. I understand that probably committing the files before adding them to .gitignore would solve the disappearing issue, but that would make the files ultimately visible in the public history. I tried to do git add -f, then git rm --cached, but it doesn't do the trick.

推荐答案

但是,当我切换分支时(即,当我签出旧分支,然后再次签出新分支时),它们消失了.我该如何避免呢?

However, they disappear when I switch branches (that is, when I check out an old branch, then check out the new one again). How can I avoid that?

您不能.这是设计使然.在您的 publish 分支中,您没有跟踪文件,并且有一个 .gitignore .伟大的!但是,当您切换到确实跟踪了这些文件的其他分支时,它们将被检出到您的工作目录中.这是因为 .gitignore 仅适用于未跟踪的文件.

You cannot. This is by-design. In your publish branch, you do not have the files tracked, and you have a .gitignore for them. Great! But when you switch to some other branch that does have those files tracked, they will be checked out to your working directory. This is because a .gitignore only applies to untracked files.

文档:

一个gitignore文件指定Git应该忽略的故意未跟踪的文件.已被Git跟踪的文件不受影响.

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.

现在,如果您尝试切换回 publish 分支,则文件将在磁盘上删除,因为该分支中不存在这些文件.您对这些文件的 .gitignore 被忽略,因为它们已被跟踪,并且不适用于它们.

Now if you try to switch back to your publish branch, the files will be removed on-disk because they do not exist in that branch. Your .gitignore for these files is ignored because they are tracked and it would not apply to them.

(请注意,即使它们位于您要离开的分支中,按定义,它们也会被跟踪 .这主要只是实现细节,我承认这可能不会直观一点.)

(Note that they are - by definition - tracked even if they're in the branch that you're switching away from. This is mostly just an implementation detail and I concede that this may not be intuitive.)

您要执行的操作是删除所有分支中所有出现的这些文件.使用 BFG Repo Cleaner 之类的工具从您的历史记录中完全删除这些文件,在所有分支机构中.然后,您可以安全地 .gitignore 它们,然后一旦它们完全消失 即可安全地将其推送到GitHub.

What you want to do is remove every occurrence of these files in all the branches. Use a tool like BFG Repo Cleaner to remove these files from your history completely, in all branches. Then you can safely .gitignore them, then once they're gone completely you can safely push this to GitHub.

这篇关于Git:切换分支时保留忽略的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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