0

I would like to update my local branch by pulling latest changes from the remote master. The only caveat is that I want to leave a single file as it is in my current local branch.

NOTE:

  1. The file is NOT different from what I have at the tip of the current branch.
  2. The file is NOT a config file.
Rebel
  • 472
  • 8
  • 25
  • 2
    Is the file different from what you have right now at the tip of the current branch? In other words, if you run `git diff the-file`, does git provide anything there? – eftshift0 Aug 23 '23 at 19:45
  • 3
    Is this a config file and you'd like to preserve the specific contents even if the upstream version changes? – Jim Redmond Aug 23 '23 at 19:50
  • 2
    Given that the file is not different from what you have in `HEAD`, then just allow the operation to go and then run `git checkout -- the-file`, and you have the file just like you had it there. Depending on the operation, `` could be replaced with `HEAD@{1}`. – eftshift0 Aug 23 '23 at 20:09
  • 1
    Based on the edit, @eftshift0's comment should probably be made into the answer. – TTT Aug 24 '23 at 02:03
  • 1
    Following clamor from the general public asking for it (@TTT ;-)), I have turned it into an answer. – eftshift0 Aug 25 '23 at 13:13

2 Answers2

2

Stash the file, pull the latest, and the pop the stashed file.

If everyone who works in the repo generally has the same ongoing need because the file has user settings or something, consider listing the file in the .gitignore file for the repo.

Caleb
  • 124,013
  • 19
  • 183
  • 272
2

Given that the file is not different from what you have in HEAD, then just allow the operation to go on and then run git checkout <commit-where-I-was> -- the-file or, these days, git restore --source=<commit-where-I-was> -- the-file, and you have the file just like you had it there in the original commit where you were. Depending on the operation, <commit-where-I-was> could be replaced with HEAD@{1}.

eftshift0
  • 26,375
  • 3
  • 36
  • 60