Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a branch without an error message if the branch does not exist?

Tags:

git

branch

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?

like image 772
nwinkler Avatar asked Mar 13 '14 10:03

nwinkler


People also ask

How do I force delete a branch?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

Why can't I delete my git branch?

If you are sure you want to delete it, run ' git branch -D issue-5632 '. This error is caused because we have some un-merged changes in branch issue-5632 due to which the branch delete has failed. In such case either you can delete the branch forcefully or merge the changes and then perform the delete operation.

How do I force delete a remote branch?

Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .

How do you delete a branch which is not measured to any other branch?

Click the Branches button at the top of the sheet. Select a branch and click the minus button at the bottom to remove the branch. Note that Xcode does not let you remove the current branch. Apple moved the user interface for branches to the source control navigator in Xcode 9.


1 Answers

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
like image 68
mockinterface Avatar answered Sep 24 '22 19:09

mockinterface