1

Usually when I run:

git stash push --all

if I don't have any modification, nothing is pushed on the stash. Would it be possible to force a push even in this case, so that git stash push --all && git stash pop always gives the identity?

I'm asking because in some script I want to stash push, do some stuff, and stash pop, and if the stash push don't create a new stash, stash pop may pop older and unrelated stash. Another solution would be to detect from GitPython that no stash happened (I would say by comparing the output of two consecutives repo.git.stash("list")), but it would also complicates a bit the code.

Thank you!

tobiasBora
  • 1,542
  • 14
  • 23
  • 1
    See https://stackoverflow.com/q/28635989/1256452 and https://stackoverflow.com/q/48904806/1256452 (not precisely duplicates but achieves your goal) – torek Jan 23 '19 at 20:31

2 Answers2

0

General use of git stash is to store changed data that you don't want to commit or back to this changes later when u swap to another branch for example. So if you have not any uncommitted changes how do you want to stash something? I think it's problem with your behaviour with changes. Easier way to get the same result is reset repository/branch state to actual commit and then stash pop. I mean i would recommend you something like this. git stash -> do some stuff -> git reset --hard (this discard all local changes, but won't clear stash) and then use git pop to get your state before "do some stuff".

dunajski
  • 381
  • 3
  • 15
  • 1
    Thanks for the answer. But actually, that's my point, I'd like to do an "empty stash" just to treat "no modification" exactly like "modifications" in my script (don't want to parse `git stash list` output to count the number of stash...). It's possible to do an empty commit, why not an empty stash? Because in your example, the problem is exactly like in mine: in case of no modification, `git stash` will do nothing, and the last `git pop` may pop an older (not empty) stash. – tobiasBora Jan 23 '19 at 19:45
  • So.. If I have problem like you, I'd make aliases for special uses git pop and git stash. In short concatenate and `git stash` with adding file to repository adding file to version control and similarly for `git pop` concatenate git pop and removing file from version control and from repository. Thats the easiest (for me) solution but making unnecessary moves with repository. You should try this solution and wait for better one. Do you know how to perform this? – dunajski Jan 23 '19 at 20:09
0

In addition to the comments by torek, even if this does not solve my exact initial question, it was enough for my problem: I'd like to point out the function of GitPython repo.is_dirty(untracked_files=True) that is usefull to check if you have anything to stash push --all. That way I can save this value for later and do not stash pop if I didn't stash push --all before.

NB: it seems to work so far, but maybe I missed some important options that may have importance in some case, so let me know if it's the case!

tobiasBora
  • 1,542
  • 14
  • 23
  • suppose you do manually `git stash push --all`, your script fails a push because empty, but later detects there is something and then happily does `git stash pop`. It would seem to succeed, but it actually messes up. – Rax Jan 24 '22 at 18:46