23

I would like to include a command to delete a local Git branch in a script, and I don't want any error message to be shown if the branch does not exist. At the same time, I also don't want a status code indicating a failure from the Git command.

Given the following example:

git branch -D foo

If the branch exists, it is deleted, and the return status of the command is 0, indicating success. If I run the same script again, the branch is no longer there, therefore the command fails, prints

error: branch 'foo' not found.

and the return status of the Git command is >0, indicating an error.

Is there a way to silence the command, so that it does not care whether the branch was there in the first place? Ideally, it would not print an error message and it also would not indicate a failure through a non-zero return status.

I know that I can work around these things using some scripting magic, but I would prefer a simple solution, since I have to do the same thing on Windows (.bat) and for Unix/Linux/Mac (.sh).

Did I miss an option, or am I out of luck?

nwinkler
  • 52,665
  • 21
  • 154
  • 168

4 Answers4

20

If the branch exists, it is deleted, and the return status .. is 0, ... Is there a way to silence the command, so that it does not care whether the branch was there ... it would not print an error message and it also would not indicate a failure through a non-zero return status.

The following examples will suppress all output, and indicate the success or failure via the exit code:

Linux   $ git branch -D <branch> &>/dev/null    
Windows $ git branch -D <branch> 1>nul 2>nul

If you intend to consciously ignore the exit code, simply don’t check it, and proceed to the next command in your script.

Or, If you must exit with a zero code then

Linux   $ git branch -D <branch> &>/dev/null || true  
Windows $ git branch -D <branch> 1>nul 2>nul || ver>nul
Mohit Solanki
  • 2,122
  • 12
  • 20
mockinterface
  • 14,452
  • 5
  • 28
  • 49
  • 8
    Note that if you run your script under bash or sh (and ksh probably?) using "-e" switch, so "exist if any command returns failure", script will terminate. You can prevent it and treat such failing command: "git branch -D &>/dev/null || true". This will execute "true" when git returns error, effectively turning whole statement to successful execution in all cases. – Jędrzej Dudkiewicz Oct 26 '18 at 09:54
4

Try this:

branch="${1:?foo}"  

if git show-ref --verify --quiet "refs/heads/$branch"; then
echo "Branch exists."
.......
chamamo
  • 270
  • 2
  • 12
  • Good point - I could use an if statement, but I would have to do the same for the Windows script as well. I was hoping for some switch I missed on the "git branch" command. – nwinkler Mar 13 '14 at 12:16
2

Use the core command, after the command runs that ref is gone, so it regards the result as successful:

git update-ref -d refs/heads/$branch

Stuff like this is one of the reasons for the "don't use convenience aka porcelain commands for scripting" rule that's so widely ignored.

jthill
  • 55,082
  • 5
  • 77
  • 137
0

For Windows powershell, the following command will delete all local branches except the current branch

git branch | %{ $_.Trim() } | ?{ $_ -notmatch '\*' } | %{ git branch -D $_ }
alfi
  • 371
  • 4
  • 5