1

I performed a fetch from GitHub, so that I may inspect any changes. This made a remotes/origin/master branch on my local machine that I could checkout, look around in, etc.

I did:

git fetch origin

All was well, so I checkedout back to my local master branch, and merge origin/master into it. Again, all is well. I did:

git checkout origin/master

# Look around, see everything is pk

git checkout master
git merge origin/master

Now I am wondering, can I (and should I) delete the local origin/master branch? I tried to, via:

git branch -d origin/master

but it wont let me, and the error message is

error: branch 'origin/master' not found.

I delete local branches after a merge, so I am wondering why I can't do this now, and if I even should, and if so, how?

TheGrapeBeyond
  • 543
  • 2
  • 8
  • 19
  • 2
    No, you should not delete the remote master – j3ny4 May 30 '14 at 17:02
  • @j3ny4 So it just sits there forever on my local machine? It also seems like it will always show up on doing a git log. Why cant/shouldnt we delete it like we would delete local branches when we are done with them? thanks. – TheGrapeBeyond May 30 '14 at 17:04
  • You didn't explain how "you tried to" delete the remote branch. You can **greatly improve** your question by including the commands that you used. –  May 30 '14 at 17:49
  • @Cupcake I thought that would be obvious, but being a n00b did not realize the ambiguity. I will edit. – TheGrapeBeyond May 30 '14 at 17:51
  • 2
    You don't delete it because your'e **not** done with it, and it's just going to come back every time you fetch. You're not done with it until you're done with `origin`, in which case you should just `git remote rm origin`. – user229044 May 30 '14 at 17:55
  • @meagar adding your advice to my answer, hope you don't mind, if you want to add it as your own answer then just leave a comment, and I'll edit it back out of my own. Scratch that, making it into community wiki. Actually, I'll leave that up to you. –  May 30 '14 at 18:01
  • possible duplicate of [How do I delete a Git branch both locally and remotely?](http://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely) –  May 30 '14 at 18:15

2 Answers2

3

Disambiguation: master on origin vs origin/master on your local repo

You didn't make it very clear in your question about what you mean by whether or not you should delete your remote master branch, because in reality, you could be referring to two different but related branches:

master on the remote origin

This is the master branch on your remote origin. If you wanted to delete it, you could do

git push origin --delete master

# Or
git push origin :master

However, in typical Git workflows, Git users want to keep that branch around for various reasons, including (but not limited to):

  1. Sharing work on the master branch with other people.
  2. Keeping a backup of the work done to the master branch.

Given that, there's usually not a good reason to delete this branch on the remote, even if you're working alone.

Remote-tracking branch origin/master on your local repo

This is the remote-tracking branch on your local repo that keeps track of the state of the master branch on the remote origin. To delete remote-tracking branches, you need to use the --remotes or -r flag (as it states in the documentation for git branch):

git branch --remotes --delete origin/master

As the documentation for git branch states:

-r
--remotes

List or delete (if used with -d) the remote-tracking branches.

However, deleting your remote-tracking branch for master is not usually a good idea in most cases, because it helps you to keep track of changes to the remote master branch.

As meager point's out, you don't normally delete remote-tracking branches until you're actually done using the branch that the remote-tracking branch tracks. A typical case if if you have a remote feature branch that you're done with, you delete it from the remote:

git push origin --delete feature

and then update your local repository references to "prune" the obsolete remote-tracking branch:

git fetch origin --prune

# Or shorter
git fetch origin -p

However, in the case of the master branch, you're never really "done" with that branch, because for most Git workflows, it represents the main, canonical line of development and history.

Please Read

To learn more about working with remote branches please read

Community
  • 1
  • 1
  • Thanks for the disambiguation, I learned more today. I am still confused however. If I keep fetching from various remote repos, eventually whenever I do a "git log" on my local machine, it is going to get completely cluttered up with names of remote/origin branches that I have not needed/used in a long time. I delete local branches when I have no more use for them. Why are the remote-tracking branches any different? I am not totally seeing that... – TheGrapeBeyond May 30 '14 at 17:49
  • @TheGrapeBeyond you didn't explain how you're doing `git log` (is it really that exact command?). Also, you should show us what you see (copy and paste or screenshot). If you're talking about the names of remote-tracking branches that show up when you go `git log --oneline --graph --decorate), yes, those can fill your log with more "noise", but people don't normally delete entire remote-tracking branches just to get rid of them in the log output. –  May 30 '14 at 17:57
  • @TheGrapeBeyond right, I would take [meager's advice](http://stackoverflow.com/questions/23959738/can-should-i-delete-the-remote-origin-master-branch-after-i-have-merged-it-with#comment36913287_23959738). Also, you should edit that screenshot into your own question, and host it using Stack Overflow, not some 3rd-party image hosting service. –  May 30 '14 at 18:00
  • Hmm ok. I wont delete them per your advice, but I am still somewhat puzzled by how not deleting them wont somewhat end up cluttering the log output, I suppose Ill just cross that bridge when I get there. – TheGrapeBeyond May 30 '14 at 18:06
  • @TheGrapeBeyond if you have a lot of remote branches, then it does start cluttering up the log, but I personally don't find that to be reason enough to just start nuking remote-tracking branches. –  May 30 '14 at 18:08
  • See [this answer](http://stackoverflow.com/a/16842584/369792) to a different question. Looks like you can use git config so the local repository will fetch only branches you want. Then you just have to remove the ones you've already fetched with `git branch -rd origin/badbranch` – Jason Goemaat Jun 03 '16 at 09:23
1

You should be able to delete the local copy. It's probably not a good idea to remove it considering that it's the one everyone else is also tracking but if you have multiple remotes, i.e. from colleagues, then it may be OK to save space.

Try:

git branch -dr origin/master

-d for deletion if merged upstream. -r for only remote-tracking branches.

I've made a fresh git repository with two commits and a local master setup to track the remote master where the same commits exist too. It works for me and I'm on git version 1.7.10.4

Minn Soe
  • 291
  • 1
  • 7