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

前端 未结 20 1711
梦谈多话
梦谈多话 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:55

    To prevent monitoring a file by 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

    0 讨论(0)
  • To tell Git not to track changes to your local file/folder (meaning git status won't detect changes to it), do:

    git update-index --skip-worktree path/to/file
    

    And to tell Git to track changes to your local version once again (so you can commit the changes), do:

    git update-index --no-skip-worktree path/to/file
    
    0 讨论(0)
  • 2020-11-21 07:56

    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 .gitignore or .git/info/exclude, then ones you create will be ignored)

    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.

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

    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.

    What you probably want to do is this: 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.)

    Difference between the first two options?

    • assume-unchanged is 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-unchanged then probably stash modifications done.
    • skip-worktree will follow you whatever the branch you checkout, with your modifications!

    Use case of 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 status won't show you the flagged files).

    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 same modification.

    Use case of 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.

    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.

    0 讨论(0)
  • 2020-11-21 08:00

    I am assuming that you are trying to remove a single file from git tacking. for that I would recommend below command.

    git update-index --assume-unchanged

    Ex - git update-index --assume-unchanged .gitignore .idea/compiler.xml

    0 讨论(0)
  • 2020-11-21 08:02

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

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

    or

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

    e.t.c.

    0 讨论(0)
提交回复
热议问题