0

After I try to create new branch by either commands new branch is either not created or master is gone. If I use command git branch newBranch branch is not created. If I use command git checkout -b newBranch new branch is created but master branch is gone. `

mat1
  • 83
  • 10
  • Master branch is gone ? Does it means if you run "git branch" command , there is no master branch ? – Arsalan Valoojerdi Jun 06 '20 at 11:18
  • yes, it appears that new branch just replaces master branch. If I want to get master branch back, I run checkout -b master and again only master branch is there. – mat1 Jun 06 '20 at 11:49
  • Do you understand what a branch is? If you checkout a branch, you change your worktree (the files you can see) to match that branch. It’s like a teleportation machine: you can say “I want to be in London” or “I want to be in Paris” and poof, you are there. But you cannot be in London and Paris at the same time. If you jump to Paris, then London “is gone”. – matt Jun 06 '20 at 12:08

1 Answers1

1

First, use the new git switch command, not the old confusing git checkout one.

 git switch -c newBranch

Second, make sure you have a master branch to begin with, meaning git log --decorate --oneline --graph --all --branches shows at least one commit in the master branch.
Not "origin/master", but master.
You can list all branches with git branch -avv.

If you had zero commit in a newly initialized repository, then there would be no branch at all, as seen here.

The OP mat1 confirms in the comments:

I have made first push to remote repo, now it works.
Because I didn't have made initial commit and push , that is why I could not create branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • git switch -c newBranch doesnt create new branch, neither it switches to new branch since new branch doesnt even exists. – mat1 Jun 06 '20 at 12:13
  • @mat1 What version of Git are you using? [`git switch -c` option](https://git-scm.com/docs/git-switch#Documentation/git-switch.txt--cltnew-branchgt) is meant to *create* a new branch. – VonC Jun 06 '20 at 12:19
  • git version 2.16.1.windows.1 – mat1 Jun 06 '20 at 22:39
  • @mat1 Can you update first to Git 2.27? (https://github.com/git-for-windows/git/releases) – VonC Jun 06 '20 at 22:53
  • Before updating git I have made first push to remote repo, now it works. Because I didnt have made initial commit and push , thats why I could not create branch. – mat1 Jun 06 '20 at 23:25