Pushing and deleting remote branches are common actions that many Git users need to perform regularly. However, some users may forget how to do so or simply not know the process. Here’s a definitive guide to help you navigate these tasks.

Let’s say you’ve checked out a new branch, committed some amazing changes, and now you need to share this branch with another developer. You can easily push the branch up to a remote repository using the following command:

git push origin newfeature

Here, origin represents your remote name, and newfeature is the name of the branch you want to push up. This is the simplest and most common way to push a branch to a remote repository. However, there’s another method if you prefer a different option. Geoff Lane has created a helpful tutorial explaining how to push a reference to a remote repository, fetch any updates, and then start tracking the branch.

Deleting a remote branch is also a straightforward task, albeit it may feel a bit awkward:

git push origin :newfeature

This command will delete the newfeature branch on the origin remote repository. However, you’ll still need to delete the branch locally using git branch -d newfeature.

If you find yourself frequently performing these actions, there’s a handy script called git-publish-branch created by William Morgan that can automate this process. It also makes deleting remote branches feel more intuitive. If you know of better or easier ways to accomplish the above tasks, feel free to share them in the comments or submit your own tip!