Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge and delete branch in one step/command

Is it possible, to merge a branch and automatically delete it with a single command? The delete step should only be executed if merging was successful.

like image 835
BendEg Avatar asked Feb 19 '16 14:02

BendEg


People also ask

How do I delete a merge branch?

Do git rebase -i <sha before the branches diverged> this will allow you to remove the merge commit and the log will be one single line as you wanted. You can also delete any commits that you do not want any more.

How do I delete a branch in CMD?

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.


2 Answers

This is an old question, but for those who stumble upon it looking for this functionality, you can now add a Git alias to accomplish this. For example, in .gitconfig:

# ...  [alias]   mgd = "!git mg $1 && git br -d $1; #" 

Then, you could run git mgd branch-name to merge and delete a branch in one go. Note that the lowercase -d flag ensures that the branch will only be deleted if it has already been fully merged; thus, you don't have to worry about the first command not working correctly and then losing the branch.

The exclamation point at the beginning of the alias signifies that this is a bash command, and $1 stores the argument that the user passed in (i.e. the name of the branch to merge/delete).

NOTE: Do not forget the comment character (#) at the end of the alias! It will not work without it.

like image 45
Jacob Lockard Avatar answered Sep 20 '22 07:09

Jacob Lockard


No, git doesn't support this at the same time.

However, you can run the commands in a shell conditionally:

git merge source-branch && git branch -d source-branch 

Edit:

-d will only remove merged branches while -D will also remove unmerged branches, so -d will ensure that the branch is merged and you don't delete a branch by accident.

like image 154
Zbynek Vyskovsky - kvr000 Avatar answered Sep 18 '22 07:09

Zbynek Vyskovsky - kvr000