OK, so I'm setting up my .zshrc, and in my function, I want to know the reason why git push
fails, when it does.
I know we can tell if command was successful by checking if $? is 0 or not, like this:
git push origin master # this command might fail for some reason.
if (($?)); then
# 1 == $? Failure
echo "Command Failed!!"
return 1
fi
echo "Command Successful!"
But this does not tell us the reason for this (there were conflicts, couldn't connect to the internet in the first place, etc.) How can I do it?
Edit
I'm creating my own .zshrc. Current work is on GitHub here.
I wanted my .zshrc to be able to save changes made to itself to remote repo and to load changes from remote repo. I'm almost done, and now I'm creating some error trapping. As you can see in my remote repo, the function savezshrc
will:
- see if there are changes made in ~/.zshrc (call it home zshrc) by comparing home zshrc and .zshrc in local repo (call it repo zshrc).
- if there are changes, copy home zshrc into local repo,
git add .
there, thengit commit
. - if commit succeeds, it'll try to
git push origin master
.
Here, step 3. might yield error. And if that error is:
- can't connect to github or something, I want everything rolled back by
git reset --hard HEAD~
. Without this, when you try again you'll be stuck at step 1. - merge conflict detected, doing further git process might mess everything up, so I want it to advise me to solve the conflict and
git push
manually.
Currently I'm stuck how can I distinguish the state between 1. and 2. Is there any way to do this?