LoginSignup
0
0

More than 1 year has passed since last update.

ディレクトリをgitの管理下に置いてブランチを作成するまでの流れ

Last updated at Posted at 2022-08-14

 Gitで管理していないディレクトリをgit initでGitの管理下に置く場合、最初にコミットをしておかないとブランチが作成できない。ただコミットする中身はまだないので、空のコミットを行う。空のコミットというのは、ステージに何も変更がないのにコミットをするということ。そのような状態では通常コミットは出来ないけど、--allow-emptyをつければ出来る。

# ディレクトリを作成
$ mkdir git-practice

# 作成したディレクトリに移動
$ cd git-practice/

# まだ.gitはない
$ ls -a
./  ../

# ディレクトリをGitの管理下に置く
$ git init
Initialized empty Git repository in hoge/git-practice/.git/

# .gitが出来ている
$ ls -a
./  ../  .git/

# 何も表示されない
$ git branch

# ブランチの作成に失敗してしまう
$ git branch sample
fatal: Not a valid object name: 'master'.

# --allow-emptyなしでコミットしようとしても出来ない
$ git commit -m"init"
On branch sample
nothing to commit, working tree clean

# 空のコミットをする
$ git commit --allow-empty -m"init"
[master (root-commit) abc125d] init

# コミットが履歴に追加されている(結果は割愛)
$ git log

# ブランチの一覧が見られるようになった
$ git branch
* master

# sampleブランチを作成
$ git branch sample

# 一覧に追加されてる
$ git branch
* master
  sample

# ブランチをsampleに切り替え
$ git checkout sample
Switched to branch 'sample'
0
0
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
0
0