Keeping local files with --skip-worktree flag intact on git pull

176 views Asked by At

I have a similar situation like this: Link Basically, I have a file called settings_data.json in my repository. I have --skip-worktree flag set on this file, so that if I make changes on my local machine, I don't accidentally push those changes to production. This is a normal approach for a situation like this, I understand. The problem is, that this file also changes on production independently. In this situation, when I make a pull, I don't want to pull changes for this file on my local repository. I want to completely ignore it. Is there an opinionated approach for a situation like this?

EDIT: I found a "partial solution" based on this answer. I wrote a bash script for git alias, that pulls from remote without comitting, then check out files that has conflict and finally commit changes. But this also pulls changes that I don't want to have locally. I still need something to completely ignore files I don't want to change locally. This is my alias: pullbase = "!f() { git pull base main --no-commit; git diff --name-only --diff-filter=U --relative | xargs git checkout HEAD; git commit --no-edit; }; f"

2

There are 2 answers

0
bmandl On BEST ANSWER

I think I finally found a solution for my specific case. This is my git alias: pullbase = "!f() { git pull base main --no-commit; git diff --name-only --diff-filter=M --stat base/main..HEAD templates config/settings_data.json | xargs git checkout HEAD; git log --pretty=format:'%s' -n 1 base/main | git commit -F -; }; f"

Basiscally what I do is I pull changes from my "base" repository, I check out files that are different between HEAD and remote, then I checkout to HEAD only files I don't want to update. Lastly, I use last commit message from remote and commit my local repo. I hope someone finds this as usefull.

0
torek On

You can't. (You can only get fairly close.)

Seriously, this isn't what --skip-worktree is for; it's for sparse checkout. It can be abused this way, or used the way it's designed (via sparse checkout).

This is a normal approach for a situation like this, I understand.

No, it's not. This is a Git Frequently Asked Question, specifically this one:

How do I tell Git to ignore tracked files?

You're using "Option c":

  • Option c: Make Git ignore changes to the configuration file. There is no automatic centralized way to do that. But on a per-clone basis you can tell Git to do it using either git update-index --assume-unchanged <file> or sparse checkout (see the corresponding section in git-read-tree(1) for details). Note that neither feature was designed for that purpose. Use at own risk.

(emphasis theirs, i.e., the caveat is part of the answer to the frequently asked question).

(I prefer Option "a" myself, whenever it can be used; Option "b" is also good. Option "c" is terrible: use it only as a last resort.)