2016-07-11 8 views
1
$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 

私の最初のcheckout -b aで何が問題だったのですが、なぜブランチが作成されなかったのですか?チェックアウト-bは2回目の試行後にのみ動作するのはなぜですか?

答えて

6

まあ、gitに空のリポジトリにブランチを作成するように指示しました。コミットはまだありません。ブランチはコミットを指し示す単なる「付箋」です。だから、gitは何をすべきか...

少なくとも、新しいブランチはHEADに保存されているので、ブランチのヒントは将来のコミットで更新されます。あなたのコミットによって示されるように。空のリポジトリと

もっと楽しい:

~/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は:@jthillからのコメントを採用しました。

+0

あなたは数秒で私を打ち負かす:)ええ、gitはリポジトリにコミットがないと面白いように振舞う傾向がある – quetzalcoatl

+0

この動作は混乱します。私はGitを新しいバージョンにアップデートした後に見つけました。これで 'git init'は偽のマスターブランチを作成します。私はそれをバグと見なします。 – ElpieKay

+0

誰かがそれをバグとみなしているようです。私の例は 'git 2.0.4'です。 – AnoE

関連する問題