Search code examples
gitgitignoregit-stash

Ignore changes in the files, which are in the git repository, locally


To make my project work locally I have to change one file. But this file has to be unchanged in the remote. How do I make git ignore the changes in this file when using git status, git add and git stash command?

What I tried:

1.

git update-index --assume-unchanged filename

makes it work with 'git status' and 'git add', but 'git stash'+'git stash pop' restores the file to the original state, breaking my local setup.

2. I found this solution, but it changes remote as well, which I don't want to: https://www.codeblocq.com/2016/01/Untrack-files-already-added-to-git-repository-based-on-gitignore/

3. adding the file to .git/info/exclude doesn't work, since it is a file from the repository, not a new file.


Solution

  • If you want to modify files locally but you don't want that Git manage these changes remotely, you can set the flag --skip-worktree on a file which means the files should change locally, but not remotely. Any changes made and saved to this file will not be flagged as a change.

    $ git update-index --skip-worktree path/to/your/skipping/file
    

    To confirm, which files are skipped by Git, you can execute following command:

    $ git ls-files -v | grep ^S
    
    • git ls-files shows all files managed by Git
    • -v check the file being ignored
    • --skip-worktree is displayed with S at line begin

    To restore the file-skipping by Git, you need this command:

    $ git update-index --no-skip-worktree path/to/your/skipping/file