0

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.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
klm123
  • 12,105
  • 14
  • 57
  • 95
  • 1
    Does this answer your question? [How do I configure git to ignore some files locally?](https://stackoverflow.com/questions/1753070/how-do-i-configure-git-to-ignore-some-files-locally) – SwissCodeMen Mar 14 '21 at 08:51
  • 2
    You can't. Whether Git tracks a file is all or nothing. You need to split your configuration into two files, one for the defaults which is checked in, and one for the local overrides which is not. Alternatively, follow the 12 Factor App and [store the configuration in the environment](https://www.12factor.net/config), particularly if it's something like a key or password or database name. – Schwern Mar 14 '21 at 08:59
  • @SwissCodeMen, that link says to do fixes 1 and 3. and as I told it doesn't work with 'git stash' for me. – klm123 Mar 14 '21 at 09:08
  • @SwissCodeMen, oh, i'm sorry. git update-index --skip-worktree actually worked for me. I got confused by that answer, since it told that this is another way to do the thing, which didn't work for me. git update-index --skip-worktree is not equivalent for git update-index --assume-unchanged + adding the file to git/exlude. Do you want to make an answer from this or I should? – klm123 Mar 14 '21 at 09:17
  • 1
    @Schwern but why is this not possible? Look at my answer, with the flag `--skip-worktree` is it possible to ignore changes on local files. Any changes made and saved to this file will not be flagged as a change and Git doesn't respond to it. – SwissCodeMen Mar 14 '21 at 12:56
  • @SwissCodeMen You're right, you can do that for an individual clone. It's extremely fragile. – Schwern Mar 14 '21 at 18:44

2 Answers2

1

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
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • Note this only applies to your specific clone. Any other clone will still track changes to the file. It's a fragile technique. – Schwern Mar 14 '21 at 18:45
  • 1
    @Schwern but this is the whole point! to avoid any external changes, so no other people or even my own clones on other PC's are affected. What's fragile about it? – klm123 Mar 14 '21 at 19:31
  • @klm123 If it works for you, ok, but it will only work for you... maybe. If a second developer works on the project, or if you have to clone it again, they will also have to remember to skip that file. Someone eventually won't know or will forget and a local-only change will be committed and pushed. Git is a poor configuration manager. – Schwern Mar 14 '21 at 19:40
0

You can do this several ways:

  • Do a git status and then you will see what files are changed. after that you can git add files separately.
    Eg: git add [file name (simply copy the path here when you get result from git status )]

  • Add to your file to git ignore file. This will un-track your changes on that mention file.

Leel K
  • 70
  • 1
  • 10
  • The first item isn't a solution. It is the problem :) I don't want to do this every time. The second option - I can't add my file to .gitingore, .gitingore is on the remote, so this will make changes to the remote. – klm123 Mar 14 '21 at 09:11
  • Did you mention the file path for git update-index command ? eg: git update-index --assume-unchanged "resources/application.properties" – Leel K Mar 14 '21 at 10:13