Git-.gitignore中的提交文件 [英] Git - Commit file in .gitignore

查看:63
本文介绍了Git-.gitignore中的提交文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提交并推送项目.gitignore中列出的文件夹内的两个新文件.我认为我可以只更改.gitignore文件,但随后我需要进行两次提交:一次提交以推送两个新文件,第二次提交以重置.gitignore.如果可能的话,我想一次提交.

I want to commit and push two new files that are inside a folder that is listed in the project's .gitignore. I figure I can just change the .gitignore file, but then I would require two commits: one commit to push the two new files, and a second commit to reset the .gitignore. I'd like to do this in one commit if possible.

是否有任何方法可以提交&推送被忽略的特定文件?

Is there any way to commit & push a specific file that is ignored?

推荐答案

您不必修改 .gitignore:,您可以强制添加这些文件:

You don't have to modify the .gitignore: you can force the addition of those files:

git add --force -- file1 file2
git commit -m "add previously ignored files"
git push

来自 git add 手册页:

-f
--force

允许添加否则被忽略的文件.

Allow adding otherwise ignored files.

作为 JakubNarębski的评论,这些文件将不再被忽略>,即使它们仍可以由 .gitignore 指令选择.

As Jakub Narębski comments, those files are no longer ignored, even if they would still be selected by the .gitignore directives.

汤姆·哈特(Tom Hart)

只是想知道在使用--force之后是否有任何方法可以重新忽略文件?

Just wondering if there's anyway to re-ignore the files after using --force?

您需要记录它们从索引中的删除,以便在工作树中再次忽略这些文件:

You need to record their deletion from the index, in order for those files to be ignored again in the working tree:

git rm --cached file1 file2
git commit -m "Remove files which should be ignored"
git push

.gitignore 规则将在从索引中删除文件后立即生效(在提交和推送步骤之前).
推送意味着其他贡献者将受到操作的影响.

The .gitignore rules will work as soon as the files are removed from the index (before the commit and push steps).
Pushing means other contributors will be impacted by the operation.

如果您只是想暂时本地忽略这些文件,请使用:

If you just want to ignore those files locally and temporarily, use:

git update-index --assume-unchanged -- file1 file2

(正如我今天早些时候在" GIT中忽略的那样,使用排除进行本地更改忽略已经提交的文件")

(as I detailed earlier today in "GIT Ignore already committed files using exclude for local changes")

作为 Zeeker 添加

Atm git没有提供一种机制来忽略每个克隆中对提交文件的所有更改.

Atm git doesn't provide a mechanism to ignore all changes to a committed file across each clone.

所以:

  • git rm --cache 将删除文件(同时保留其以前的历史记录):file1或file2将不再可见,并且将被忽略.
  • git update-index --assume-unchanged-file1 file2 删除文件,但不再检测到 local 修改.
  • git rm --cache would remove the file (while keeping its previous history): file1 or file2 would no longer be visible, and would be ignore.
  • git update-index --assume-unchanged -- file1 file2 would not remove the files, but no longer detect local modifications.

这篇关于Git-.gitignore中的提交文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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