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?
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.
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With