Create and delete remote git branches

Creating and delete local git branches is easy:

Once you have created a local branch, you can push it to the remote. Note: the branch must exist locally.

  • OPTION 1: switch to the branch locally, and then push

  • OPTION 2: stay on the current branch, and when pushing specify the local branch you want to push

    Notice the syntax: you specific the remote repository name origin and then specify the local branch name, colon, the remote branch name. The remote branch can have a different name from the local branch too.

    The proper syntax of the git push command can be seen in option 2; while option 1 is the simplified special case version. The [local branch name]:[remote branch name] part is known as a refspec.

Which brings us to delete remote branches. To delete remote branches, you do the same as above except that you push an empty local branch to the remote branch that you want to delete – effectively nullifying the remote branch! It’s non-intuitive in a way, but I find it very geeky and logical. It’s as though you no longer had a delete command on your file system, and if you wanted to deleted a file you copied /dev/null to it thus deleting it.