16
7

More than 3 years have passed since last update.

git branchでbranch名が表示されない!!!(ローカルリポジトリ )

Posted at

はいどうも、git苦手なプログラミング初心者です。

Railsでアプリケーションを作っていたのですが、
「コマンドラインで git branch と打ってもローカルのbranchが表示されないよー」という状態になりました。

mbc:ディレクトリ名  ユーザー名$ git branch
mbc:ディレクトリ名  ユーザー名$

さっきまでbranchのmasterを表示できていたのに!
なんでだろう?

環境

ruby 2.7.2
Rails 6.0.3.4
git version 2.29.2

発端

mbc:ディレクトリ名  ユーザー名$ git branch
* master
mbc:ディレクトリ名  ユーザー名$

最初はこんな感じでmasterが表示されていました。その後、少しいじっていたら

mbc:ディレクトリ名  ユーザー名$ git branch
mbc:ディレクトリ名  ユーザー名$

あれ?masterが出ない...

原因

原因は初回のcommitの取り消しでした。
こちらの記事を参考にしました。

なるほど、git branchはcommitが無いと表示されないのか。
そういえばcommitの取り消しをしてたのを忘れてました...

mbc:ディレクトリ名  ユーザー名$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .browserslistrc
        new file:   .gitattributes
        new file:   .gitignore

ステージング状態になってますわ

解決

では実際にcommitしてこの状態を解決してみようと思います。

mbc:ディレクトリ名  ユーザー名$ git branch
mbc:ディレクトリ名 ユーザー名$ git log
fatal: your current branch 'master' does not have any commits yet

はい、確かにmasterにcommitがまだないよって書かれています。
ちなみに現時点でもbranch名は表示されていません。

それでは
git commit
git log
git branch

を入力してみます。

mbc:ディレクトリ名 ユーザー名$ git commit -m "first commit"
     ~~省略~~
mbc:ディレクトリ名 ユーザー名$  git log
Author: ユーザー <メールアドレス>
Date:   Mon Feb 8 15:52:34 2021 +0900

    first commit
mbc:ディレクトリ名 ユーザー名$ git branch
* master

git commitをしたらbranch名が出て来ました!

結論

結構初歩的なつまづきですね。お恥ずかしい限りです。
やっぱりプログラミング周りの予備知識ってメインのプログラミングよりも難しいですね。もっと学ばねば。
おわりー。

16
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
7