如何停止跟踪并忽略对 Git 中文件的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/936249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:32:44  来源:igfitidea点击:

How to stop tracking and ignore changes to a file in Git?

git

提问by Joshua Ball

I have cloned a project that includes some .csprojfiles. I don't need/like my local csprojfiles being tracked by Git (or being brought up when creating a patch), but clearly they are needed in the project.

我克隆了一个包含一些.csproj文件的项目。我不需要/喜欢我的本地csproj文件被 Git 跟踪(或在创建补丁时被调出),但显然项目中需要它们。

I have added *.csprojto my LOCAL .gitignore, but the files are already in the repo.

我已经添加*.csproj到我的 LOCAL .gitignore,但文件已经在 repo 中。

When I type git status, it shows my changes to csprojwhich I am not interested in keeping track of or submitting for patches.

当我输入 git status 时,它会显示我csproj对跟踪或提交补丁不感兴趣的更改。

How do I remove the "tracking of" these files from my personal repo (but keep them in the source so I can use them) so that I don't see the changes when I do a status (or create a patch)?

如何从我的个人存储库中删除这些文件的“跟踪”(但将它们保留在源中以便我可以使用它们),以便在执行状态(或创建补丁)时看不到更改?

Is there a correct/canonical way to handle this situation?

有没有正确/规范的方法来处理这种情况?

回答by anthony

Just calling git rm --cachedon each of the files you want to remove from revision control should be fine. As long as your local ignore patterns are correct you won't see these files included in the output of git status.

只需调用git rm --cached要从修订控制中删除的每个文件就可以了。只要您的本地忽略模式是正确的,您就不会在 git status 的输出中看到这些文件。

Note that this solution removes the files from the repository, so all developers would need to maintain their own local (non-revision controlled) copies of the file

请注意,此解决方案从存储库中删除文件,因此所有开发人员都需要维护自己的本地(非修订控制)文件副本

To prevent git from detecting changes in these files you should also use this command:

为了防止 git 检测这些文件中的更改,您还应该使用以下命令:

git update-index --assume-unchanged [path]

What you probably want to do:(from below @Ryan Taylor answer)

你可能想要做什么:(从下面@Ryan Taylor 回答

  1. This is to tell git you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

git update-index --skip-worktree <path-name>

  1. 这是告诉 git 你想要你自己的文件或文件夹的独立版本。例如,您不想覆盖(或删除)生产/暂存配置文件。

git update-index --skip-worktree <path-name>

The full answer is here in this URL: http://source.kohlerville.com/2009/02/untrack-files-in-git/

完整答案在此 URL 中:http: //source.kohlerville.com/2009/02/untrack-files-in-git/

回答by araqnid

If you do git update-index --assume-unchanged file.csproj, git won't check file.csproj for changes automatically: that will stop them coming up in git status whenever you change them. So you can mark all your .csproj files this way- although you'll have to manually mark any new ones that the upstream repo sends you. (If you have them in your .gitignoreor .git/info/exclude, then ones you create will be ignored)

如果你这样做git update-index --assume-unchanged file.csproj,git 将不会自动检查 file.csproj 的更改:无论何时更改它们,这都会阻止它们进入 git 状态。因此,您可以通过这种方式标记所有 .csproj 文件 - 尽管您必须手动标记上游存储库发送给您的任何新文件。(如果您的.gitignoreor 中有它们.git/info/exclude,那么您创建的将被忽略)

I'm not entirely sure what .csproj files are... if they're something along the lines of IDE configurations (similar to Eclipse's .eclipse and .classpath files) then I'd suggest they should simply never be source-controlled at all. On the other hand, if they're part of the build system (like Makefiles) then clearly they should--- and a way to pick up optional local changes (e.g. from a local.csproj a la config.mk) would be useful: divide the build up into global parts and local overrides.

我不完全确定 .csproj 文件是什么......如果它们是 IDE 配置(类似于 Eclipse 的 .eclipse 和 .classpath 文件)的东西,那么我建议它们根本不应该在全部。另一方面,如果它们是构建系统的一部分(如 Makefile),那么显然它们应该——并且一种获取可选本地更改的方法(例如从 local.csproj 和 config.mk)将很有用:将构建分为全局部分和局部覆盖。

回答by Ryan Taylor

There are 3 options, you probably want #3

有 3 个选项,您可能想要 #3

1.This will keep the local file for you, but will delete it for anyone else when they pull.

1.这将为您保留本地文件,但会在其他人拉取时将其删除。

git rm --cached <file-name>or git rm -r --cached <folder-name>

git rm --cached <file-name>或者 git rm -r --cached <folder-name>

2.This is for optimization, like a folder with a large number of files, e.g. SDKs that probably won't ever change. It tells git to stop checking that huge folder every time for changes, locally, since it won't have any. The assume-unchangedindex will be reset and file(s) overwritten if there are upstream changes to the file/folder (when you pull).

2.这是为了优化,比如一个文件夹里有大量文件,例如可能永远不会改变的SDK。它告诉 git 每次在本地停止检查那个巨大的文件夹是否有更改,因为它不会有任何更改。assume-unchanged如果文件/文件夹的上游更改(当您拉取时),索引将被重置并覆盖文件。

git update-index --assume-unchanged <path-name>

3.This is to tell git you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

3.这是告诉git你想要你自己独立版本的文件或文件夹。例如,您不想覆盖(或删除)生产/暂存配置文件。

git update-index --skip-worktree <path-name>

It's important to know that git update-indexwill not propagatewith git, and each user will have to run it independently.

重要的是要知道gitgit update-index不会传播,每个用户都必须独立运行它。

回答by rjha94

This is a two step process:

这是一个两步过程:

  1. Remove tracking of file/folder - but keep them on disk - using

    git rm --cached 
    

    Now they do not show up as "changed" but still show as

        untracked files in  git status -u  
    
  2. Add them to .gitignore

  1. 删除文件/文件夹的跟踪 - 但将它们保留在磁盘上 - 使用

    git rm --cached 
    

    现在它们不会显示为“已更改”,但仍显示为

        untracked files in  git status -u  
    
  2. 将它们添加到 .gitignore

回答by Buddhika Hasthanayake

The accepted answer still did not work for me

接受的答案仍然对我不起作用

I used

我用了

git rm -r --cached .

git add .

git commit -m "fixing .gitignore"

git rm -r --cached 。

git 添加。

git commit -m "修复 .gitignore"

Found the answer from here

这里找到答案

回答by Joe Lloyd

Forgot your .gitignore?

忘记了你的 .gitignore?

If you have the entire project locally but forgot to add you git ignore and are now tracking some unnecessary files use this command to remove everything

如果您在本地拥有整个项目但忘记添加您 git ignore 并且现在正在跟踪一些不必要的文件,请使用此命令删除所有内容

git rm --cached -r .

make sure you are at the root of the project.

确保您位于项目的根目录。

Then you can do the usual

然后你可以做通常的

Add

添加

git add .

Commit

犯罪

git commit -m 'removed all and added with git ignore'

Push

git push origin master

Conclusion

结论

Hope this helps out people who have to make changes to their .gitignoreor forgot it all together.

希望这可以帮助那些必须对其进行更改.gitignore或将其全部忘记的人。

  • It removes the entire cache
  • Looks at your .gitignore
  • Adds the files you want to track
  • Pushes to your repo
  • 它删除整个缓存
  • 看看你的 .gitignore
  • 添加要跟踪的文件
  • 推送到你的仓库

回答by the_new_mr

As pointed out in other answers, the selected answer is wrong.

正如其他答案中所指出的,所选答案是错误的。

The answerto another question suggests that it may be skip-worktree that would be required.

另一个问题的答案表明可能需要跳过工作树。

git update-index --skip-worktree <file>

回答by Peter

To save some time the rules you add to your .gitignore can be used for removing multiple files/folders i.e.

为了节省一些时间,您添加到 .gitignore 的规则可用于删除多个文件/文件夹,即

git rm --cached app/**/*.xml

git rm --cached app/**/*.xml

or

或者

git rm --cached -r app/widgets/yourfolder/

git rm --cached -r app/widgets/yourfolder/

e.t.c.

等等

回答by shijin

To prevent monitoring a file by git

防止通过 git 监视文件

git update-index --assume-unchanged [file-path]

And to revert it back use

并恢复使用

git update-index --no-assume-unchanged [file-path]

A repo to refer for similar use cases https://github.com/awslabs/git-secrets

参考类似用例的回购https://github.com/awslabs/git-secrets

回答by belka

Lots of people advise you to use git update-index --assume-unchanged. Indeed, this may be a good solution, but only in the short run.

很多人建议你使用git update-index --assume-unchanged. 的确,这可能是一个很好的解决方案,但只是在短期内。

What you probably want to do is this: git update-index --skip-worktree.

什么你可能想要做的是这样的:git update-index --skip-worktree

(The third option, which you probably don't want is: git rm --cached. It will keep your local file, but will be marked as removed from the remote repository.)

(您可能不想要的第三个选项是:git rm --cached。它将保留您的本地文件,但会被标记为从远程存储库中删除。)

Difference between the first two options?

前两个选项的区别?

  • assume-unchangedis to temporary allow you to hide modifications from a file. If you want to hide modifications done to a file, modify the file, then checkout another branch, you'll have to use no-assume-unchangedthen probably stash modifications done.
  • skip-worktreewill follow you whatever the branch you checkout, with your modifications!
  • assume-unchanged是临时允许您隐藏对文件的修改。如果你想隐藏对文件所做的修改,修改文件,然后签出另一个分支,你将不得不使用no-assume-unchanged然后可能隐藏修改完成。
  • skip-worktree无论您结帐的分支是什么,都将跟随您,并进行修改!

Use case of assume-unchanged

用例 assume-unchanged

It assumes this file should not be modified, and gives you a cleaner output when doing git status. But when checking out to another branch, you need to reset the flag and commit or stash changes before so. If you pull with this option activated, you'll need to solve conflicts and git won't auto merge. It actually only hides modifications (git statuswon't show you the flagged files).

它假定不应修改此文件,并在执行git status. 但是当签出到另一个分支时,您需要重置标志并在此之前提交或存储更改。如果在激活此选项的情况下拉动,则需要解决冲突并且 git 不会自动合并。它实际上只隐藏修改(git status不会向您显示标记的文件)。

I like to use it when I only want to stop tracking changes for a while + commit a bunch of files (git commit -a) related to the samemodification.

当我只想停止跟踪更改一段时间 + 提交一堆git commit -a相同修改相关的文件 ( )时,我喜欢使用它。

Use case of skip-worktree

用例 skip-worktree

You have a setup class containing parameters (eg. including passwords) that your friends have to change accordingly to their setup.

您有一个包含参数(例如,包括密码)的设置类,您的朋友必须根据他们的设置更改这些参数。

  • 1: Create a first version of this class, fill in fields you can fill and leave others empty/null.
  • 2: Commit and push it to the remote server.
  • 3: git update-index --skip-worktree MySetupClass.java
  • 4: Update your configuration class with your own parameters.
  • 5: Go back to work on another functionnality.
  • 1:创建此类的第一个版本,填写您可以填写的字段,并将其他字段留空/空。
  • 2:提交并推送到远程服务器。
  • 3: git update-index --skip-worktree MySetupClass.java
  • 4:用你自己的参数更新你的配置类。
  • 5:回去工作另一个功能。

The modifications you do will follow you whatever the branch. Warning: if your friends also want to modify this class, they have to have the same setup, otherwise their modifications would be pushed to the remote repository. When pulling, the remote version of the file should overwrite yours.

无论分支如何,您所做的修改都将跟随您。警告:如果你的朋友也想修改这个类,他们必须有相同的设置,否则他们的修改将被推送到远程存储库。拉取时,文件的远程版本应该覆盖你的。

PS: do one or the other, but not both as you'll have undesirable side-effects. If you want to try another flag, you should disable the latter first.

PS:做一个或另一个,但不要两个都做,因为你会产生不良的副作用。如果你想尝试另一个标志,你应该先禁用后者。