Home > front end >  Your main branch isn't protected
Your main branch isn't protected

Time:07-20

After running a bunch of commands trying to create and delete tags and branches, I see the following

enter image description here

What is it, and how should I handle it? My other repositories don't have it.

Can I "undo" the commands that caused it? I believe it happened after some of below commands:

$ git push --set-upstream origin branch_name
error: src refspec branch_name matches more than one
error: failed to push some refs to 'https://github.com/my_name/repo_name.git'
$ git push origin :refs/heads/branch_name
remote: warning: Deleting a non-existent ref.
To https://github.com/my_name/repo_name.git
 - [deleted]         branch_name
$ git push --set-upstream origin branch_name -f
error: src refspec branch_name matches more than one
error: failed to push some refs to 'https://github.com/my_name/my_repo.git'
$ git push --set-upstream origin main
Everything up-to-date
Branch 'main' set up to track remote branch 'main' from 'origin'.

CodePudding user response:

Git has no notion of protected branches. They are purely a GitHub/GitLab, etc. concept. You should simply click "Protect this branch" to enable branch protection.

I'm not sure why you're repeatedly trying to change the upstream and push to different branches. I'm assuming you're on the same branch running each of these pushes. But if that's the case then you're changing the upstream and which branch your targeting with each push. Git will protect you, but using -f is very destructive since it disables a majority of the existing safety checks including potentially overwriting yours or someone else's work.

Also,

I figured maybe it's a built-in alarm for when someone's forcefully deleting stuff, but I'd like to be sure. Also, I'm sure you're right on -f, but I'll keep doing it - at least as long as it says so in an SO answer

Just because another SO post claims something is the answer, doesn't mean its the same answer in your situation. Trying the same commands over and over won't solve your issue, it'll only serve to frustrate you.

What seems to be the problem?

You're receiving an error that there are multiple refspecs with the name branch_name. My guess is you have names that collide, such as a tag and a branch name that are the same, etc. As a general rule never have tags and branches with the same name. This can cause conflicts with git.

However, let's start simply here. Nowhere do I see any updating of the local refs. Simply updating the refs and then pushing might solve your issue.

Approach 1: Ensuring your refs are up to date

  1. git fetch will keep your git client's refs information in sync with remote.
  2. git checkout branch_name
  3. git push -u origin branch_name

Approach 2: Creating a different branch

Let's create a new branch with a name other than branch_name since there seems to be some sort of conflict:

  1. git fetch
  2. git checkout -b my_new_branch branch_name will create a new branch off of branch_name locally and switch to it.
  3. git push -u origin my_new_branch

Approach 3: Trying to fix the refs issue

If you absolutely must have branch_name as the name, you can try to resolve the conflicts. I'm not sure if the below will work as its untested, based on speculation and I'm unsure how to recreate your scenario.

  1. git fetch will update the git client's refs so they're aware of changes on remote.
  2. git checkout branch_name to checkout your local branch
  3. Delete any tags that conflict:
  • git tag -d branch_name will delete the corresponding local tag.
  • git push --delete origin :refs/tags/branch_name will delete tags on remote that have the conflicting name. You'll get an error if non existent
  1. git push --delete origin branch_name will permanently delete the remote copy of branch_name. Before running this make sure you have all the commits you expect on your local copy. Again, you'll get an error if the branch does not exist.
  2. This should clear up any issues with the refs. You should be able to do git push -u origin branch_name now.
  • Alternatively, you can also try git fetch --prune will remove all branch refs that do not exist on remote.
  1. git tag -a new_tag_name -m "tag message" sha1 recreate the release tag we previously deleted. sha1 is the commit hash of the release you previously tagged.
  2. git push origin new_tag_name pushes the tag to remote and automatically creates a release in GitHub.

I hope one of those solves your issue.

CodePudding user response:

While it's getting sorted, from GitHub Help:

This appears to be a nudge that GitHub is incrementally deploying.

Ditto- it is just a reminder to set some rules on your main branch - nothing to worry about!

but my reply asking "what is the trigger?" remains unanswered. I've confirmed this for my other repositories, even without commits in the past month, but couldn't find a pattern.

  • Related