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

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

    An almost git command-free approach was given in this answer:

    To ignore certain files for every local repo:

    1. Create a file ~/.gitignore_global, e.g. by touch ~/.gitignore_global in your terminal.
    2. Run git config --global core.excludesfile ~/.gitignore_global for once.
    3. Write the file/dir paths you want to ignore into ~/.gitignore_global. e.g. modules/*.H, which will be assumed to be in your working directory, i.e. $WORK_DIR/modules/*.H.

    To ignore certain files for a single local repo:

    1. Do the above third step for file .git/info/exclude within the repo, that is write the file/dir paths you want to ignore into .git/info/exclude. e.g. modules/*.C, which will be assumed to be in your working directory, i.e. $WORK_DIR/modules/*.C.
    0 讨论(0)
  • 2020-11-21 07:39

    This is a two step process:

    1. Remove tracking of file/folder - but keep them on disk - using

      git rm --cached 
      

      Now they do not show up as "changed" but still show as

          untracked files in  git status -u  
      
    2. Add them to .gitignore

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

    There are 3 options, you probably want #3

    1. This will keep the local file for you, but will delete it for anyone else when they pull.

    git rm --cached <file-name> or git rm -r --cached <folder-name>

    2. This is for optimization, like a folder with a large number of files, e.g. SDKs that probably won't ever change. It tells git to stop checking that huge folder every time for changes, locally, since it won't have any. The assume-unchanged index will be reset and file(s) overwritten if there are upstream changes to the file/folder (when you pull).

    git update-index --assume-unchanged <path-name>
    

    3. 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>
    

    It's important to know that git update-index will not propagate with git, and each user will have to run it independently.

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

    Apply .gitignore to the present/future

    This method applies the standard .gitignore behavior, and does not require manually specifying the files that need to be ignored.

    Can't use --exclude-from=.gitignore anymore :/ - Here's the updated method:

    General advice: start with a clean repo - everything committed, nothing pending in working directory or index, and make a backup!

    #commit up-to-date .gitignore (if not already existing)
    #this command must be run on each branch
    git add .gitignore
    git commit -m "Create .gitignore"
    
    #apply standard git ignore behavior only to current index, not working directory (--cached)
    #if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
    #this command must be run on each branch
    git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
    
    #optionally add anything to the index that was previously ignored but now shouldn't be:
    git add *
    
    #commit again
    #optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.
    
    git commit -m "re-applied modified .gitignore"
    
    #other devs who pull after this commit is pushed will see the  newly-.gitignored files DELETED
    

    If you also need to purge the newly-ignored files from the branch's commit history or if you don't want the newly-ignored files to be deleted from future pulls, see this answer.

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

    To ignore any changes to all the files (of a certain type) in a directory, I had to combine some of these approaches, otherwise the files were created if they didn't previously exist.

    In the below, "excludedir" is the name of the directory that I wish to not watch changes to.

    First, remove any existing new files from your change tracking cache (without removing from your file system).

    git status | grep "new file:" | cut  --complement -d " " -f1-4 | grep "^excludedir" | xargs git rm --cache
    

    You can do the same with modified:. renamed: is a bit more complicated, as you'll have to look at the post -> bit for the new filename, and do the pre -> bit as described for deleted: below.

    deleted: files prove a bit more complicated, as you can't seem to update-index for a file that doesn't exist on the local system

    echo .deletedfiles >> .gitignore
    git status | grep "deleted:" | cut  --complement -d " " -f1-4 | grep "^excludedir" > .deletedfiles
    cat .deletedfiles | xargs -d '\n' touch
    cat .deletedfiles | xargs -d '\n' git add -f
    cat .deletedfiles | xargs -d '\n' git update-index --assume-unchanged
    cat .deletedfiles | xargs -d '\n' rm
    

    The last command in the list above will remove the files again from your file system, so feel free to omit that.

    Then, block change tracking from that directory

    git ls-files excludedir/ | xargs git update-index --skip-worktree
    git update index --skip-worktree excludedir/
    
    0 讨论(0)
  • 2020-11-21 07:47

    The problem may be caused by the order of operation. If you modified the .gitignore first, then git rm --cached xxx,you may have to continue to encounter this problem.

    Correct solution:

    1. git rm --cached xxx
    2. modified the .gitignore

    Order invariant!

    The .gitignore reload after modification!

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