0

I need to make changes in files, which are in remote repositories locally and make git ignore those changes when I do git commit/git push.

There are already answers for that: https://stackoverflow.com/a/1753078/747050 https://stackoverflow.com/a/13631525/747050 All of them tell to use

git update-index --skip-worktree <file-name>

When I execute this command and change the file, and then switch the branch git switch master I get an error:

error: Your local changes to the following files would be overwritten by checkout:
        <file-name>
Please commit your changes or stash them before you switch branches.

Neither editing .git/info/exclude file nor git update-index --assume-unchanged doesn't help.

klm123
  • 12,105
  • 14
  • 57
  • 95
  • Just don't add them when commiting? – fredrik Nov 12 '21 at 08:37
  • This happens when the files are not identical between the branches you are switching. The question is: what do you expect to happen? – 1615903 Nov 12 '21 at 08:42
  • 3
    As long as git is tracking that file, git want to keep tracking modifications to that file. The only way to avoid this properly is to remove the file from the repository and add the appropriate filter to gitignore. Hackish solutions like asking git to pretend the file hasn't change has flaws, as you have already observed and there is no easy way to avoid this. The reason this is hard to do right is that the general consensus is that it's the wrong thing to do in the first place, so no good solution has been added. – Lasse V. Karlsen Nov 12 '21 at 08:46
  • 1
    You should consider renaming the files in the repository that you need to do this with to a template file, and then let developers make a copy of this to the file that will be used by your application and change this file, but this file will be ignored by git. – Lasse V. Karlsen Nov 12 '21 at 08:46
  • @1615903, I expect git to ingnore everything what is going on with the file locally, w/o me changing remove repository (i.e. remote .gitignore file) – klm123 Nov 12 '21 at 08:47
  • 2
    @1615903, ah, right, I was sure X and Y are the same and they were not. Once I've merged branches and made them the same skip-worktree is working w/o errors. Thanks a lot! If you make an answer out of it, I will accept it. – klm123 Nov 12 '21 at 08:59

1 Answers1

0

You have branch A in which the file has contents X, branch B in which it has contents Y, and then you have some local changes Z to the file that you have ignored with skip-worktree. When switching from branch A to B, how is git supposed to know what the file should look like afterwards? Should it look like Z, Y, or should it extract some changeset from X and Z, and apply it to Y?

skip-worktree works reasonably well when the tracked contents of the file are identical, but when there's differences, you need manual work when switching branches.

1615903
  • 32,635
  • 12
  • 70
  • 99