Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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 $?
}
share|improve this answer

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!'
share|improve this answer
    
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 '13 at 12:45

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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