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

前端 未结 20 1698
梦谈多话
梦谈多话 2020-11-21 07:17

I have cloned a project that includes some .csproj files. I don\'t need/like my local csproj files being tracked by Git (or being brought up when c

相关标签:
20条回答
  • 2020-11-21 07:47

    git rm --fileName

    git ls-files to make sure that the file is removed or untracked

    git commit -m "UntrackChanges"

    git push

    0 讨论(0)
  • 2020-11-21 07:48

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

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

    git update-index --skip-worktree <file>
    
    0 讨论(0)
  • 2020-11-21 07:50

    I am assuming that you are asking how to remove ALL the files in a specific folder or the bin folder, Rather than selecting each files separately.

    You can use this command:

    git rm -r -f /<floder-name>\*

    Make sure that you are in the parent directory of the of that directory.
    This command will, recursively "delete" all the files which are in the bin/ or build/ folders. By the word delete I mean that git will pretend that those files are "deleted" and those files will not be tracked. The git really marks those files to be in delete mode.

    Do make sure that you have your .gitignore ready for upcoming commits.
    Documentation : git rm

    0 讨论(0)
  • 2020-11-21 07:51

    The accepted answer still did not work for me

    I used

    git rm -r --cached .

    git add .

    git commit -m "fixing .gitignore"

    Found the answer from here

    0 讨论(0)
  • 2020-11-21 07:54

    Just calling git rm --cached on 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.

    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 update-index --assume-unchanged [path]
    

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

    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>

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

    0 讨论(0)
  • 2020-11-21 07:55

    Forgot your .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 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 .gitignore or forgot it all together.

    • It removes the entire cache
    • Looks at your .gitignore
    • Adds the files you want to track
    • Pushes to your repo
    0 讨论(0)
提交回复
热议问题