Showing posts with label GIT Delete Branch. Show all posts
Showing posts with label GIT Delete Branch. Show all posts

Friday, 28 December 2018

GIT: How can I delete branches in Git?

Before we look at deleting remote branches, let's discuss the syntax for deleting a local branch in Git.

Deleting local branches in Git

$ git branch -d feature/login
Using the "-d" flag, you tell "git branch" which item you want to delete.
Note that you might also need the "-f" flag if you're trying to delete a branch that contains unmerged changes. Use this option with care because it makes losing data very easy.

Deleting remote branches in Git

To delete a remote branch, we do not use the "git branch" command - but instead "git push" with the "--delete" flag:
$ git push origin --delete feature/login

Deleting both a local and a remote branch

Just a side note: please keep in mind that local and remote branches actually have nothing to do with each other. They are completely separate objects in Git.
Even if you've established a tracking connection (which you should for most scenarios), this still does not mean that deleting one would delete the other, too!
If you want any branch item to be deleted, you need to delete it explicitly.

Tuesday, 25 September 2018

Delete a local and a remote GIT branch

Have you ever gone through a few sprints of work, created several local and remote branches and wondering how to get rid of them (both local and remote branches)?
Here some tips to do that:

Delete a Local GIT branch

To delete the local GIT branch we can try one of the following commands:
git branch -d branch_name
git branch -D branch_name
as you can see above, we have 2 different argument, one with ‘d’ and one with ‘D’.
The -d option stands for --delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches.
The -D option stands for --delete --force, which deletes the branch regardless of its push and merge status, so be careful using this one!

Delete a remote GIT branch

To delete a remote branch you can use the following command:
git push <remote_name> --delete <branch_name>
Alternatively, there is this other following option too, which might be just a bit hard to remember:
$ git push <remote_name> :<branch_name>
These top options can also be used if you want to delete a “tag”.

Push to remote branch and delete

If you ever want to push your local branch to remote and delete your local, you can use git push with the -d option as an alias for --delete.