0

I ran git fetch origin and similar commands before, which fetched all of my coworkers' branches. However, I don't need them. I need only master and my branches. If I run git branch, it lists the branches that I created on my machine. For each of these local branches, there's an equivalent branch upstream. How can I delete the local copies of all the other branches?

A related question is, how can I git fetch all of my branches without fetching my coworkers' branches?

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • Possible duplicate of [How do you create a remote Git branch?](https://stackoverflow.com/questions/1519006/how-do-you-create-a-remote-git-branch) – tangoal Oct 08 '18 at 22:53

1 Answers1

0

Delete your remote tracking branches

Individually run git branch -rd origin/branchIDontWant for each branch, or if there are many you can blow them all away with git for-each-ref --format "%(refname)" refs/remotes/origin | xargs -I% git update-ref -d "%".

Fetch desired branches

You could only fetch (or pull) the branches that you want (git fetch origin branchIWant) going forward, or configure git fetch to only fetch desired branches from the remote.

To configure git fetch to only pull branches that you desire, you'll need to update your configs for the remote you're pulling from. This is described in more detail here: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec. You will want to remove the default refspec fetch = +refs/heads/*:refs/remotes/origin/* from your .git/config for the repository. Then, you'll add a refspec fetch = +refs/heads/branchIWant:refs/remotes/origin/branchIWant for each branch you want fetched (branchIWant).

Keep in mind I'm assuming you're only using a single remote called origin, repeat above steps for more remotes.

John
  • 2,395
  • 15
  • 21