Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git alias to delete local and remote

I'm trying to write an alias to delete both a local and remote branch at the same time, but I can't figure out why the syntax is not working. In ~/.gitconfig, I've tried the following aliases, but each produces the same result, which is unexpected:

[alias]
     nuke = !sh -c 'git branch -D $1 && git push origin :$1'

and

[alias]
     nuke = !git branch -D $1 && git push origin :$1

both produce:

$> git branch
  * master
  mybranch
$> git nuke mybranch
Everything up-to-date
$> git branch
  * master
  mybranch

Switching the order of the commands produces a different result, but also not entirely what I'm looking for:

[alias]
    nuke = !git push origin :$1 && git branch -D $1

...

$> git branch
  * master
  mybranch
$> git nuke mybranch
Everything up-to-date
Deleted branch mybranch (was d719895)
$> git branch
  * master
$> git push origin :mybranch
To [email protected]:biegel/repo.git
 - [deleted]         mybranch

When I run that command directly on the shell, it works nicely:

$> git branch
* master
  mybranch
$> git branch -D mybranch && git push origin :mybranch
Deleted branch mybranch (was d719895
To [email protected]:biegel/repo.git
 - [deleted]         mybranch
$> git branch
* master

I've tried creating an alias in ~/.bashrc, using git push origin --delete $1 and using a shell function with !f() { }; and nothing seems to take!

I'm ready to give up. Any thoughts on what I'm missing here?

Thanks.

like image 976
biegel Avatar asked May 24 '13 17:05

biegel


People also ask

How do I delete both a local and remote branch?

So, to delete the remote branch AND locally-stored remote-tracking branch in one command, just use git push origin --delete <branch> . Then, you just need to delete the local branch with git branch -D branch . That covers the deletion of all 3 branches with only 2 commands.

How do I delete a local branch if the remote branch is deleted?

First, use the git branch -a command to display all branches (both local and remote). Next, you can delete the local branch, using the git branch -d command, followed by the name of the branch you want to delete.

How do I delete my local branch in git?

The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.

Can we delete remote branch in git?

Steps to delete remote Git branches Issue the git push origin –delete branch-name command, or use the vendor's online UI to perform a branch deletion. After the remote branch is deleted, then delete the remote tracking branch with the git fetch origin –prune command.


1 Answers

You can make this work just fine. You just need to add a missing '-' at the end of your definition. The '-' will signal to bash that all option processing is done, and anything that comes after becomes a parameter you can reference via $1, $2, etc:

[alias]
     nuke = !sh -c 'git branch -D $1 && git push origin :$1' -

From the command line, switch to another branch, then run the command:

git nuke branch-name

Alternately… If you are unable to add the above to your .gitconfig file for some reason, but have access to the .bashrc, .bash_profile, etc… you can add the following:

git config --global alias.nuke '!sh -c "git branch -D $1 && git push origin :$1" -'
like image 126
John Szakmeister Avatar answered Sep 25 '22 11:09

John Szakmeister