简体   繁体   中英

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.

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)

    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

Approach 1:

Do not commit files that should differ per developer. All of my config files (eg config.yaml ) are in .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.

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. If you do wish to commit them again, rerun it with --no-assume-unchanged .

There doesn't seem to a be a problem. Git is notifying you that those files to be in the repository and are being deleted from the repository. 在存储库中并且正在从存储库中删除。

This is exactly what you want. The fact that git removes them from the repository does not mean that you are deleting them from your local file system. 意味着您正在从本地文件系统中删除它们。 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 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 .很有For instance, it's very commit to add

*.class
/bin

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

If you want to omit files from a commit, you should use the ' stash ' method.

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.

http://git-scm.com/docs

Another option is to use: 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. Use assume-unchanged when git can safely assume the file is unchanged and optimize its behavior accordingly.

A very thorough explanation is given here: Git - Difference Between 'assume-unchanged' and 'skip-worktree'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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