46 lines
2.1 KiB
Markdown
46 lines
2.1 KiB
Markdown
---
|
|
title: Set default git branch to main
|
|
author: Yarmo Mackenbach
|
|
slug: git-main
|
|
date: "2020-06-25 11:37:43"
|
|
published: true
|
|
---
|
|
|
|
For a while, we've all been seeing the "switch git default branch from master to main" posts, the earliest I recall having been [written by Scott Hanselman](https://www.hanselman.com/blog/EasilyRenameYourGitDefaultBranchFromMasterToMain.aspx). I've been postponing the change for a bit, but it was [the post by Kristófer Reykjalín](https://www.thorlaksson.com/im-changing-the-default-branch-name-in-my-git-repositories-and-you-should-too/) that gave the required motivation to go out and just do it.
|
|
|
|
For new repositories, [Gitea](https://gitea.io) already has [the option to set the default branch name](https://github.com/go-gitea/gitea/pull/10803).
|
|
|
|
For exiting repositories, the [commands provided by Scott](https://www.hanselman.com/blog/EasilyRenameYourGitDefaultBranchFromMasterToMain.aspx) work perfectly:
|
|
|
|
```
|
|
git checkout master; git branch -m master main; git push -u origin main
|
|
```
|
|
|
|
Yes, a one-liner :) If you like to take things more slowly, here it goes:
|
|
|
|
```
|
|
git checkout master # switch to master branch
|
|
git branch -m master main # move existing master branch to main (keeping history)
|
|
git push -u origin main # push the main branch to the server
|
|
```
|
|
|
|
If you use [Gitea](https://gitea.io), you should now go the repository's Settings > Branches and set the main branch as the default branch. Once done, you can now safely delete the obsolete master branch with the command below.
|
|
|
|
```
|
|
git push --delete origin master # delete the master from the server
|
|
```
|
|
|
|
As for my website, here's the [commit](https://git.yarmo.eu/yarmo/yarmo.eu/commit/78e18c55c59c8e65e99013718cd42154ddb7ebd6) that completed the transition, making sure my CI/CD solution knows what to listen to.
|
|
|
|
---
|
|
|
|
## Update 1
|
|
|
|
Thanks to [Charles Pence](https://charlespence.net/) for reminding me to add the `git push --delete origin master` line.
|
|
|
|
---
|
|
|
|
## Update 2
|
|
|
|
Thanks to [Charles Pence](https://charlespence.net/) for reminding me that a Gitea branch cannot be deleted as long as it's the default.
|