When you work with remotes on git, your local repository is entirely separate from the remote, and only gets entangled with remote branches when you say so.
The git fetch
command is important here (documentation). When you run git fetch
, it doesn't really change anything in your local repository. Rather, you can think of it as fetching the information about the remote and its branches, and exposing them to you. If you have fetched a remote, git branch -a
will show you all your branches, including those on the remote.
But important to note is that none of those remote branches exist in your local repository. git branch
will show you all your local branches. Let's say this is your situation:
Local branches origin (remote) branches
--- ---
main main
feature1 staging
otherFeature2
git branch -a
will list all those branches, but only the local ones exist in your repository.
You can set a local branch to "track" a remote one, which can be done a couple of different ways. If you run git checkout staging
in the above state, git will say "I don't see a staging branch here, but I see that there is one on origin
, so I'll set up a local branch with the same name for you and set it to track origin/staging
." If you create a branch locally, you push it with the command git push -u <remote> <branch-name>
, where -u
means upstream (the name of the remote you wish to track it to), and <branch-name>
will be the name of the branch on the remote. That can be different from your local name, but it's usually easier if they're the same. I have aliased git push -u origin `git branch --show-current`
because it's such a common command. If a branch with that name doesn't exist on the remote, it will be created, but if it already exists, git will simply start tracking your local branch to it.
Now if the state is this:
Local branches origin (remote) branches
--- ---
main -track-> main
staging -track-> staging
feature1 -track-> feature1
otherFeature2 -track-> otherFeature2
It should now be more clear what's actually going on. There are 8 branches here: 4 local, and 4 remote. If you go to GitHub and delete a branch there, it will only delete the remote branch - it would be pretty awful if remote branches could reach into your local repository and delete your stuff! But your local repository won't know about the changes made on GitHub until you tell it to grab the remote's info - with git fetch origin
. Then, origin/branch-name
should stop showing up.
Another way to delete a remote is with the command you mentioned, git push --delete <remote> <branch-name>
. Because you run that from your local repository, you won't need to git fetch
to update, but your local branch will still be around. That means you still need to delete your local branch using git branch -d <branch-name>
.
On the flip side, if you delete a branch locally, its tracked remote branch will happily keep existing. If you want to totally clear the project of this branch, you will have to do both. But take care to ensure that no one else is using it before deleting remote branches!