How to rename git branch name

Suppose we run this command git push -u origin main and get the following errors:

error: src refspec main does not match any
error: failed to push some refs to 'https://codeberg.org/tasin/Clarity.git'

So this is happening because my remote branch name is master while we are trying to push to the main branch.
Now let's see how we can rename the branch name from master to main.

Check Your Local Branch Name

git branch

This will show you all the local branches. You'll likely see master as your current branch.

Rename Your Branch to 'main'

# Rename master to main
git branch -M main

# Now push
git push -u origin main

If You Want to Keep 'master' but Codeberg Expects 'main'

# Push master to main on remote
git push -u origin master:main

Why This Happens:

  • Older Git versions default to master as the branch name
  • Newer Git versions and platforms like GitHub/Codeberg default to main
  • Your local repo was created with master, but you're telling Git to push to main

To Check Your Default Branch Name:

git config --global init.defaultBranch

To Change Default for Future Repos:

git config --global init.defaultBranch main
Post Comment

Comments