Why checkout -b does work only after second attempt?

exebook
$sudo git clone sample.git 

Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
done.

$ cd sample

$ echo "hello world" > readme

$ git add readme

$ git checkout -b a
Switched to a new branch 'a'

$ git branch

$ git checkout master
error: pathspec 'master' did not match any file(s) known to git.

$ git checkout a
error: pathspec 'a' did not match any file(s) known to git.

$ git checkout -b b
Switched to a new branch 'b'

$ git branch

$ git commit -am .
[b (root-commit) 12b8434] .
 1 file changed, 1 insertion(+)
 create mode 100644 go

$ git branch
* b

$ git checkout a
error: pathspec 'a' did not match any file(s) known to git.

$ git checkout -b a
Switched to a new branch 'a'

$ git branch
* a
  b

What was wrong with my first checkout -b a, why the branch was not created?

AnoE

Well, you told git to create a branch in an empty repository. There is no commit yet, and a branch is just a "sticky note" pointing to a commit. So what is git supposed to do...

At least it stores your new branch in HEAD, so the branch tip will be updated with future commits. As shown by your commit.

More fun with empty repositories:

~/tmp> mkdir empty
~/tmp> cd empty
~/tmp/empty> git init
Initialized empty Git repository in tmp/empty/.git/
~/tmp/empty> git log
fatal: bad default revision 'HEAD'
~/tmp/empty> git branch xxx
fatal: Not a valid object name: 'master'.
~/tmp/empty> git checkout -b xxx
Switched to a new branch 'xxx'
~/tmp/empty> git log
fatal: bad default revision 'HEAD'
~/tmp/empty> ls -l .git/
branches/    config       description  HEAD         hooks/       info/        objects/     refs/
~/tmp/empty> cat .git/HEAD
ref: refs/heads/xxx
~/tmp/empty> ls -l .git/refs/heads
total 0

EDIT: adopted the comment from @jthill.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does function only work on second click?

From Dev

Keyboard shown only after second attempt

From Dev

Why does my form script only work on second submission

From Dev

Why does this click function not work after clicking a second item?

From Dev

Why does WebRTC set up a connection only after second offer?

From Dev

Why does WebRTC set up a connection only after second offer?

From Dev

Why the second statement does not work?

From Dev

Why the second statement does not work?

From Dev

$anchorScroll and $location only work after second try

From Dev

$anchorScroll and $location only work after second try

From Dev

Why only windowGainedFocus() does not work?

From Dev

Why does the first assert work, but not the second?

From Dev

Why does the second form disappear after submiting?

From Dev

Why backspace '\b' does not work in C++?

From Dev

Why does `a<b<c` work in Python?

From Dev

Why \b does not work correctly for some languages?

From Dev

Why backspace '\b' does not work in C++?

From Dev

Hello, Why my button B does not work?

From Dev

Why BACKSPACE (\b) does not work as intended with a file?

From Java

Why does JavaScript only work after opening developer tools in IE once?

From Dev

UITableView reloads data only on second attempt

From Dev

Why does TypeScript assertion of object literal `{a}` work with interface `{a, b}` but not `{a?, b}`

From Dev

error msgs after second loop attempt

From Dev

Why does getBoundingClientRect only work locally?

From Dev

Why does this statement only work with a WHERE?

From Dev

Why does this code work only with *args in something()?

From Dev

Why does each of these buttons only work once?

From Dev

Why does this code work only partially?

From Dev

Why does the following code only work with GridLayout?

Related Related

  1. 1

    Why does function only work on second click?

  2. 2

    Keyboard shown only after second attempt

  3. 3

    Why does my form script only work on second submission

  4. 4

    Why does this click function not work after clicking a second item?

  5. 5

    Why does WebRTC set up a connection only after second offer?

  6. 6

    Why does WebRTC set up a connection only after second offer?

  7. 7

    Why the second statement does not work?

  8. 8

    Why the second statement does not work?

  9. 9

    $anchorScroll and $location only work after second try

  10. 10

    $anchorScroll and $location only work after second try

  11. 11

    Why only windowGainedFocus() does not work?

  12. 12

    Why does the first assert work, but not the second?

  13. 13

    Why does the second form disappear after submiting?

  14. 14

    Why backspace '\b' does not work in C++?

  15. 15

    Why does `a<b<c` work in Python?

  16. 16

    Why \b does not work correctly for some languages?

  17. 17

    Why backspace '\b' does not work in C++?

  18. 18

    Hello, Why my button B does not work?

  19. 19

    Why BACKSPACE (\b) does not work as intended with a file?

  20. 20

    Why does JavaScript only work after opening developer tools in IE once?

  21. 21

    UITableView reloads data only on second attempt

  22. 22

    Why does TypeScript assertion of object literal `{a}` work with interface `{a, b}` but not `{a?, b}`

  23. 23

    error msgs after second loop attempt

  24. 24

    Why does getBoundingClientRect only work locally?

  25. 25

    Why does this statement only work with a WHERE?

  26. 26

    Why does this code work only with *args in something()?

  27. 27

    Why does each of these buttons only work once?

  28. 28

    Why does this code work only partially?

  29. 29

    Why does the following code only work with GridLayout?

HotTag

Archive