将文件添加到gitignore [英] Adding files to gitignore

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

问题描述

我不小心提交了应该在gitignore中的文件,当我拉提交时,我的文件被提交中的文件覆盖了.

I accidentally committed files that should be in gitignore, and when I pulled the commit my files were overwritten by the ones from the commit.

我将提交还原到上一个提交,并替换了文件的内容,但是现在当我将进行新的更改时,这些文件的内容将被还原的先前提交再次覆盖.

I reverted the commit to the previous one and replaced the content of the files, but now when I will pull new changes, the content of those files will be overwritten again by the reverted earlier commit.

我该如何处理?

推荐答案

我不小心提交了应该在gitignore中的文件

I accidentally committed files that should be in gitignore

您首先必须将其从历史记录中删除.
您有几种方法可以执行此操作(还原,重置,签出< commit>等)

You first have to remove them from the history.
You have few ways for doing it (revert, reset, checkout <commit> etc)

您只需在提交中还原代码即可.这将撤消您的更改

You can simply revert the code in your commit. This will undo your changes

revert <commit id>

重置

reset HEAD <commit id>

这会将您的分支移动"到任何给定的所需提交,然后签出分支,然后返回旧代码ץ

This will "move" your branch to any given desired commit and then checkout branch and you are back with your old codeץ

一旦有了所需的代码,便要使用-f/--force标志将其推送到遥控器.

Once you have the desired code you want push it to the remote with the -f/--force flag.

将文件添加到.gitignore,以便git无法检测到.

Add the file to the .gitignore so it will be undetectable by git.

如果您仍然希望文件在存储库中(例如,使用默认值),则应在跟踪的文件上使用assume-unchanged标志.

If you still want the file in the repository (with default values for example) you should use the assume-unchanged flag on the tracked file.

https://git-scm.com/docs/git-update-index

指定此标志时,不会更新为路径记录的对象名称.
而是,此选项设置/取消设置路径的假定不变"位.

When this flag is specified, the object names recorded for the paths are not updated.
Instead, this option sets/unsets the "assume unchanged" bit for the paths.

当假定不变"位打开时,用户承诺不更改文件,并允许Git假定工作树文件与索引中记录的文件匹配.如果要更改工作树文件,则需要取消设置该位以告知Git.当在lstat(2)系统调用非常慢的文件系统(例如cifs)上处理大型项目时,这有时会很有帮助.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

如果需要在索引中修改此文件,Git将失败(正常).合并提交时;因此,如果假定未跟踪的文件在上游进行了更改,则您将需要手动处理这种情况.

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

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

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