2

The project we are using had its master branch moved to main, on GitLab for reasons of consistency.

Locally I had renamed the master branch to main:

git branch -m master main

This seemed to be enough until I accidentally did git checkout master and then it recreated the master branch. Doing a git branch -a, I see I now have the following two remotes (in addition to my other ones):

  remotes/origin/main
  remotes/origin/master

How do a tell the local repo to forget about remotes/origin/master, to avoid accidentally recreating master, when I mean to switch to the main branch?

Andre M
  • 6,649
  • 7
  • 52
  • 93
  • See `git fetch --prune`. By default, `git fetch` reads the names from the other (remote) Git and creates or updates your local `origin/*` names to match. So you *had* `origin/master`. Then you renamed `master` over there, and now your Git sees `main` and creates and updates `origin/main` and ... leaves the old stale `origin/master` in place because nobody said to delete anything, now did they? – torek Feb 04 '22 at 06:47
  • The `--prune` option, which I do think should be the default but one must be slightly careful with it, means *if you don't see it over there, remove it over here*. – torek Feb 04 '22 at 06:48

1 Answers1

2

First step would be to try:

git fetch --prune

If that does not work, it could mean the master branch is still on GitLab remote repository side (it could have been pushed by mistake by another developer who also checkout the wrong local master branch first)

In any case, once GitLab repository is clean, you can also add a pre-commit hook in order to prevent any future accidental checkout of the wrong branch.

lindes
  • 9,854
  • 3
  • 33
  • 45
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250