-1

I have created a file called basic and I have created a branch called new. After moving to the new branch, I want to switch to the master branch, hence i used git checkout master. Unfortunately, the terminal stated there is no master branch.

(base) Dun-Yan:basic ongdunyan$ git checkout master
error: pathspec 'master' did not match any file(s) known to git

Here is the git status:

(base) Dun-Yan:basic ongdunyan$ git status
On branch new
nothing to commit, working tree clean

I would appreciate if you all can help me, peace :)

Dun Yann
  • 31
  • 2
  • 2
    Some tips: use `git branch` to list all your local branches or `git fetch && git branch -r` to list all remote branches to be sure that the base branch name was master at all. 2: to go back to previous branch you can always use `git checkout -` – gazdagergo Oct 24 '21 at 07:21
  • I bet this is a fresh repo so no master yet exists. Did I win the bet? – terrorrussia-keeps-killing Oct 24 '21 at 07:22
  • ```git checkout -``` didnt work, instead – Dun Yann Oct 24 '21 at 07:52
  • ```(base) Dun-Yan:basic ongdunyan$ git checkout - error: pathspec '-' did not match any file(s) known to git``` – Dun Yann Oct 24 '21 at 07:52
  • 1
    Maybe it is called `main`, not `master`? Use `git branch` to list all your local branches or `git reflog` to show which old commits or branches were checked out – knittl Oct 24 '21 at 10:19

2 Answers2

0

You have to make at least one commit on master before you check out to a different branch, otherwise the branch HEAD will practically not exist and git cannot checkout back to "nowhere".

gazdagergo
  • 6,187
  • 1
  • 31
  • 45
0

This means that, yes indeed, there is no branch named master. There's no requirement that any Git repository have any particular branch names—you can have a repository with no branch names at all—so that's not an error, unless of course you want it to be one.

Use git branch to list out all the existing branch names. In a new, empty repository, this would print no names as there are no branches, but your git status output indicates that you do at least have one branch name, specifically new.

Use git branch -r to list out all the existing remote-tracking names. That should make you ask: wait, what's a remote-tracking name? (Especially since Git calls this a remote-tracking branch name: I drop the word branch because these names aren't actually branch names at all.) A remote-tracking name is the way your Git remembers some other repository's branch names.

The key insight here is that your branch names are yours. They are not anyone else's branches. Just as your name might be Sue, or Rajeesh, or Quentin, or whatever, that doesn't mean that everyone named Sue (or whatever your name may be) is you. Your Git repository has its own branch names. You may want yours to match up with someone else's, but that's just a desire on your part, not something Git must do.

When you have your Git—your Git software operating on your Git repository—talk to some other Git (software on another repository), the two Gits will confer, and your Git will be able to see their commits and their branches (to whatever extent they're willing to show them anyway). Your Git will get their branches-and-commits when you run git fetch. But your Git will turn their branch names into your remote-tracking names, so that if they have a branch named fred, you get a remote-tracking branch with some other name.

In order to have your Git talk to another Git, you will set your Git up with what Git calls a remote. A remote is just a short name like origin: we have Git store a URL under this shorter name, and then we run:

git fetch origin

to direct our Git to look up the short name, origin, find the long URL (ssh://git@github.com/some/path/to/repo.git or https://github.com/some/path/to/repo.git or whatever), and connect there and see their branches and commits.

Having seen origin's branch named fred, if your Git is directed to bring it over—that's the default—you will then find, in your repository, the remote-tracking name origin/fred. This represents the branch name fred as seen on the remote named origin.

The git branch -r command lists out these remote-tracking names. Using git branch -a, your Git will list out your branch names and your remote-tracking names, so that you will be able to see your new and—if it exists at this point—your origin/fred. For whatever reason,1 git branch -r prints this one as origin/fred and git branch -a prints it as remotes/origin/fred.

Now, you might want your Git to create a new branch name, and you can do that. You should, however, first read up on how commits and branch names actually work. Git uses the names only2 to find commits: it's really the commits and their hash IDs (or "object IDs", to be formal) that matter.


1This is just plain inconsistent. Both forms work: you can write either origin/fred or remotes/origin/fred, if you like. But there's no solid reason for this difference in output format. The actual full name origin/fred is refs/remotes/origin/fred, and if one is going to go to the effort to insert the remotes/ part, why not go all the way and put in the unambiguous refs/remotes/origin/fred version? But that's all just an aside.

2Well, mainly is more accurate than only, since branch names also participate in key-value lookups for branch.* settings in your configuration.

torek
  • 448,244
  • 59
  • 642
  • 775