Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Resolving "pathspec 'master' did not match any files known to git" When Switching Branches

Tech 1

Reproducing the error

  • Start from an empty repository, create and commit on a non-master branch, then try to switch to master.
$ git init .
$ git switch -c dev
$ printf "hello\n" > notes.txt
$ git add notes.txt
$ git commit -m "seed: add notes.txt on dev"
$ git checkout master
error: pathspec 'master' did not match any file(s) known to git

Root cause

  • No branch actually exists until the first commits created. After git init, HEAD is on an unborn branch (its name depends on your Git config; historically master, often main today).
  • In this sequence the first commit is created on dev, so only dev exists. There is no master ref yet, so resolving "master" fails—Git cannot match it as a branch name or a path in the index/working tree.
  • git checkout tries to enterpret as a branch or a pathspec. Because master is neither an existing branch nor a file in this repo state, the command errors out.

Fix options

Option A: Create the initial commit on master, then branch off

$ git init .
$ printf "# Project\n" > README.md
$ git add README.md
$ git commit -m "chore: initial commit on master"
$ git branch -a
* master

# Now create your development branch
$ git switch -c dev

Option B: If you already committed on another branch, create master now at the current commit

  • This makes master point at the same commit as your current branch.
# Starting from the earlier example where you're on 'dev'
$ git branch master
$ git switch master

Option C: If master exists on the remote, create a local tracking branch

$ git fetch origin
$ git switch -c master --track origin/master
# or equivalently
$ git checkout --track origin/master

Option D: Align your default initial branch name to avoid confusion

  • Newer Git versions may default to main. You can set the default initial branch explicitly:
# Choose one default for new repos
$ git config --global init.defaultBranch master
# or
$ git config --global init.defaultBranch main

Pushing to create the remote master

  • If you initialized locally on master and want a remote master, push it once:
$ git push -u origin master

Verifying branches

$ git branch -a
* master
  dev
  remotes/origin/master

Notes on switching branches

  • Changing branches updates files in the working directory to match the target commit. If your uncommitted changes would be overwritten by the checkout, Git will prevent the switch until you commit, stash, or discard those changes.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.