Reputation: 35467
I created a repository on Github. I have two development environments, and I access Github with Github for Windows on both.
On one of the two development environments, I deleted a branch and clicked "synchronize" in Github for Windows. The branch appropriately got deleted in Github. When I synchronized from my other development environment, there was an option to "publish" the branch-to-be-deleted, but none to delete it.
How can I tell git or Github for Windows to make a full sync, including the deletion of obsolete branches?
Upvotes: 20
Views: 14863
Reputation: 55533
Yes and no — depending on what you really meant by "deletion of a local branch".
No, it's impossible to have a remote repository delete a normal branch in your local repository.
Yes, it's possible to ask Git to delete so-called remote branches in your local repository for the branches which were deleted on a particular named remote (such as "origin").
This one is achieved by running
git remote prune origin
or
git fetch --prune
Please do understand first that Git's model for working with remote repositories is asymmetric: the fact a branch in your local repository has the name exactly matching that of a branch in a remote repository has no meaning to Git.
The reason is two-fold:
In other words, if you, say, have a private repository hosted on Github, and a local clone of it on your PC, you deleted a branch via the Github's UI and now want this branch to go away in your local clone, that's indeed understandable as these two repositories have 1-to-1 relation. Now consider that you have a repository which was cloned by your fellow developers Gill, Joe and Alice, each of them simplemindedly created a branch named "feature" to delevop different features, and you have then fetched from all these three repositories — ending up with a three remote branches named <remote>/feature
, with that <remote>
placeholder in each case being the name of a remote you gave to these three remote repositories. Add to this that you can have your own branch named "feature" which has nothing to do with the same-named branches of your fellow developers. Now consider that, say, Alice merges her "feature" branch and then gets rid of it — does this mean your own branch "feature" has to go away automatically as well next time you fetch from the Alice's repo? Surely not.
So while Git has certain helpers to somewhat tie local branches to particular remote branches, these ties are asymmetric. In particular, Git treats your local history as being "sacred", and destructive operations on it require your explicit actions.
The asymmetry explained above only takes place in "normal" Git repositories—which are used most of the time. It should be noted that it's possible to create a Git repository that will behave in a different manner: say, a call to git fetch origin
in such a repository will make sure the state of the branches and tags of <origin>
will be mirrored in the local repository—deleting and creating stuff when necessary.
Upvotes: 32