3

My usual git workflow for deploying is the following:

[while on master branch]

git pull
git push
git checkout production
git pull
git merge master
git push

Would it be possible to create a function for executing all these commands, but if one of them returns an error (i.e. merge conflict), it stops right there?

3 Answers 3

6

Use && which will only action the following command if the previous command completed successfully. $? returns the status command of the most recent command.

function git_checkout()
{
 git pull && git push && git checkout production && git pull && git merge master && git push
 return $?
}
3

Setting -e in bash will cause it to exit if it sees a non-zero exit code in the last command in a command chain.

#!/bin/bash -e
# or `set -e`
false
echo 'I will never run!'
1
  • The thing is that I would prefer it as a function, so I don't have to create a separate file for it. Thank you for your reply anyway :)
    – nuc
    Jun 27, 2013 at 12:45
1

Use ERR_EXIT which will cause your script to exit on any error:

emulate -L zsh # do not change the environment outside the script

# If a command has a non-zero exit status, execute the ZERR  trap,
# if set, and exit.
setopt ERR_EXIT

cd /jhkljhlk   # <-- file does not exit and ls gives an error
echo "this will not run"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.