1

I am behind of master of one commit. I know the commit only affects file A. I have a local change of file B. Even B will not be changed, git still won't pull but let me do stash or discard local changes. Is there any way to pull from master without committing or stashing or resetting file B.

Now I have to stash it and pop it back. I am not sure but I remember years ago I can just pull head with irrelevant local changes. Did I miss some settings?

K--
  • 659
  • 1
  • 7
  • 18
  • 2
    Why don't you want to stash? That is what it is used for (among other things). – eftshift0 Jul 05 '21 at 01:19
  • 1
    Does this answer your question? [Git Pull While Ignoring Local Changes?](https://stackoverflow.com/questions/4157189/git-pull-while-ignoring-local-changes) – Obsidian Age Jul 05 '21 at 01:21
  • 1
    From memory, _maybe_ you are allowed to merge if the files you have modified in your working tree are not affected by the merge.... but I wouldn't bet on it. Anyway, can you add the commands you are using and their output in the question? – eftshift0 Jul 05 '21 at 01:22
  • It's true that in ancient versions of Git, `git pull` would *sometimes* work even with uncommitted changes. However, in ancient versions of Git, `git pull` would sometimes destroy uncommitted work. It's a tradeoff, you can have a 100% working Git that doesn't let you do this, or a buggy ancient Git that does but loses your files. :-) – torek Jul 05 '21 at 12:43
  • More seriously, the "autostash" mode (rebase only) does work. I dislike it for the same reason I dislike `git stash`: it's overly fragile. If the rebase goes wrong, the stash doesn't get auto-unstashed (in at least some versions of Git, maybe all, I have not checked lately). The new autostash-with-merge also works, but I'd expect the same caveat (if the merge stops, you have to finish it manually, after which I imagine you have to unstash manually too). – torek Jul 05 '21 at 12:44
  • Thanks for all comments. As a user I don't quite understand the backend of Git. To me, to merge new commits, I expect git just apply diffs on files that related. And if the stash-merge-pop is the only shortest way to achieve that, I'd like a simple in-line solution for non-engineers. Just like I can copy-paste files between folders with keyboard shortcuts but most of the time I just drag them with my mouse. It is just more intuitive. – K-- Jul 13 '21 at 01:11
  • Does this answer your question? [Can "git pull" automatically stash and pop pending changes?](https://stackoverflow.com/questions/30208928/can-git-pull-automatically-stash-and-pop-pending-changes) – Lucius Hu Aug 04 '22 at 22:58

1 Answers1

3

You can git fetch with active changes but git pull needs to merge or rebase, and therefore needs a clean working tree.

There is shortcut though, git pull --rebase --autostash will automatically stash, pull, and apply the stash back. There are more details in this other question.

Mathieu Rene
  • 924
  • 5
  • 11
  • Thanks, this does solve my problem. I wonder why the rebase is necessary since If I stash by myself I won't add rebase here. Well as I never commits, just pulls, I'm ok with this option. – K-- Jul 13 '21 at 01:13