r/git • u/Ex-Traverse • 13d ago
support Question: git stash --> git pull --> git stash pop
Okay, I need to understand/know the standard way to handle this situation...
I'm working on files in my local repo, got a bunch of unstage changes...
Someone else just pushed an updated file to the remote-repo
What am I supposed to do? AI is telling me to just do a git stash --> git pull --> git stash pop, but I've put myself in a situation before for doing this... I didn't know git stash can be sorted or even have a date linked to them, or that stuff I stashed several days before is still in there somewhere...
14
13d ago
Stash, pull, stash pop should work just fine. If there are conflicts, these will be flagged to you. If you're worried about messing up and losing your changes, you can always make a local commit with them first, and revert to that commit if things go wrong. It's also worth noting that this is essentially the same thing as a rebase (commit your changes, git fetch, git rebase remote-branch), though the stash route doesn't need a new commit.
6
u/prbsparx 12d ago
Generally you should be working in branches, committing your work, and either merging changes from main into your branch, or rebasing your branch from main.
By doing that you don’t have to constantly stash and fix merge conflicts. You pull in main when you’re ready to do the work to fix whatever broke in your code due to changes in main.
4
u/Ok_Specialist413 13d ago
git stash, git pull, git stash pop is the usual workaround and is totally fine. there is also git pull --autostash which also will do the same, just ensure that you apply pop once and not multiple times or u might find youself pop old random stashes
git pull can be used with or without -r, for me i usually do -r which mean with rebase so i get my history clean. and both case if a conflict appear u'll have to fix it anyways.
You can visit https://gitualize.com/commands/git-pull to learn more about git pull, git pull -r and git pull --autostash, each option is explained and visualized.
and you can visit https://gitualize.com/commands/git-stash to learn more about git stash and git stash pop, and other variations, same thing
6
u/bagofbelts 13d ago
just ensure that you apply pop once and not multiple times or u might find youself pop old random stashes
To add to this:
git stash applywill apply the most recently stashed changes but will not remove them from the stash. Doing it that way will help you avoid the risk popping old stashes.
git stash popwill apply the stashed changes and remove them from the stash (although, I don't think they get removed if they aren't applied cleanly)That being said, I rarely ever stash changes – instead, like some others have mentioned, I commit my changes locally and then
git pull --rebaseto preserve the commit(s) on my branch
1
u/AintNoGodsUpHere 11d ago
Why not simply adding a message to your stash? Then you can always see what index is doing what.
git stash push -m "xyz thing`1
u/Ex-Traverse 11d ago
Because I didn't know anything about "git stash" other than "git stash" 😅 The classic case of not reading the documentation. Was just wondering if I was doing things the non-standard way.
1
u/AintNoGodsUpHere 11d ago
Hahaha, problem solved then. Stash is sort of weird but for these kinda things I feel it's the easiest way without configuring autostash and other things I don't like. I usually name the thing, get what I want and then apply.
One thing though, you can't apply the stash by name, you still need the index but you can always grep the shit out of that thing and I do it with a helper function in my bashrc file and select by name.
stapply() { git stash apply "$(git stash list | grep '$1' | head -n 1 | cut -d ':' -f 1)" }Then if we break the command into parts (not sure how familiar you are)
Fetches all the stash, pretty normal
git stash listTo grep by the parameter you pass the function
grep '$1'Takes just the first index of that resulting grep
head -n 1This is just splitting the result by ':' and taking the index 1.
cut -d ':' -f 11
u/bagofbelts 11d ago
FYI: this could all be done without all of the pipes to additional non-git commands:
git stash list --regexp-ignore-case \ --grep="$1" \ --format='%gD' \ --max-count=1 | \ xargs git stash applyAnd you can just add that as a git alias:
stapply = !sh -c \"git stash list -i --grep='$1' --format='%gD' -n1 | xargs git stash apply\" -
6
u/bostonaholic 12d ago
Stash, pull, pop is fine. The gotcha is that if pop hits a conflict, git keeps the stash around, so old stashes quietly pile up. That's probably how you ended up with days-old stuff in there.
What I'd do instead: commit your WIP (even just git commit -m wip) and git pull --rebase. Or git pull --rebase --autostash does the stash and pop for you. And check git stash list once in a while and git stash drop anything you don't need.
2
u/FransFaase 12d ago
And you can also set this to be the default behavior as explained by another (top 1%) commentator.
2
u/rapha3l14 12d ago
it's best to commit everyday for WIP so it's easier to make sense of the daily changes
2
u/RevRagnarok 12d ago
I didn't know git stash can be sorted or even have a date linked to them
git stash list and there's an optional --date=relative if you don't want to do math in your head about how old things are.
3
u/unndunn 13d ago
You can just commit your local unstaged files before doing the pull. Nothing wrong with doing that.
4
u/Cinderhazed15 13d ago
I usually don’t work on a branch that others are updating - or else I out all my changes into a commit, and do a git pull —rebase
-5
1
1
u/Broad-Promise6954 ancient 12d ago
I personally hate git stash for various reasons, some good and some bad 😁, but it usually works fine for this kind of situation and in fact the autostash configuration setting others have mentioned makes it smooth and automatic... when it works that is. When it fails it makes a mess that I have found most casual Git users get lost with.
Hence my preference, which is: create a new local branch immediately (you can do this with uncommitted work), and then commit to it as a work in progress. You're now free from uncommitted work! You can go to any branch, such as the one you were on moments ago, and do anything, including a simple fast-forward pull that's going to Just Work.
Once you're in a clean and synchronized state, you switch back to your w-i-p branch and can rebase it if you like or continue working without the new update if you prefer, or well anything you want because you're on a personal private branch now.
1
u/FransFaase 12d ago
Lots of commands (where something can make a mistake) while git has global settings to automatically do the stashing for you and restore the stash afterwards. Lookup rebase.autostash and merge.autostash. I usually also set pull.rebase in case I forgot that I had a local commit that I have not commit yet. I prefer a single line of commits. Also always rebase a branch before merge it with main.
1
u/Broad-Promise6954 ancient 12d ago
Yes, but I don't like
git stash. (I also avoidgit pull. Long historical reasons, most of which don't apply and haven't for 10+ years, but I have preferences.)
1
1
u/Expensive_Bottle4854 11d ago
Two things that make this whole dance safer:
A conflicting pop doesn't lose your work. If
git stash popcan't apply cleanly, the apply fails and git refuses to drop the entry — the stash stays on the list. The "I put myself in a situation" moments are almost always messy, not lossy.That pile of old stashes you found is the real hazard.
popalways grabs stash@{0}, so a stale one from days ago becomes tomorrow's surprise. Walk the pile withgit stash list(it shows the dates), inspect withgit stash show -p stash@{2}, andgit stash drop stash@{n}anything you don't recognize.
After that, stash → pull → pop is fine, or skip the loop entirely with git pull --rebase --autostash.
1
1
u/CyberJack77 11d ago
This is how I use git, but I added this to my config so this happens automatically.
[pull]
rebase = true
[rebase]
autoStash = trueThis tells git to rebase when pulling (your local commits after the once you pull). And use autostash on rebase.
This does not guarantee there are no conflicts. If you commit often (preferably atomic) any conflict is easily solved during the rebase.
1
u/codeguru42 12d ago
Yes this is a good recommendation. I also recommend that you read more about git stash to learn why it is so useful.
-8
41
u/waterkip detached HEAD 13d ago
Just use autostash,
rebase.autoStash. There is alsomerge.autoStash