Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 615
Suleman Ahmad Avatar asked Jul 24 '13 11:07

Suleman Ahmad


People also ask

What is fetch prune in git?

git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.

Should I use git pull or fetch?

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.

How does git fetch work?

The git fetch command downloads objects to the local machine without overwriting existing local code in the current branch. The command pulls a record of remote repository changes, allowing insight into progress history before adjustments.


1 Answers

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.

like image 148
poke Avatar answered Sep 18 '22 04:09

poke