🗝️

Gitコマンド入門::checkout(push -u)「第四十回」

2021/03/09に公開

みなさん、こんにちは! 第四十回になって、なんと、git checkout に辿り着きました。というか、一番最初に学習するべきな、、、ビックコマンドなんですけどね~ そもそも前回も書きましたけど、switch , restore コマンドが、このcheckout の変わりとして、2.2xぐらいから出てきたのもあって、restore から入ってしまったのが、このような経緯を生んでしまいましたね。ここまで、購読して頂いた方には、ちょっと申し訳ないと思っていますよ~(^▽^;)

前回の記事は、こちら!

https://zenn.dev/shiozumi/articles/013d34e19fdba1

git本家本元の情報はこちらから!

https://git-scm.com/book/ja/v2

では、今日の学習リポジトリーは、ありません!(笑)

たまには、なんにもないところから、ゼロ0ベースでやって行きましょう!
ここまで、なんとなくですが、gitを使ってきているので、ここからは、その仕組みについても、もう少し深堀して、さらに理解を深めて来たいと思います。

// まずは、適当にフォルダーを作成して!
$ mkdir temp0040
$ cd temp0040

// いつもの、README.md を作成
$ echo "#  git checkout" >> README.md

// init,add,commit
$ git init
$ git add README.md
$ git commit -m "1st"

// ここで、ブランチを確認すると、master のみですね。
$ git branch
* master

// masterブランチからチェックアウトして、subブランチを作成!
$ git checkout -b sub
Switched to a new branch 'sub'

// ここで、ブランチを確認すると、master,subとなります。
$ git branch
  master
* sub

// では、git log で確認
$ git log --oneline
2e7e4c4 (HEAD -> sub, master) 1st

// subブランチからチェックアウトして、tempブランチを作成!
$ git checkout -b temp
Switched to a new branch 'temp'

// はい! 3つのブランチが出来ました!
$ git branch
  master
  sub
* temp

$ git log --oneline
2e7e4c4 (HEAD -> temp, sub, master) 1st
// HEAD -> の部分が、temp,sub,master と続いていますね。

$ git status
On branch temp
nothing to commit, working tree clean
// ワーキングディレクトリ、ステージングエリア、すべて初期化されています。

git checkout -b <ブランチ名>

このコマンドは、現在のブランチから、-b で指定された新しいブランチ名で作成するとともに、環境そのものも全て更新しますから、ワーキングディレクトリ、ステージングエリアも、その時点のコミットに完全にリセットされますね。

ブランチの作成 git branch <ブランチ名>

では、今度はブランチのみの作成です。

$ git branch zzz

$ git branch
  master
  sub
* temp  // <!-- ポインタは、temp のままですね。
  zzz   // <!-- 新しく、zzz ブランチが作成されました。
  
$ git checkout zzz
Switched to branch 'zzz'

$ git branch
  master
  sub
  temp
* zzz   // <!-- ブランチも、zzzに切り替わりました。

$ git log --oneline
2e7e4c4 (HEAD -> zzz, temp, sub, master) 1st
// どうやら、ポインターのハッシュ値は、全て同じ用ですね。

ブランチ名は異なっても、ハッシュ値は同じ値です!

ハッシュ値 ブランチ名 補足事項
2e7e4c4 main -
2e7e4c4 sub -
2e7e4c4 temp -
2e7e4c4 zzz -

さらに、git reflog で履歴を確認!

2e7e4c4 (HEAD -> zzz, temp, sub, master) HEAD@{6}: checkout: moving from temp to zzz
2e7e4c4 (HEAD -> zzz, temp, sub, master) HEAD@{7}: checkout: moving from sub to temp
2e7e4c4 (HEAD -> zzz, temp, sub, master) HEAD@{8}: checkout: moving from master to sub
2e7e4c4 (HEAD -> zzz, temp, sub, master) HEAD@{9}: commit (initial): 1st

git branch -M main

ここで、初期化でよく出てくる、コマンドを実行してみます!

$ git branch -M main

$ git branch
* main // <!--- zzz ブランチが、main に書き換わりました。
  master
  sub
  temp

git branch -D <ブランチ名>

次は、ブランチの削除を行ってみましょう!

$ git branch -D master
Deleted branch master (was 2e7e4c4).

$ git branch -D temp
Deleted branch temp (was 2e7e4c4).

$ git branch
* main
  sub
// これで、master,temp が無事に削除されました。

はい! これでスッキリしてきましたね。main と、sub のブランチのみとなりました。
次に、githubのリモートにリポジトリーに、pushしてみましょう!

その前に各自で新しいリポジトリを作成してくださいね!

https://zenn.dev/shiozumi/articles/b682aef41e8d8a

第一回で、githubに新しいプロジェクト、新規リポジトリ作成が解説してあります!
今回の私は、test で作成しました。

すると、以下のような、スクリプトが、gitHub上で表示されますね。

echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/shiozumi-makoto/test.git
git push -u origin main

では実際に、main,subを、pushしてみます!

// まずは、現在のconfig を確認してみます!
$ git config -l
user.name=Makoto Shiozumi
user.email=shiozumi@esmile-hd.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
// ここにも、リモートリポジトリの情報が表示されていませんね。

$ git remote -v
$
// なにも表示されませんね。ローカルのリポジトリーと、
// リモートのリポジトリーの関連付けが未設定の状態です。

この状態では、push 出来ませんので、以下の2行で行います。

  1. git remote add origin https://github.com/<user-name>/<リポジトリ名>.git
  2. git push -u origin main
// まずは、1番のコマンドを実行
$ git remote add origin https://github.com/shiozumi-makoto/test.git

$ git remote -v
origin  https://github.com/shiozumi-makoto/test.git (fetch)
origin  https://github.com/shiozumi-makoto/test.git (push)
// これで、リポジトリーの設定が出来ました。

$ git config -l
user.name=Makoto Shiozumi
user.email=shiozumi@esmile-hd.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

// 以下の2行も新しく追加されました!
remote.origin.url=https://github.com/shiozumi-makoto/test.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

// ブランチの詳細表示も合わせて確認してみましょう!
git branch -vv

$ git branch -vv
* main 2e7e4c4 1st
  sub  2e7e4c4 1st
// ブランチ名、コミットのハッシュ値、コメントのみ
// この状態では、まだ、push できません。

次は2番、git push -u origin <ブランチ名>

ここでは、-u オプションを使わずに、pushしてみますね。
ちなみに、-u は、--set-upstream と同じです。要するに、ロカールブランチとリモートブランチの関係づけも、pushと同時に行ってしまうという、便利オプションなんですね。

git config branch.main.<remote/merge>

// では、-u オプションの代替えコマンドの2つを実行します!
$ git config branch.main.remote origin
$ git config branch.main.merge refs/heads/main

git config -l
// ~ 中略
branch.main.remote=origin
branch.main.merge=refs/heads/main
// この2行が追加されていたら、OK!

それぞれの2行についての説明は、以下の通りとなります。

branch.main.remote=origin

ブランチ名:mainのリモートリポジトリは、origin となります。
origin は、git remote add origin https://github.com/<user-name>/<リポジトリ名>.git で、設定しましたね。

branch.main.merge=refs/heads/main

ブランチ名:mainのマージの紐づけは、refs/heads/main となります。
実際に、refs/heads/main の中身を見れば、コミットのハッシュ値が確認できますね。

$ cat .git/refs/heads/main
2e7e4c45825763a66c373ca71eb23afcf1d3a4ff
// 先ほどから何回も出てくる、HEADが指しているハッシュ値ですね。

$ git branch -vv
* main 2e7e4c4 [origin/main: gone] 1st
  sub  2e7e4c4 1st
// 関連付けが完了したので、ハッシュ値の後ろには、
// [origin/main: gone] と表示されました。

git push origin main

これで、-u オプションを着けずに、実行できます!

$ git push origin main
Username for 'https://github.com': shiozumi-makoto
Password for 'https://shiozumi-makoto@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 229 bytes | 229.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/shiozumi-makoto/test.git
 * [new branch]      main -> main
$

私のgithubのキャプチャー画面です!

origin/main: gone の gone が消えました!

$ git branch -vv
* main 2e7e4c4 [origin/main] 1st
  sub  2e7e4c4 1st

git push -u origin sub では、-u オプション付きで実行してみましょう!

$ git push -u origin sub
Username for 'https://github.com': shiozumi-makoto
Password for 'https://shiozumi-makoto@github.com':
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'sub' on GitHub by visiting:
remote:      https://github.com/shiozumi-makoto/test/pull/new/sub
remote:
To https://github.com/shiozumi-makoto/test.git
 * [new branch]      sub -> sub
Branch 'sub' set up to track remote branch 'sub' from 'origin'.

[origin/sub] が追記されましたね。

$ git branch -vv
* main 2e7e4c4 [origin/main] 1st
  sub  2e7e4c4 [origin/sub] 1st
// origin/sub

$ cat .git/refs/heads/sub
2e7e4c45825763a66c373ca71eb23afcf1d3a4ff
// ハッシュ値も確認!

$ git config -l
// 中略~
branch.main.remote=origin
branch.main.merge=refs/heads/main
branch.sub.remote=origin
branch.sub.merge=refs/heads/sub
// mainと同じく、sub の設定も無事追加されました!
  1. branch.sub.remote=origin
  2. branch.sub.merge=refs/heads/sub

今回のまとめ

  1. ローカルのリポジトリの作成、切り替え、複製
  2. リモートリポジトリとローカルリポジトリの関連付け
  3. git push で、-u の有無の両方で実行する。

新しいコマンド基本3つ

  1. git checkout -b <新規ブランチ名> // ブランチ作成と切り替え同時
  2. git switch <ブランチ名> // ブランチの切り替え
  3. git branch <ブランチ名> // ブランチの作成

configファイルの設定「やや深堀り」

  1. git config -l // コンフィグ設定内容の表示
  2. git config branch.<ブランチ名>.remote origin
  3. git config branch.<ブランチ名>.merge refs/heads/<ブランチ名>

pushと確認コマンド!

  1. git push -u origin <ブランチ名> // リモートへのpushと関連付けを同時実行
  2. git remote add origin https://github.com/<user-name>/<リポジトリ名>.git
  3. git remote -v // origin と、リモートアドレスの関連付け表示
  4. git branch -vv // ブランチの詳細表示、リモートブランチとの関連づけ表示

では最後におまけで、configの中身もドン!!

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = https://github.com/shiozumi-makoto/test.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main
[branch "sub"]
        remote = origin
        merge = refs/heads/sub

それでは、今回はここまで、お疲れ様でした!

今回は、かなりの情報量となってしまって、やや駆け足で急いでしまいましたね。分かりづらいところもあるかと思います。ごめんなさい。次回は、復習も兼ねて、checkout, push, の復習を行って行きたいと思います!
https://zenn.dev/shiozumi/articles/cf14e1000334f5
https://twitter.com/esmile2013

脚注
  1. 現在のヘッダーは、zzz ブランチです。これを、git switch master などで切り替えると、HEAD -> master, zzz, temp, sub という順序で、reflogに表示されます。 ↩︎

Discussion