Home > Back-end >  How to sync removed branches?
How to sync removed branches?

Time:11-27

I have branches like "main" and "refactoring". I made the changes I needed in "refactoring" and created a pull request in "main" and accepted it. Now the "refactoring" branch gets in the way. I removed it from the GitHub interface. How can I sync locally now? So that the "refactoring" branch does not appear on the local repository? Until this day, I would just delete the "refactoring" branch locally and that's it. But I feel that this is wrong and decided to find out how to do it right. How do I keep my local branches in sync with branches in a remote repository?

Thanks!

CodePudding user response:

Unfortunately, Git does not automatically prune local branches. You can use git fetch -p to prune local tracking branches like remotes/origin/refactoring, but it won't delete the plain refactoring branch for you. If Git were to delete branches whose upstream refs had disappeared there would be a risk of data loss. You don't want your local data disappearing because something happened on the remote.

Therefore, deleting the branch manually is appropriate:

$ git fetch
<see a message that the remote branch 'refactoring' has been deleted>

$ git branch -d refactoring

If accepting the PR involved squashing commits or otherwise rewriting the commit history then you'll need to force delete:

$ git branch -D refactoring

(Note that while I understand Git's behavior I don't love it. I'm not defending it, merely explaining it.)

  •  Tags:  
  • git
  • Related