2

I have a script (called update_content.sh) that is supposed to run 3 consecutive git commands and the first version looked like this:

#!/bin/sh
git fetch --all && git reset --hard && git merge

When I run it I see this output:

web@bane:~# ./update_content.sh
Fetching origin
error: unable to create file locales/fr.json (File exists)
error: unable to create file locales/it.json (File exists)
fatal: Could not reset index file to revision 'HEAD'.

However when I changed it to this:

#!/bin/sh
git fetch --all
git reset --hard
git merge

And run it I see this:

web@bane:~# ./update_content.sh
Fetching origin
HEAD is now at 5859b2e Added Business tab
Already up-to-date.

The newline approach gets the result I want however it does not have the "don't proceed if a step failed" quality of the &&. Is there something about how git works that makes it incompatible with the && or am I missing something about &&?

I did a little more digging and found that the one liner with && when run from the command line doesn't throw errors but also doesn't actually reset the local working copy, however the new line approach inside a script (and run one at a time) does.

3
  • shouldn't your question be on unix.SE though?
    – joH1
    Aug 9, 2017 at 17:35
  • I can move it if you think it has a better shot over there Aug 9, 2017 at 17:36
  • I have to wait 40 minutes before the other stackexchange site will let me post, Aug 9, 2017 at 17:41

1 Answer 1

3

You may as well create an alias to chain all the commands and then execute the alias object itself.

git config alias.op '!git fetch --all && git reset --hard && git merge'
git op

&& only runs the second command if the first one exited with status 0 (was successful). ; runs both the commands, even if the first one exits with a non zero status.

Your example with && can be equivalently paraphrased as

if git fetch --all ; then
    if git reset --hard; then
       git merge
    fi
fi
6
  • I understand this aspect of &&, the strange thing to me at least is why when I run the 3 commands with && then git reset --hard is failing, however when I run them in their own lines the same command is succeeding (not producing any error, also I tried ; and the reset command still failed (though because of ; it now attempted to run git merge) Aug 9, 2017 at 17:40
  • It is because while the first process of fetching the branch is running, the next command git reset --hard gets executed simultaneously which in turn results into a conflict. Aug 9, 2017 at 17:43
  • I don't think && runs simultaneously (stackoverflow.com/questions/4510640/…) Aug 9, 2017 at 17:45
  • My bad! Please check the updates and check if that works out for you. Aug 9, 2017 at 18:02
  • I had no idea that was possible. I'll try it but I don't like the idea of configuring git in that way, it seems like a config option like that would get lost as this project changed hands. Still a fascinating approach. Aug 9, 2017 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .