137

I've accidentally pruned some remote branches and I don't really know what the consequence of this is (I clicked the "Prune remote branches" button in Git Extensions, thinking it would delete a remote branch).

The official documentation says "git-prune - Prune all unreachable objects from the object database ". I don't really understand what this means. I'm guessing this might have removed merged branches but I'm not really sure.

2

4 Answers 4

136

"Prune remote branches" in Git Extensions executes git remote prune command, which removes your local remote tracking branches where the branch no longer exists on the remote.

See here: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-empruneem

Deletes stale references associated with <name>. By default, stale remote-tracking branches under <name> are deleted, but depending on global configuration and the configuration of the remote we might even prune local tags that haven’t been pushed there. Equivalent to git fetch --prune <name>, except that no new references will be fetched.

See the PRUNING section of git-fetch for what it’ll prune depending on various configuration.

With --dry-run option, report what branches would be pruned, but do not actually prune them.

4
  • so it basically gives priority to your remote and adjust your local to match your remote branches?
    – mfaani
    Oct 2, 2017 at 14:04
  • 4
    @Honey no, it just removes the local remote tracking branches, normal branches are not touched.
    – 1615903
    Oct 3, 2017 at 4:19
  • 3
    The complete command is git remote prune <name> where <name> is the name of the remote (usually origin).
    – Johan Maes
    Oct 28, 2021 at 6:30
  • This could potentially result in loss of access to the commits in those branches if they were not merged to another branch. A warning would be in order.
    – ADTC
    Nov 5, 2021 at 10:23
42

This just garbage collects your branches.

Thats means, if an object (a commit) cannot be reached in any of your branch's ancestors, it will be removed for the git database, and as such couldn't be reach anymore.

This just cleans up a little the git repository and make it lighter.

2
  • 5
    According to the documentation, for house keeping tasks such as this it may be better to do a git gc rather than a git prune which will essentially do a git prune as well.
    – kovac
    Mar 6, 2018 at 7:36
  • 4
    Downvote: this answer (and the question for that matter) confuses git prune and git remote prune. They are not the same, the latter actually clears up branches which no longer exist on the remote while leaving local branches untouched.
    – JBert
    Jan 9, 2020 at 16:58
21

It's important to know that prune is repo-bound. Not everyone knows that you can link your local repo to multiple remotes. It comes in handy when, for example, you work with an open-source project and is enforced to work via forks.

So, prune command requires a repo name. In most cases it's git remote prune origin, but you can call your repo anything, it doesn't have to be origin.

3

There may be remote feature branches which are removed after we merge them into master. We might have deleted the feature branches as a way of cleaning up. But if you had checked out the deleted branch to the local system and set to status as tracking, git pull will not delete those local branches (because those are already disconnected from the server). To clean up that kind of local orphan branches, git prune command will come handy to help.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.