SlideShare ist ein Scribd-Unternehmen logo
1 von 59
Downloaden Sie, um offline zu lesen
GitのHEADって何?
2021年08月21日
自己紹介
名前: 三宅 英明
Twitter: @mollifier
神戸のプログラマ
GitのHEADって何?
GitのHEADって何?
Gitの説明を読んでいると、HEADというものが出てき
ます
GitのHEADって何?
この本(新しいLinuxの教科書)にも出てきます
GitのHEADって何?
$ git diff HEAD
GitのHEADって何?
$ git checkout HEAD .
GitのHEADって何?
「HEADって何?」
「唐突すぎて意味分かんない」
GitのHEADって何?
このHEADとは一体何なのかを説明します
GitのHEADって何?
Gitコマンドにはglossary(用語集)というものがついて
いて、そこにHEADの説明も書いてあります。
$ git help glossary
または
$ man gitglossary
GitのHEADって何?
The current branch. In more detail: Your
working tree is normally derived from the
state of the tree referred to by HEAD. HEAD
is a reference to one of the heads in your
repository, except when using a detached
HEAD, in which case it directly references
an arbitrary commit.
GitのHEADって何?
「HEADとは、要するにcurrent branch(現在のブ
ランチ)だよ」
GitのHEADって何?
「もっと詳しく言うと、HEADとはheadのうちのど
れか一つを指し示す参照だよ」
GitのHEADって何?
じゃあブランチとかhead(小文字)って何?
Gitのブランチって何?
ブランチとは
Gitのブランチって何?
ブランチとは、噛み砕いて言うと「枝分かれした開発
の流れ」のことなのですが
Gitのブランチって何?
ブランチの実体は、リポジトリの中の.git/refs/heads/
ディレクトリの下にあります
Gitのブランチって何?
feature-1ブランチとmasterブランチがあるリポジトリ
の例
$ ls .git/refs/heads/
feature-1 master
Gitのブランチって何?
feature-1、master、と2つファイルがあります
Gitのブランチって何?
.git/refs/heads/の下にあるファイルのことをheadまた
はhead refと呼びます。 これがブランチの先頭を指し
示すファイルです。
Gitのブランチって何?
ブランチの先頭、ということでheadという名前がつけ
られたのでしょう
Gitのブランチって何?
中身はただのテキストファイルです
$ cat .git/refs/heads/feature-1
54c5f13f3c4bf8b49ffe3dd499903cbe278868ca
Gitのブランチって何?
これは
feature-1ブランチの先頭は
54c5f13f3c4bf8b49ffe3dd499903cbe278868caと
いうコミットですよ
ということを表しています
Gitのブランチって何?
他のブランチも同様です。
$ cat .git/refs/heads/master
f90ca53d778c708330013f80acb884914ec115ae
Gitのブランチって何?
ここまでのまとめ
Gitのブランチって何?
.git/refs/heads/の下にhead(またはhead ref)と
呼ばれるファイルがある
headを見れば、各ブランチの先頭がどのコミット
になっているのか分かる
GitのHEADって何?
じゃあHEAD(大文字)って何?
GitのHEADって何?
あるGitリポジトリに複数のコミットがあったとします
GitのHEADって何?
Gitのワークツリー(作業コピー)には、そのリポジトリの
どこかのコミット(版)が展開されています。どのコミット
が展開されているのかを指し示しているのがHEAD
です。
GitのHEADって何?
.gitディレクトリの中にHEADというファイルがありま
す。これがHEADの実体です。
GitのHEADって何?
例
GitのHEADって何?
現在のブランチはmasterブランチだとします。
HEADはmasterブランチを指し示しています。
$ cat .git/HEAD
ref: refs/heads/master
GitのHEADって何?
feature-1ブランチに切り替えたとします
$ git checkout feature-1
Switched to branch 'feature-1'
GitのHEADって何?
HEADがfeature-1ブランチを指し示すようになりまし
た
$ cat .git/HEAD
ref: refs/heads/feature-1
GitのHEADって何?
git checkoutコマンドは、後ろにhead(head ref)を指
定している、と解釈できます
$ git checkout feature-1
そして、そのとき指定したheadがHEADとして保存さ
れます
GitのHEADって何?
現在のheadという意味で、HEADと大文字の名前が
つけられたのでしょう
GitのHEADって何?
git checkoutコマンドはブランチだけではなく、特定の
コミットも指定できます
$ git checkout
075950f107f25f69b0e6d91194063861063bc618
Note: switching to
'075950f107f25f69b0e6d91194063861063bc618'
GitのHEADって何?
その場合、HEADはその特定のコミットを指し示して
いる状態になります
$ cat .git/HEAD
075950f107f25f69b0e6d91194063861063bc618
GitのHEADって何?
要するに、HEADはgit checkoutの後に指定したブラ
ンチやコミットのことです
GitのHEADって何?
多くの場合git checkoutの後にブランチ名を指定する
ので、HEADはそのブランチを指し示した状態になっ
ています
GitのHEADって何?
ここまでのまとめ
git checkoutコマンドの後ろに指定したブランチ
やコミットがHEADになる
GitのHEADって何?
で、HEADがあると何がうれしいのか?
GitのHEADって何?
たいていの場合、HEADは現在のブランチを指し示し
ています
GitのHEADって何?
逆に、現在のブランチを指定したいとき代わりに
HEADとも指定できるようになっています
GitのHEADって何?
最初に出した例
$ git diff HEAD
GitのHEADって何?
$ git diff HEAD
「リポジトリに最後にコミットしたところ」から「現在の
ワークツリー」までの差分を見るコマンド
GitのHEADって何?
そもそもgit diffコマンド後に1つ特定のコミットを指定
すると、「指定したコミット」から「ワークツリー」までの
差分が表示される
$ git diff
1f61256e66fda560ecf06190930aabc76c84e675
GitのHEADって何?
代わりに1つブランチを指定してもよい。そうした場
合、「指定したブランチの先頭」から「ワークツリー」ま
での差分が表示される
GitのHEADって何?
「feature-1ブランチの先頭」から「ワークツリー」まで
の差分が表示される
$ git diff feature-1
GitのHEADって何?
代わりにHEADと指定してもよい。そうした場合、
「HEAD」から「ワークツリー」までの差分が表示され
る
GitのHEADって何?
HEADは(たいていの場合)現在のブランチを指し示し
ているので、結局
$ git diff HEAD
で「現在のブランチの先頭」から「ワークツリー」まで
の差分が表示される
GitのHEADって何?
例えば現在のブランチがfeature-1だとして、
$ git diff HEAD
$ git diff feature-1
この2つはまったく同じ結果になる
GitのHEADって何?
どっちを使ってもよい。ただ、現在のブランチの名前は
今作業しているリポジトリによって変わる。HEADを使
えばリポジトリの状態によらず現在のブランチを指定
できる
GitのHEADって何?
git diffでもcheckoutでもpushでも、どんなコマンドで
もHEADは使えます
GitのHEADって何?
引数に現在のブランチを指定したいことはよくある。
そういうときHEADを指定すると楽になる。
GitのHEADって何?
絶対HEADを指定しないとだめ、というわけではな
い。明示的にブランチ名を指定してもよい。
GitのHEADって何?
つまり、HEADは現在のブランチを指定するショートカ
ットとして使える
GitのHEADって何?
まとめ
HEADはgit checkoutで指定したブランチやコミ
ットを指し示している
逆にgit checkoutしたものを指定したい時のショ
ートカットとしてHEADを指定してもよい
GitのHEADって何?
これでもうHEADの使い方で迷わないでしょう

Weitere ähnliche Inhalte

Empfohlen

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Empfohlen (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

GitのHEADって何?