简体   繁体   English

如何忽略 Git 更改的文件?

[英]How to ignore Git changed files?

I'm working with this setup at the moment.我目前正在使用此设置。 I have 3 changed files that contains mostly environment variables such as Jwt tokens, cloud hosting variables, etc. The changes are mostly of this kind: empty string --> my env variable我有 3 个更改的文件,其中主要包含环境变量,例如 Jwt 令牌、云托管变量等。这些更改主要是这样的:空字符串 --> 我的环境变量

I won't ever include them in a commit because, obviously, each member of our team have them different from the other.我永远不会将它们包含在提交中,因为显然,我们团队的每个成员都有它们彼此不同。

But it's pretty annoying in my workflow.但这在我的工作流程中很烦人。 I can't stash them permanently because I want that the changes in those 3 files affect my local application.我无法永久存储它们,因为我希望这 3 个文件中的更改会影响我的本地应用程序。 I have to stash them and then stash pop to have them back, every single time I switch branch.每次切换分支时,我都必须将它们存储起来,然后将它们stash pop起来。

Somehow, I want Git to assume them as changed in every branch of my local repository without keeping them unstaged all the time... Have them untracked even if I changed the files.不知何故,我希望 Git 假设它们在我的本地存储库的每个分支中都已更改,而不会一直保持它们处于未暂存状态......即使我更改了文件,它们也不会被跟踪。

Thank you.谢谢你。

You can unstage files from the index using您可以使用从索引中取消暂存文件

git reset HEAD -- path/to/file git 重置 HEAD -- path/to/file

Just like git add, you can unstage files recursively by directory and so forth, so to unstage everything at once, run this from the root directory of your repository:就像 git add 一样,您可以通过目录等递归地取消暂存文件,因此要一次取消暂存所有内容,请从存储库的根目录运行:

git reset HEAD --. git 复位 HEAD --。

Also, for future reference, the output of git status will tell you the commands you need to run to move files from one state to another.此外,为了将来参考,git 状态的 output 将告诉您将文件从一个 state 移动到另一个所需的命令。

You can tell git that it should "assume" that the file is unchanged.你可以告诉 git 它应该“假设”文件没有改变。

git update-index --assume-unchanged some_file.txt

This just sets a flag that tells git这只是设置一个标志,告诉 git

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.“假设不变”位打开时,用户承诺不更改文件并允许 Git 假设工作树文件与索引中记录的内容匹配。

You can unset the flag with您可以取消设置标志

git update-index --no-assume-unchanged some_file.txt

EDIT编辑

What's the different between this update-index options and simply unstaging them and keeping them unstaged?这个 update-index 选项与简单地取消暂存它们并保持它们未暂存有什么不同? Anyway, thanks a lot.无论如何,非常感谢。

When the flag is set the path will not show up in a git status as modified.设置该标志后,路径将不会显示为已修改的 git 状态。 Changes to that path will be ignored.对该路径的更改将被忽略。 If you unstage the changes the working copy will show up as modified and you can accidentially add and commit it.如果您取消暂存更改,则工作副本将显示为已修改,您可能会意外添加并提交它。 Eg例如

git init test_repo
cd test_repo
cat << EOF > .credentials
# credentials template file
user=
pass=
EOF
git add .credentials
git commit -m 'Credentials template file added'

When you now edit the .credentials file it will show up in git status as modified.当您现在编辑.credentials文件时,它将以 git 状态显示为已修改。

git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .credentials

no changes added to commit (use "git add" and/or "git commit -a")

But if you tell git to assume that the file is unchanged it will be ignored.但是,如果您告诉 git 假设文件未更改,它将被忽略。

git update-index --assume-unchanged .credentials
git status
On branch master
nothing to commit, working tree clean

PS: It would be better to add those files to .gitignore and never commit then, even templates. PS:最好将这些文件添加到.gitignore然后永远不要提交,即使是模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM