Git Ignore 不起作用,我不明白

发布于 2024-12-12 09:16:58 字数 454 浏览 0 评论 0原文

我以为我已经解决了这个问题,但最近的尝试却失败了。

我需要提交一个文件,然后在提交后在任何后续存储库提交中忽略它。

假设该文件是 example.txt。

我提交对 example.txt 的更改并将其推送到 github。

然后我触摸 .gitignore 并将 example.txt 添加到其中并保存。
然后我使用 git rm --cached example.txt
然后 git add .gitignore 进行提交 --> 推送

这一步的问题是 git 状态显示 example.txt 已删除,因此提交/推送会将其从远程存储库中删除。

我尝试了创建 .gitignore 和 rm --cached 的几种变体,它要么删除了文件,要么不忽略它并提交不需要的更改。

寻找有关忽略已提交文件而不删除它们的确切过程的见解。

I thought I had figured this out but on recent attempts it is failing.

I need to commit a file, then after the commit ignore it on any subsequent repo commits.

Lets say the file is example.txt.

I commit changes to example.txt and push them to github.

I then touch .gitignore and add example.txt to it and save.
I then use git rm --cached example.txt
Then git add .gitignore to commit -->push it

The problem at this step is a git status is showing example.txt as deleted and thus the commit/push deletes it from the remote repo.

I have tried several variations on creating a .gitignore and rm --cached, and it either deleted the file or does not ignore it and commits unwanted changes.

Looking for insight on the exact process to ignore already committed files without deleting them.

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

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

发布评论

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

评论(2

青芜 2024-12-19 09:16:58

您确实意识到 git rm --cached example.txt 将从索引中删除该文件并将其单独保留在工作目录中。然后,当您提交时,除了您添加的 .gitignore 文件之外,您还提交了文件的删除。因此,当您推送文件时,它将从存储库中删除。

另外,您不能使用 gitignore 忽略对跟踪文件的更改。您可以尝试执行 git update-index --assume-unchanged example.txt 来“忽略”文件。

You do realize that git rm --cached example.txt will remove the file from the index and keep it in the working directory alone. And then when you commit, your are committing the the deletion of the file, in addition to the .gitignore file that you added. Thus, when you push the file, it will be deleted from the repo.

Also, you cannot ignore changes to a tracked file using gitignore. You can instead try to do git update-index --assume-unchanged example.txt to "ignore" the file.

万水千山粽是情ミ 2024-12-19 09:16:58

一种可能的方法是告诉 Git 假设文件未更改。

$ mkdir project
$ cd project
$ touch example.txt
$ git init
$ git add .
$ git commit -m "checkin..."
$ git update-index --assume-unchanged example.txt
$ echo "hello" >> example.txt
$ git status
# On branch master
nothing to commit (working directory clean)
$

相信这就是你想要的。请预先警告,这种方法有其缺点——特别是当您尝试切换到 example.txt 版本与 master 中版本不同的分支时。

One possible approach is to tell Git to assume the file is unchanged.

$ mkdir project
$ cd project
$ touch example.txt
$ git init
$ git add .
$ git commit -m "checkin..."
$ git update-index --assume-unchanged example.txt
$ echo "hello" >> example.txt
$ git status
# On branch master
nothing to commit (working directory clean)
$

Trust that's what you want. Be forewarned that this approach has its warts -- especially if you try to switch to a branch where the version of example.txt is different from what's in master.

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