How to Create and Delete a Git Branch

Creating and deleting local and remote development branches

Some of the basic things we may need to do when working on a feature involved creating a development branch and then deleting that branch after merging all the changes.

Here is how we can do that.

Creating a Local Branch

Below is an example of creating a new branch new-dev-branch from the master branch.

git checkout -b new-dev-branch master

Creating a Remote Branch

Creating a local branch does not mean that a branch is also created in the remote repository. There is a new command for that.

git push -u origin new-dev-branch

In this example, I have used the same name for the remote branch as for the local branch, but they can have different names.

Deleting a Local Branch

Once we finished working on the development branch and merged our changes back into the original branch (the master branch in our case) we can delete the local development branch. Here is the command for that.

git branch -d new-dev-branch

The -d option does not delete the local branch if it contains commits that haven’t been merged into other local branches or pushed to the remote repository. To delete the branch even in these cases use the -D option.

Deleting a Remote Branch

Here is the git command for deleting the new-dev-branch remote branch.

git push origin --delete new-dev-branch

That’s all. Thanks for reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store