Skip to content

new/init: default git branch to 'main'; update tests and Dockerfiles #15857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

doocho
Copy link

@doocho doocho commented Aug 18, 2025

Summary

  • Change: cargo new/cargo init initializes Git repos with main as the default branch.
  • Update: Tests and test Dockerfiles adjusted to match the new default.

Motivation

  • Consistency: Stable behavior regardless of user init.defaultBranch.
  • Convention: Align with current industry standard and inclusive naming.

Behavior change

  • Before: Default branch could be master or follow user Git config.
  • After: Always main for repos initialized by Cargo. Existing repos unaffected.

Implementation details

  • Core: Set libgit2 RepositoryInitOptions::initial_head("main") in GitRepo::init.
  • Files:
    • src/cargo/util/vcs.rs
    • tests/testsuite/new.rs
    • crates/cargo-test-support/containers/apache/Dockerfile
    • crates/cargo-test-support/containers/sshd/Dockerfile

Tests

  • Adjusted: tests/testsuite/new.rs expects refs/heads/main.
  • Infra: Test Dockerfiles use git init -b main.

Backwards compatibility

  • Scope: Only affects newly created repos via cargo new/cargo init.
  • Note: Scripts assuming master on fresh repos should be updated.

How to verify

cargo new foo
git -C foo symbolic-ref HEAD  # expects: refs/heads/main

Alternatives considered

  • Respect init.defaultBranch: Rejected to ensure deterministic behavior across environments and CI.
  • Config override: Could be added later if configurability is desired.

Release notes

  • Changed: cargo new/cargo init now create Git repositories with main as the default branch.

Checklist

  • Code updated
  • Tests updated
  • Test Dockerfiles updated
  • Documentation (can update if there’s a canonical place describing default branch behavior)

@rustbot
Copy link
Collaborator

rustbot commented Aug 18, 2025

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-testing-cargo-itself Area: cargo's tests A-vcs Area: general VCS issues S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 18, 2025
Comment on lines +35 to +36
// Initialize Git repository with "main" as the default branch,
// regardless of the user's global git configuration.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cargo delegates this to libgit2, so it respects whatever libgit2 respects. I would generally recommend we don't do this change in this PR. We should discuss in an issue first before proceeding, per our contributing guideline

Other parts changing dockerfiles and tests are fine, btw, as they are not user-facing.

See also:

@epage
Copy link
Contributor

epage commented Aug 18, 2025

Based on at, I'm going to go ahead and close this.

@epage epage closed this Aug 18, 2025
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testing-cargo-itself Area: cargo's tests A-vcs Area: general VCS issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants