28

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?

1

4 Answers 4

27

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
Sign up to request clarification or add additional context in comments.

1 Comment

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 <branch> &>/dev/null || true". This will execute "true" when git returns error, effectively turning whole statement to successful execution in all cases.
4

Try this:

branch="${1:?foo}"  

if git show-ref --verify --quiet "refs/heads/$branch"; then
echo "Branch exists."
.......

1 Comment

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.
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.

Comments

0

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

git branch | %{ $_.Trim() } | ?{ $_ -notmatch '\*' } | %{ git branch -D $_ }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.