What does 'git fetch -p' (or --prune) mean

git

Solution

When you fetch a remote repository, say “origin”, you will get remote branches for each branch that exists on that remote repository. Those branches are locally stored as `<remote>/<branch>`.

So assume origin has branches `master`, `featureX` and `featureY`. Then after fetching the following “remote branches” exist in your local repository: `origin/master`, `origin/featureX` and `origin/featureY`.

Now, imagine someone else merges `featureX` into master and removes the feature branch from the remote repository. Then origin only has two branches `master` and `featureY`.

However, when you fetch, the three remote branches will all still exist, including the one that was deleted in the remote repository. This is to prevent you from accidentally losing the branch (imagine someone accidentally removed the branch from the remote, then everyone who fetched from it would also lose it, making it hard to restore it).

Instead, you will need to tell the fetch command to prune any branches that no longer exist on the remote branch. So by executing `git fetch --prune origin` or `git fetch -p` the remote branch `origin/featureX` will be removed too.

Btw. if you want to remove a branch from a remote repository, you will have to push an “empty” branch to it, e.g. `git push origin :branchname` will remove the remote branch `origin/branchname` both locally and on the remote itself.

Problem

I had run this command with misunderstandings while I was deleting my one local branch, ``` git branch -D branch-name git fetch -p ``` but I have seen their is a list of branch names that are showing to be deleted. I was afraid to see the list of deleted branches list, and thinking might be I had executed wrong command, and accidentally deleted all the branches!! What does mean of this command (`git fetch -p`). Any idea?

Original source