Skip to content

Instantly share code, notes, and snippets.

@acfoltzer
Last active October 6, 2020 11:54
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acfoltzer/4284d240f2787eb1826ec074e08234b5 to your computer and use it in GitHub Desktop.
Save acfoltzer/4284d240f2787eb1826ec074e08234b5 to your computer and use it in GitHub Desktop.
Change git init's default branch name

Change the name of the default branch created by git init

Suppose you want the default branch name created by git init (or tools that shell out to it, like cargo new) to be something other than master. In this example, I'll call it develop.

  1. Add the following to your .gitconfig:

    [init]
        templateDir = ~/.config/git/template/
    

    This tells git init to use the files found in ~/.config/git/template to populate the initial .git directory. You can use whatever path you'd like.

  2. Create a file in the template directory called HEAD, for example ~/.config/git/template/HEAD, with the following contents:

    ref: refs/heads/develop
    

    Replace develop with the default name of your choice.

@softprops
Copy link

Very cool. Thanks for sharing!

@beeman
Copy link

beeman commented Jun 12, 2020

Thanks!

I've added this function in my .zsrhc to rename the active project I'm on.

# Rename git branch master to main
function rename-master() {
  git branch -m master main
  git push origin HEAD
  git branch -u origin/main
}

@zigiprimo
Copy link

very neat - thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment