Git alias with two commands (stash pop + merge) executes only the first command. Why? How to execute also the merge?
Asked Answered
A

1

8

I set up a git alias like this:

git config --global alias.popmerge '!git stash pop && git merge master'

Then I call it, like this:

git popmerge

The "git stash pop" is executed, but the "git merge master" is ignored.

If I run "git merge master" right after the "git popmerge"... it sumply runs as expected, performing the merge.

I have other aliases with long sequences of commands... and they run flawlessly. It seems something at "git stash pop" makes the alias process to halt... Is it possible to avoid this behavior? How?

Thanks.

Apograph answered 25/5, 2011 at 15:17 Comment(3)
Are you sure, you first want to make your working directory dirty using stash pop and then do a merge? Wouldn't it be safer to first do the merge, then pop? Also, I am not sure ignoring the exit status, accepted as an answer, is really desirable. Doesn't failure in stash pop mean, there were conflicts? Do you really want to increase the mess doing another conflict-prone merge?Erlanger
Yes, I am sure. The sole stash save / stash pop purpose is ONLY to allow the checkout master command in this sequence to be always successfull. I want the stash stack to be empty, and the stash pop to be always successfull. The exit status of a successfull stash pop is not zero. Don't ask me why. That's why I needed the ; workaround. Consider the chances of conflict coming from a pop immediately after save: it is zero, in this context.Apograph
ok, didn't know the save was based exactly on the same commit. Then, the only conflicts to be expected are from merging master to dev - fine. Too bad git command exit status behaviour is not really documented...Erlanger
P
8

Have you checked the exit code from stash pop?

&& implies that the subsequent list is only executed if the exitcode is 0 (success).

You can simply ignore the exitcode by using ; instead of &&.


Verify the success by using stuff like:

true  && echo ok || echo fail   # echoes "ok"

false && echo ok || echo fail   # echoes "fail"
Polecat answered 25/5, 2011 at 15:27 Comment(2)
Great. As suggested, I changed && to ; and it worked. Thanks also for the explanation.Apograph
@J.Bruni and @LeifWickland: Thanks! i like the attention to detail :)Polecat

© 2022 - 2024 — McMap. All rights reserved.