Default branch name configuration in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to set or get the default branch name changes as the number of repositories grows.
How does the work scale when configuring default branch names in git?
Analyze the time complexity of the following git commands.
git config --global init.defaultBranch main
git init myrepo
cd myrepo
# The new repo uses 'main' as the default branch
This code sets the global default branch name and initializes a new repository using that default.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Setting or reading configuration values and initializing repository metadata.
- How many times: Each command runs once per repository or per user configuration.
Setting the default branch name globally is a single operation, no matter how many repositories exist.
| Input Size (number of repos) | Approx. Operations |
|---|---|
| 10 | 1 (set config once) |
| 100 | 1 (still one config set) |
| 1000 | 1 (no extra work for more repos) |
Pattern observation: The work to set the default branch name does not increase with more repositories.
Time Complexity: O(1)
This means the time to set or get the default branch name stays the same no matter how many repositories you have.
[X] Wrong: "Setting the default branch name takes longer as I create more repositories."
[OK] Correct: The configuration is stored once globally and does not repeat per repository, so the time stays constant.
Understanding how configuration commands scale helps you explain efficiency in managing many projects, a useful skill in real work.
"What if we set the default branch name separately inside each repository instead of globally? How would the time complexity change?"
Practice
init.defaultBranch setting in Git control?Solution
Step 1: Understand the purpose of
This setting defines the branch name Git uses when you runinit.defaultBranchgit initto create a new repository.Step 2: Identify what it controls
It controls the default branch name, usually 'master' or 'main', for new repositories.Final Answer:
The name of the main branch created when initializing a new repository -> Option DQuick Check:
Default branch name = main branch name [OK]
- Confusing branch name with remote URL
- Thinking it sets commit message templates
- Assuming it sets user info
main globally in Git?Solution
Step 1: Recall the correct Git config syntax
The correct syntax to set a global config isgit config --global <key> <value>.Step 2: Match the key for default branch name
The key isinit.defaultBranch, so the full command isgit config --global init.defaultBranch main.Final Answer:
git config --global init.defaultBranch main -> Option CQuick Check:
Usegit config --global init.defaultBranch[OK]
- Omitting 'init.' prefix in config key
- Using 'git set' instead of 'git config'
- Trying to set default branch during init command
git config --global init.defaultBranch develop, what will be the default branch name when you run git init in a new folder?Solution
Step 1: Understand the effect of the config command
Settinginit.defaultBranchto 'develop' globally changes the default branch name for all new repos initialized after this.Step 2: Predict the branch name after
When you rungit initgit init, the initial branch will be named 'develop' instead of the usual 'master' or 'main'.Final Answer:
develop -> Option BQuick Check:
Config set to 'develop' means new repos start with 'develop' [OK]
- Assuming default stays 'master' or 'main'
- Confusing local repo branch with global config
- Thinking config affects existing repos
git config --global init.defaultbranch main but new repos still use 'master'. What is the problem?Solution
Step 1: Check the config key spelling
Git config keys are case-sensitive. The correct key isinit.defaultBranchwith a capital 'B'.Step 2: Understand why the setting didn't apply
Usinginit.defaultbranch(lowercase 'b') creates a different config entry that Git ignores for default branch naming.Final Answer:
The config key is case-sensitive; it should beinit.defaultBranch-> Option AQuick Check:
Config keys are case-sensitive in Git [OK]
- Ignoring case sensitivity in config keys
- Thinking terminal restart is needed
- Believing default branch can't be changed
stable. How do you set this globally and verify it?Solution
Step 1: Set the global default branch name
Usegit config --global init.defaultBranch stableto set the default branch name for all new repos globally.Step 2: Verify the setting
Rungit config --global init.defaultBranchto confirm the value is set to 'stable'.Final Answer:
Rungit config --global init.defaultBranch stableand thengit config --global init.defaultBranch-> Option AQuick Check:
Set with config, verify with config [OK]
- Using incorrect commands like git set
- Trying to verify with unrelated commands
- Using system config without permission
