简体   繁体   English

如何在 git 中本地取消跟踪几个文件

[英]How can untrack few files locally in git

I am working on project on my local machine.我正在我的本地机器上处理项目。 so i have different DB details, so i edited 2 files.所以我有不同的数据库详细信息,所以我编辑了 2 个文件。

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   product/db.py
        modified:   product/prms.py

Now i don't want to commit it and want to ignore them locally so that no matter what i edit in those files they never gets pushed to remote repo现在我不想提交它并想在本地忽略它们,这样无论我在这些文件中编辑什么,它们都不会被推送到远程仓库

I tried put them in我试着把它们放进去

.git/info/exclude

Then i did然后我做了

git rm --cached <file>

but then system is removing them但随后系统正在删除它们

Changes to be committed: (use "git reset HEAD ..." to unstage)要提交的更改:(使用“git reset HEAD ...”取消暂存)

    deleted:    product/db.py
    deleted:    product/prms.py

But i don't want to remove them as well但我也不想删除它们

How can i fix that我该如何解决

EDIT: I don't want push anything to remote repo regarding that.编辑:我不想将任何内容推送到远程仓库。 i just want to ignore edits to those file from my compuer perspective only.我只想从我的计算机角度忽略对这些文件的编辑。 so that when i got to office and then i make chnage in that file then it should work as normal.这样当我到达办公室然后我在该文件中进行更改时,它应该可以正常工作。 but from my home any edits should be invisible to git但是在我家中,任何编辑都应该对 git 不可见

Approach 1:方法一:

Do not commit files that should differ per developer.不要提交每个开发人员应该不同的文件。 All of my config files (eg config.yaml ) are in .gitignore ;我所有的配置文件(例如config.yaml )都在.gitignore for each, I will have another file, (eg config.yaml.template ), that would show the developers what they need to look like, which I would only edit when the structure changes.对于每个,我将有另一个文件(例如config.yaml.template ),它将向开发人员展示他们需要的样子,我只会在结构更改时对其进行编辑。

Approach 2:方法二:

git update-index --assume-unchanged product/db.py product/prms.py

will let you change the files, and git will not commit them.将让您更改文件,而git不会提交它们。 If you do wish to commit them again, rerun it with --no-assume-unchanged .如果您确实希望再次提交它们,请使用--no-assume-unchanged重新运行它。

There doesn't seem to a be a problem.似乎没有什么问题。 Git is notifying you that those files used to be in the repository and are being deleted from the repository. Git 会通知您这些文件曾经在存储库中并且正在从存储库中删除。

This is exactly what you want.这正是您想要的。 The fact that git removes them from the repository does not necessarily mean that you are deleting them from your local file system. git 从存储库中删除它们的事实并不一定意味着您正在从本地文件系统中删除它们。 In fact, since you've used事实上,自从你使用

git rm --cached 

they should still be in your file system.它们应该仍在您的文件系统中。

If at any point there was a commit that was pushed onto your branch which added these files, you will have to push another commit which will be removing these files.如果在任何时候有一个提交被推送到添加了这些文件的分支上,你不得不推送另一个将删除这些文件的提交。

If these files follow a pattern, you might find it useful to add them to the .gitignore .如果这些文件遵循某种模式,您可能会发现将它们添加到.gitignore很有 For instance, it's very commit to add例如,它非常致力于添加

*.class
/bin

in your .gitignore because you tend not to want to push class files (if you're doing Java/Scala) or binaries which tend to live in /bin在您的.gitignore 中,因为您往往不想推送类文件(如果您正在使用 Java/Scala)或倾向于位于/bin 中的二进制文件

If you want to omit files from a commit, you should use the ' stash ' method.如果你想从提交中省略文件,你应该使用 ' stash ' 方法。

git stash --keep-index

This will stash all your uncommitted changes -- you can always un-stash them later.这将隐藏您所有未提交的更改——您可以随时取消隐藏它们。 Now you can keep working without those files getting pushed to your remote repository..现在您可以继续工作,而不会将这些文件推送到您的远程存储库。

Keep in mind that you can pull and continue to collaborate while still keeping these changes stashed.请记住,您可以拉动并继续协作,同时仍然保留这些更改。

If you use something like bitbucket, you could have gone to the 'source' of the branch that you were working with and gotten the code from your last commit and fixed these two files.如果你使用像 bitbucket 这样的东西,你可以去你正在使用的分支的“源”并从上次提交中获取代码并修复这两个文件。

http://git-scm.com/docs http://git-scm.com/docs

Another option is to use: git update-index --skip-worktree path/to/file另一种选择是使用: git update-index --skip-worktree path/to/file

There are a few practical and philosophical differences.有一些实际和哲学上的差异。 Mainly, use skip-worktree when modifying the file is expected.主要是在需要修改文件时使用skip-worktree Use assume-unchanged when git can safely assume the file is unchanged and optimize its behavior accordingly.当 git 可以安全地假设文件未更改并相应地优化其行为时,请使用assume-unchanged

A very thorough explanation is given here: Git - Difference Between 'assume-unchanged' and 'skip-worktree'这里给出了一个非常彻底的解释: Git - 'assume-unchanged' 和 'skip-worktree' 的区别

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

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