-1

Is it often practice to delete local git repository and make fresh git clone instead of fetching every remote branch and merging it with local branches?

karol wołonciej
  • 109
  • 1
  • 1
  • 10
  • 1
    No, not at all. that's overkill. Why do you think it would be? – zero298 May 03 '20 at 00:47
  • Only when I've completely botched a merge. – stolenmoment May 03 '20 at 01:20
  • That is an odd practice. What do you mean by "fetching every remote branch and merging it with local branches"? Are there changes in the local branches, or are you just updating them? Can you provide a concrete example? – Schwern May 03 '20 at 07:50
  • I mean when I have local tracking branches and want to update them all I have to git fetch and then merge every of this branch with fetched one, right? – karol wołonciej May 03 '20 at 11:41
  • @heroarthur Yes, but you only need local branches if you intend to make changes. If deleting the repo is an alternative, you're not making any changes (that you want to keep). Don't make local branches you don't need. If you do, delete them. `git branch -d some_branch` will warn you if there are unmerged changes. – Schwern May 03 '20 at 19:14

1 Answers1

1

I'm guessing you're doing this to update all your local branches. But there should be no need to have a local branch for a remote branch unless you're working on the branch. And if you're working on branches, you certainly don't want to be deleting your local repository.

By default, when you clone a repository Git will fetch all the remote branches and store them in [remote name]/[branch name]. The default remote name is origin, so the remote master goes in origin/master. It will only make a single local branch, master.

Unless you intend to work on a branch, there is no need to make a local branch. You can instead check out the remote branch. For example, if you want to examine a branch called feature on the remote called origin (the default) you would git checkout origin/feature or to see its history git log origin/feature.

To update the remote branches, simply git fetch. This does not affect any local branches, it's safe to fetch as often as you like.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    Worth mentioning: checking out a remote-tracking name like `origin/feature` puts you in *detached HEAD* mode. There's nothing wrong with this mode, you just need to remember that this is the case (or use `git status` to check) before you start doing new work, vs just using `git fetch` and then re-checkout-ing `origin/feature` again. – torek May 03 '20 at 11:51