Tag Archives: Git

Git: Rename “master” to “main”

More and more places are starting to rename their “master” branch to “main”. Here is a short description on one way to do that

1. We start with pulling/commiting all our work so that our Git tree is clean

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

2. Now we rename our local branch

niklas@HOME MINGW64 /c/git/my-project (master)
$ git branch -m master main

niklas@HOME MINGW64 /c/git/my-project (main)

3. Now we create the “main” branch on our remote

$ git push -u origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for main, visit:
remote:   https://git.mycompany.com/nb1-team/my-project/-/merge_requests/new?merge_request%5Bsource_branch%5D=main
remote:
To https://git.mycompany.com/nb1-team/my-project.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

4. and lastly we remove the “master” branch to prevent any misstakes commiting to it in the future

$ git push origin --delete master
To https://git.mycompany.com/nb1-team/my-project.git
 - [deleted]         master

NOTE! If you have pipeline scripts that depended on “master” before you need to update them to use “main” now
NOTE 2! If your project used “master” as “Default branch” you need to change that to “main” now

Tested on Windows 10 and GitBash v3.5.2

Git: My aliases

Every time you start a new assignment on a new computer all your aliases are gone. I’ll put them here for easy access, and if someone likes them they can use them too 🙂

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'

Tested with Git v 2.24.3 on OSX and Linux

Git: “You can only push your own commits in this repository” – when there actually is a problem

I ran into this recently by creating a new Bitbucket repository when having a spelling error in my mail address in settings. I got this error:

remote: You can only push your own commits in this repository
remote: Commit 85eb7acbf42a14b9 was committed by Niklas Ottosson (username) <niklas@worldwideweeb.com>

In the commit 85eb7acbf42a14b9 (initial commit of the new repository) my mail was niklas@worldwideweeb.com when it should have been niklas@worldwideweb.com (only one ‘e’ in ‘web’)

To fix this we need to make sure we have the correct mail address in global settings:

git config --global user.email niklas@worldwideweb.com

and then reset the author of the initial commit

git commit --amend --reset-author --no-edit

This will now replace the previous author with me, and since I now have the correct spelling the commit will be “fixed”

Tested with Git 2.24.3