-1

Some git commands can give exit code 1 while it was actually successful, or for instance if I try to git commit -m <something> but there's nothing to commit, or trying git pull origin master while there're no changes and my local branch is up to date with the remote.

E.g: "Commit failed - exit code 1 received" when trying to commit a new local reprository in gihub desktop

How then to check that the git commands didn't actually fail and that it just says "nothing there to do"

islamhamdi
  • 207
  • 2
  • 8
  • 5
    In general, the thing to do is to not use commands that do that. Use commands that produce sensible exit status values instead. For instance, never run `git pull` from a *program*. The pull command runs `git fetch` first, so run that first. It then runs a second command of the user's choice. Determine *your* choice (which may not be the user's choice) and run *that* command directly. – torek Jan 11 '21 at 22:11
  • 1
    `but there's nothing to commit` So check if there's something to commit first, then `git commit` – KamilCuk Jan 11 '21 at 22:15
  • 2
    @torek why not make that an answer? It's the answer. porcelain commands, what I call convenience commands, are there for human convenience. plumbing commands, what I call core commands, are for scripting and for things that are rarely enough needed to not be worth providing a handy shortcut for. – jthill Jan 11 '21 at 22:15

1 Answers1

0

The output when you commit, when there is nothing to commit, could be stored in a variable. When you have something to commit, you could store the output in a variable as well.

Afterwards you could use an if statement to test if one of the variables, which stored the output of, there was nothing to commit. Which would be unique and would give your custom output of, There was no code to commit. Now the output of the variable which has code to commit, can be tested and you can have a custom output for this also.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5