0

In a large project with many independent files, I have several local changes. Now I want to update to the latest upstream version, keeping my local changes. I already know that my local changes are completely distinct from any upstream changes.

I know I could do this:

git stash
git pull --rebase
git stash pop

I don't see any reason though to first stash the local changes and then restore them again. It is pretty easy for git to see that the upstream changes will not affect any local changes, so that the upstream changes could be applied directly. This would mean I'd only have to:

git pull --rebase

Did I just miss the relevant git command line options? The manual mentions the --autostash option, but that's not what I want. I want the locally changed files to stay completely unmodified and untouched since their timestamps are relevant.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Related https://stackoverflow.com/a/34438903/1084174 – Sazzad Hissain Khan May 13 '20 at 06:59
  • @SazzadHissainKhan thanks for the `pull --rebase` hint, I integrated it into my question. My main question is different though. – Roland Illig May 13 '20 at 07:02
  • "*It is pretty easy for git to see that the upstream changes will not affect any local changes*" If it's so easy you can send a pull request to git authors. If not — stash/unstash is the only way and `--autostash` is the best option. – phd May 13 '20 at 09:23

2 Answers2

0

Now I want to update to the latest upstream version, keeping my local changes. I already know that my local changes are completely distinct from any upstream changes.

Presumably you are on a branch while the "latest upstream version" is on master. What I do in that situation is commit the branch and then pull master and rebase the branch on top of it. I do that frequently while working on a long-lived branch.

matt
  • 515,959
  • 87
  • 875
  • 1,141
-2

After trying a few more options, I finally settled on this:

git reset --soft origin/master

This works without asking any questions. And if there are any files that are modified both in the local tree and in the commits, git will abort and tell you the conflicting file so that you can run git restore on it.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • It might be obvious when one thinks about it, and you shouldn't enter commands without knowing what they do, but if you have proper commits in your local history, this will trash them and leave all the local content that isn't in the remote staged. – pentavalentcarbon Jun 28 '23 at 17:20