0

I want to make my local branches to match all the remote branches. For example, imagine that I have these branchs in my git:

- branch-foo
- branch-bar

and in my github repo I have these branchs:

- branch-foo
- branch-baz

I would like to make my local branches to be:

 - branch-foo
 - branch-baz

In other words, remove all branches that don't exist in github, pull the branches that don't exist in github and keep the branches that still exists in github.

I've normally do this manually with git pull [origin] [branch] to update branchs, git switch|checkout [branch] to go to a particular branch or git branch -a | git branch -D [branch] to check the existent branchs and remove them, but this is kinda slow, there is a command (or set of 1-2 commands) that could do this automatically?

denies
  • 61
  • 7
  • 2
    Keeping (local) branch names for all your remote-tracking names is a [fool's errand](https://www.merriam-webster.com/dictionary/fool%27s%20errand). Don't do it. Just use `git fetch --prune` to update your remote-tracking names and be happy with the remote-tracking names; make new (local) branch names only if and when you want to do work by making new commits. – torek Mar 28 '22 at 21:36
  • Thanks for the comment. I understand that it has no point in pulling branches that I'll not use. But there is a way to remove local branches if these branches are closed in github, for example? Is this a bad practice too? – denies Mar 29 '22 at 17:18
  • In the end, the reason to have a local branch is "I intend to build this version, or add commits to this branch, or otherwise have some reason to want to have a name for this version". (You can use a tag name instead of a branch name if you intend not to add new commits; there's no particular reason to prefer branch vs tag name here.) The point of this is to say that you should have a name if *you* want a *name*. That's not a matter of whether someone else has a name (on, e.g., GitHub). It's a matter of whether *you* want a name. So your branches are yours, and exist for your reasons. – torek Mar 29 '22 at 17:58
  • 1
    You said: "is there a way to remove local branches if these branches are **closed** in github". Is your goal to simply delete your local branches that you no longer need? If yes [I think this will help](https://stackoverflow.com/q/7726949/184546). – TTT Apr 02 '22 at 19:26
  • Thanks @TTT, it helped me :) also, thanks for the advice as well, torek – denies Apr 03 '22 at 00:26

0 Answers0