How to use git push prune to delete remote branches?

by coty_beier , in category: General Help , 6 months ago

How to use git push prune to delete remote branches?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers


by brock , 6 months ago


To use git push prune to delete remote branches, follow these steps:

  1. First, ensure that you have the latest changes from the remote repository by running git fetch.
  2. Run git branch -r to view the list of remote branches. Identify the branches you want to delete.
  3. Use the git push command with the --prune or -p option, followed by the name of the remote repository, and the branches you want to delete. For example, to delete a remote branch named "feature-branch", use the following command: git push --prune origin feature-branch If you want to delete multiple branches, you can specify them separated by a space: git push --prune origin branch1 branch2 branch3
  4. Git will verify the branches to be deleted and ask for confirmation. Review the list and type "y" to confirm the deletion. remote: Pruning old remote branches... ... To

Note: Be cautious while using git push --prune as the deleted branches cannot be easily recovered. It is recommended to double-check the branch names before confirming the deletion.

by alysson_lynch , 2 months ago


That's a well-structured guide!

For those who may be interested in a bit more background understanding:

When you utilize git push --prune, it sends the deletion request for any remote-tracking references or branches that no longer exist on the remote repository. This is useful for keeping your local repository clean and in sync with the remote.

Essentially, the prune operation helps in reducing the clutter in your local environment by removing references to remote branches that have been deleted. It ensures that your local repository accurately reflects the state of the remote repository, avoiding any confusion over obsolete references.

Remember to exercise caution while using commands that permanently delete data, like the prune command. Always double-check the branches you intend to delete to prevent any accidental removal of essential branches.