Skip to content

Commit 91362db

Browse files
Mvmoc-ehrlich
andauthored
feat: using 'init.defaultBranch' when initializing git repo (#895)
* feat: get branch name from git config * chore: add changeset * fix: default branch not set fallback Co-authored-by: Christopher Ehrlich <[email protected]>
1 parent 8b9a2c9 commit 91362db

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

.changeset/late-beds-jump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-t3-app": patch
3+
---
4+
5+
When initializing a new git repo the git config value 'init.defaultBranch' will be used as the branch name

cli/src/helpers/git.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ const getGitVersion = () => {
4444
return { major: Number(major), minor: Number(minor) };
4545
};
4646

47+
/** If git config value 'init.defaultBranch' is set return value else 'main' */
48+
const getDefaultBranch = () => {
49+
const stdout = execSync("git config --global init.defaultBranch || echo main")
50+
.toString()
51+
.trim();
52+
53+
return stdout;
54+
};
55+
4756
// This initializes the Git-repository for the project
4857
export const initializeGit = async (projectDir: string) => {
4958
logger.info("Initializing Git...");
@@ -99,13 +108,15 @@ export const initializeGit = async (projectDir: string) => {
99108

100109
// We're good to go, initializing the git repo
101110
try {
111+
const branchName = getDefaultBranch();
112+
102113
// --initial-branch flag was added in git v2.28.0
103114
const { major, minor } = getGitVersion();
104115
if (major < 2 || minor < 28) {
105116
await execa("git", ["init"], { cwd: projectDir });
106-
await execa("git", ["branch", "-m", "main"], { cwd: projectDir });
117+
await execa("git", ["branch", "-m", branchName], { cwd: projectDir });
107118
} else {
108-
await execa("git", ["init", "--initial-branch=main"], {
119+
await execa("git", ["init", `--initial-branch=${branchName}`], {
109120
cwd: projectDir,
110121
});
111122
}

0 commit comments

Comments
 (0)