-1
error: Your local changes to the following files would be overwritten by merge:
        bootstrap/cache/services.php
Please commit your changes or stash them before you merge.
Aborting

I discard the change, then again it automatically changes added, how to fix it ?

Jafar
  • 1

3 Answers3

1

If you are discarding your change, but it then gets quickly re-written, then something outside of Git must be modifying the file. Since the file happens to be in a directory called "cache" my guess is you probably shouldn't be tracking that folder at all. (I recommend confirming this first.) If you add that cache folder to your .gitignore, and also delete it and commit the deletion so you no longer track it, then the next time it's auto-created and/or updated it will be properly ignored.

Update: this question and its answers provide additional info on how to handle the cache directory, if it's not currently ignored.

TTT
  • 22,611
  • 8
  • 63
  • 69
  • I already added cache to .gitignore, but it didn't affect, – Jafar Jan 02 '23 at 06:21
  • ```* !.gitignore``` in cache folder – Jafar Jan 02 '23 at 06:22
  • 1
    @Jafar: "and also delete it and commit the deletion" roughly means : run `cd cache; git rm *; git commit` in your repository – LeGEC Jan 02 '23 at 12:07
  • @Jafar right- the `.gitignore` file only works on untracked files, but it looks like you accidentally committed the file already. (This was what LeGEC was pointing out in my answer.) I just added a link which discusses this folder specifically, with answers including instructions on how to untrack and ignore a previously tracked file. – TTT Jan 03 '23 at 04:37
0

If you don't want to keep your local changes, use the command :

git reset --hard HEAD

This will discard all your local changes. Then you shouldn't face any issues.

pr0to
  • 246
  • 2
  • 9
  • but after a few seconds automatically change the bootstrap/cache/services.php file – Jafar Jan 02 '23 at 04:58
  • That can't happen unless the files were not originally being tracked by git. In that case, the next time you use `git add`, they will added back. To stop this from happening, use the command `git clean -f` right after `git reset --hard HEAD`. – pr0to Jan 02 '23 at 05:08
0

From your comments it is clear that you don't want to track that file. once the file is tracked(added to index), you cannot ignore the file even if you add the file to .gitignore.

to delete the file in remote and do not not track again, add it to .gitignore and execute the following command.

git rm --cached bootstrap/cache/services.php

now the file is not tracked and adding it to .gitignore will make sure that the file changes are never pushed again.

after this you can execute git pull.

Harish Barma
  • 624
  • 8
  • 15