<メリット>
<デメリット>
# バージョン表示
$ git --version
# ユーザー名の登録
$ git config --global user.name "github user name"
# E-mailアドレスの登録
$ git config --global user.email "github@sample.ne.jp"
# エディタの登録
# --- コミットメッセージを書く際に使用
$ git config --global core.editor "atom --wait"
$ git config --global core.editor "code --wait"
# commit
$ git config --global alias.ci commit
# status
$ git config --global alias.st status
# branch
$ git config --global alias.br branch
# checkout
$ git config --global alias.co checkout
# コンソール出力の色付け
$ git config --global color.ui true
# 変更をフローチャート表示
git config --global alias.graph 'log --graph --decorate --oneline'
# Fast-Fowardマージを禁止する
# --- ff:fast-foward
$ git config --global merge.ff false
# ユーザー名の確認
$ git config user.name
# E-mailアドレスの確認
$ git config user.email
# エディタの確認
$ git config core.editor
# 設定全体の確認
$ git config --list
# 設定の保存場所
$ cat ~/.gitconfig
# **** フォルダ構造の確認 ****
# ローカルリポジトリの新規作成
# --- 適当なディレクトリで実行
$ git init
# 「.git」ディレクトリの確認
$ ls -a
./ ../ .git/
# 「.git」ディレクトリの中を確認
$ ls .git
COMMIT_EDITMSG description hooks/ info/ objects/
config HEAD index logs/ refs/
git add
で変更状況をステージに追加して、その後コミットするという2段階の操作を行うgit add
でステージに追加する前は、Gitは1つの変更として扱うgit add
でステージに追加した後は、Gitはファイルの変更を個別に扱う<ポイント>
# **** 構文 ****
# 行単位でコミット
$ git add -p <file>
<ここにコミットメッセージを記述>
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: index.html
#
<ファイルの書き方>
# 空のディレクトリ作成
$ mkdir <new_dir>
# ディレクトリ移動
$ cd <new_dir>
# ファイル作成
$ touch .gitkeep
<準備>
<ローカルでの作業>
<リモートでの作業>
# **** ハンズオン ****
# ファイル新規作成
$ touch test.txt ※Git管理するファイル
$ touch secret.txt ※Git管理しないファイル
# 除外ファイルの設定ファイル
$ cat .gitignore
secret.txt
# ファイル一覧
# --- 「.gitignore」ファイルは隠しファイルなので出力されない
$ ls
index.html secret.txt
# ステータスの確認
# --- secret.txtが除外されている
# --- 「add」コマンドでも同様に除外される
$ git status -s
?? .gitignore
?? test.txt
# **** 構文 ****
# カレントディレクトリに「.git」を作成
$ git init
# 新規フォルダを作成して「.git」を作成
$ git init <フォルダ名>
# **** ハンズオン1:カレントディレクトリに作成 ****
# カレントディレクトリに「.git」を作成
$ git init
# 「.git」ディレクトリの確認
$ ls -a
./ ../ .git/
# **** ハンズオン2:新規フォルダに作成 ****
# 新規フォルダを作成して「.git」を作成
$ git init proj_x
# 「.git」ディレクトリの確認
$ ls -a
proj_x/
# ディレクトリの移動
$ cd proj_x
# 「.git」ディレクトリの確認
$ ls -a
./ ../ .git/
# **** 構文 ****
# リモートリポジトリのクローンを作成
$ git clone <url>
# **** ハンズオン ****
# リモートリポジトリのクローンを作成
# --- テスト用:atomプロジェクト
$ git clone https://github.com/atom/atom.git
# ディレクトリ移動
$ cd atom
# ファイル一覧の確認
# --- 隠しファイルも併せて確認
$ ls -a
./ benchmarks/ package-lock.json
../ CHANGELOG.md packages/
.coffeelintignore CODE_OF_CONDUCT.md PULL_REQUEST_TEMPLATE.md
.eslintignore coffeelint.json README.md
.eslintrc.json CONTRIBUTING.md resources/
.git/ Dockerfile script/
.gitattributes docs/ spec/
.github/ dot-atom/ src/
.gitignore exports/ static/
.prettierrc keymaps/ stylelint.config.js
.python-version LICENSE.md SUPPORT.md
apm/ menus/ vendor/
atom.sh* package.json
<準備>
<複数のリモートリポジトリ登録について>
# **** 構文 ****
# リモートリポジトリを登録
# --- 以下の構文は、Githubでリポジトリを作成する際にページ上に表示される
$ git remote add <リモート名> <リモートURL>
# **** ハンズオン ****
# 登録 --------------------------------
# リモートリポジトリの追加:1つめ
$ git remote add origin https://github.com/xxxxxx/git_tutorial.git
# リモートリポジトリの追加:2つめ
$ git remote add bak https://github.com/user_name/git_tutorial_bak.git
# 確認 --------------------
# 登録したリポジトリ一覧を確認
$ git remote
bak
origin
# リポジトリの登録URLの確認
$ git remote -v
bak https://github.com/user_name/git_tutorial_bak.git (fetch)
bak https://github.com/user_name/git_tutorial_bak.git (push)
origin https://github.com/user_name/git_tutorial.git (fetch)
origin https://github.com/user_name/git_tutorial.git (push)
# **** 構文 ****
# リモートリポジトリを表示
$ git remote
# 対応するURLも併せて表示
$ git remote -v
# **** ハンズオン ****
# リモートリポジトリを表示
$ git remote
origin
# 対応するURLも併せて表示
# --- fetchとpushでURLを変更することができるので2つ表示される
$ git remote -v
origin https://github.com/user_name/git_tutorial.git (fetch)
origin https://github.com/user_name/git_tutorial.git (push)
<表示される内容>
# **** 構文 ****
# リモートから情報を取得してマージする
$ git remote show <リモート名>
# **** ハンズオン ****
# 情報を確認
$ git remote show origin
* remote origin
Fetch URL: https://github.com/user_name/git_tutorial.git
Push URL: https://github.com/user_name/git_tutorial.git
HEAD branch: master
Remote branch:
master tracked
Local ref configured for 'git push':
master pushes to master (up to date)
# **** 構文 ****
# 名前を変更
$ git remote rename <旧リモート名> <新リモート名>
# **** ハンズオン ****
# 準備:現在登録しているリモートの確認
$ git remote
bak
origin
# リモートリポジトリの名前変更
# --- コマンド出力なし
$ git remote rename bak bakup
# 変更を確認
$ git remote
bakup
origin
# **** 構文 ****
# リモートを削除
$ git remote rm <リモート名>
# **** ハンズオン ****
# 準備:現在登録しているリモートの確認
$ git remote
bakup
origin
# リモートリポジトリを削除
# --- コマンド出力なし
$ git remote rm bakup
# 削除を確認
$ git remote
origin
<フロー>
# **** 構文 ****
# 変更をステージに追加する
$ git add <ファイル名>
$ git add <ディレクトリ名>
$ git add .
# **** ハンズオン ****
# 新規ファイルを作成
$ touch test.txt
# ファイルをステージに追加
$ git add .
# **** 構文 ****
# ステージの変更をリポジトリに記録する
$ git commit
# オプション:-m
# --- Gitのエディタを立ち上げることなくメッセージを追加することができる
$ git commit -m "<メッセージ>"
# オプション:-v
# --- エディタ上に変更内容を表示する
$ git commit -v
# **** ハンズオン ****
# エディタを開く
# --- 作業ディレクトリにファイルを作成&保存
$ atom
# 保存されたファイルを確認
$ ls
# ステージに追加
$ git add .
# **** 構文 ****
# ファイルごとの移動
$ git push <リモート名> ><ブランチ名>
$ git push origin master
# オプション:-u
# --- origin masterを今後省略できるように設定
git push -u origin master
# **** Gitにプッシュ ***
# プッシュするリポジトリに移動
# --- 右クリックの「Git Bush Here」でカレントディレクトリでGit Bashを開始してもよい
cd local_repos
# プッシュ
git push -u origin master
# **** 構文 ****
# 現在の変更状況を確認する
$ git status
# **** ハンズオン:準備 ****
# ステータス確認(準備前)
# --- コミットするファイルがないことを確認
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
# 準備
# --- Git管理環境に新規ファイルを作成
$ touch test.txt
$ echo "test" > test.txt
# **** ハンズオン:ステージに追加 ****
# ステージに追加
$ git add test.txt
# ステータス確認(add後)
# --- ステージに変更が反映されていないことを検知
$ git status
On branch master
No commits yet
Changes to be committed: ※変更(ステージに追加)されたけどコミットされていないファイルがある
(use "git rm --cached <file>..." to unstage)
new file: test.txt
# **** ハンズオン:コミット ****
# コミット
$ git commit -m "initial commit"
[master (root-commit) 32d5efd] initial commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
# ステータス確認
# --- リポジトリに変更が反映されていないことを検知(コミットされていないファイルを教えてくれる)
$ git status
On branch master
nothing to commit, working tree clean ※コミットされていないファイルはない
<ステータス表記>
# **** 構文 ****
# 現在の変更状況をコンパクトに表示する
$ git status -s
# **** ハンズオン ****
# Git管理前 --------------------------------
# 準備
# --- Git管理環境に新規ファイルを作成
$ touch test2.txt
$ echo "test" > test.txt
# ステータス確認
$ git status -s
?? test.txt ※Git管理外のファイルに「??」がつく
# ステージ追加後 --------------------------------
# ステージに追加
$ git add test.txt
# ステータス確認
$ git status -s
A test.txt ※Addされたファイルに「A:Add」がつく
# ステージ追加後に変更 --------------------------------
# ファイル内容を変更
$ echo Hello World! >> test.txt
# ステータス確認
$ git status -s
AM test.txt ※変更されたファイルに「M:Modify」がつく
# ステージに再度追加 --------------------------------
# ステージに追加
$ git add test.txt
# ステータス確認
$ git status -s
A test.txt
# コミット後 --------------------------------
# コミット
$ git commit -m "initial commit"
# ステータス確認
# --- 何も表示されない
$ git status -s
# **** 構文 ****
# 変更差分の表示
# --- 「ワークツリー」と「ステージ」の差分(git addを行う前)
$ git diff
$ git diff <ファイル名>
# オプション:--staged
# --- 「リポジトリ」と「ステージ」の差分(git commmitを行う前)
$ git diff --staged
# **** ファイルを変更して変更差分を表示 ****
# エディタを起動
# --- ファイルを変更
$ atom
# ステータス確認
# --- 「ワークツリー」と「ステージ」に変更差分がないことを確認
$ git diff
diff --git a/index.html b/index.html
index d31eea4..47028db 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
<h1>Gitチュートリアル</h1>
<p>git status</p>
+<p>git diff</p>
# **** 変更ファイルをステージに追加 ****
# ステージに追加
# --- 「ワークツリー」と「ステージ」の変更差分が解消
$ git add index.html
# ステータス確認
# --- 「ワークツリー」と「ステージ」に変更差分がないことを確認
$ git diff
# ステータス確認
# --- 「ステージ」と「リポジトリ」に変更差分があることを確認
$ git diff --staged
diff --git a/index.html b/index.html
index d31eea4..47028db 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
<h1>Gitチュートリアル</h1>
<p>git status</p>
+<p>git diff</p>
# **** 変更ファイルをリポジトリに反映 ****
# コミット
# --- エディタにコミットメッセージを作成
$ git commit
# ステータス確認
# --- 「ワークツリー」と「ステージ」に変更差分がないことを確認
$ git diff
# ステータス確認
# --- 「ステージ」と「リポジトリ」に変更差分がないことを確認
$ git diff --staged
<別画面の操作>
# **** 構文 ****
# 変更履歴の表示
$ git log
# オプション:--oneline
# --- 1行で表示
$ git log --oneline
# オプション:-n
# --- 表示するコミット数を制限
$ git log -n
# オプション:-p
# --- 「git log」と「git diff」を同時に表示
$ git log -p index.html
# **** ハンズオン ****
# 変更履歴の表示
$ git log
commit 4d91580973b8cfdd167930ee7b9d19e73bab9e78 (HEAD -> master)
Author: user_name <user_name@git.com>
Date: Sat Jan 00 06:54:22 2000 +0000
initial commit
# 1行表示 ※推奨
$ git log --oneline
4d91580 (HEAD -> master) initial commit
# 「git log」と「git diff」を同時に表示
$ git log -p
commit 4d91580973b8cfdd167930ee7b9d19e73bab9e78 (HEAD -> master)
Author: user_name <user_name@git.com>
Date: Sat Jan 25 06:54:22 2020 +0900
initial commit
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..980a0d5
--- /dev/null
+++ b/test.txt
@@ -0,0 +1 @@
+Hello World!
# **** 構文 ****
# ファイルごとの削除
# --- ファイルとgit履歴の両方を削除したい
$ git rm <ファイル名>
$ git rm -r <ディレクトリ名>
# オプション:--cached
# --- gitの記録は削除したいが、ファイルは残したい
$ git rm --cached <ファイル名>
# **** ハンズオン:ファイルと履歴の削除 ****
# 準備:ステージに追加
$ git add test.txt
# 指定したファイルを削除
$ git rm test.txt
rm 'test.txt'
# ファイル一覧の確認
# --- ファイルが削除されている
$ ls
# ステータスの確認
# --- 削除した操作がステージに記録されている
$ git status -s
D test.txt
# **** ハンズオン:ファイルの履歴のみ削除 ****
# 指定したファイルを削除
# --- 「リポジトリからの削除」のみが行われる
$ git rm --cached test.txt
rm 'test.txt'
# ファイル一覧の確認
# --- ファイルは削除されていない
$ ls
test.txt
# ステータスの確認
# --- 削除した操作がステージに記録されている
$ git status -s
D test.txt
?? test.txt
# **** 構文 ****
# ファイルごとの移動
$ git mv <旧ファイル名> <新ファイル名>
# 以下の動作度同じ
$ git rm <旧ファイル名>
$ git add <新ファイル名>
# **** ハンズオン ****
# 準備:ステージに追加
$ git add test.txt
# 準備:ステータスの確認
$ git status -s
A test.txt
# ファイル名の変更
$ git mv test.txt test2.txt
# ファイル一覧の確認
# --- ファイル名が変更されている
$ ls
index2.html
# 変更状態を確認
$ git status -s
A test2.txt
<pullの注意点>
# **** ハンズオン ****
# 現在のブランチの確認
$ git branch
* feature ※現在のブランチ
master ※マージしたいブランチ
# (重要)ブランチの切り替え
# --- 現在のブランチをマージしたいブランチに切り替えておく
$ git checkout master
# **** 構文 ****
# リモートリポジトリを表示
$ git fetch <リモート名>
# **** ハンズオン ****
# 準備 --------------------------------
# ワークツリーのファイル確認
$ ls
index.html
# リモートにファイル追加
# --- 通常は他のメンバーが更新したリモートリポジトリを取り込む
# --- Githubから直接作成(ファイル名:git_fetch.txt)
# リモートから取得 ---------------------
# リモートリポジトリの取得
# --- ローカルリポジトリにダウンロードされる
$ git fetch origin
remote: Enumerating objects: 4, done.
# 取得ファイルの確認 ---------------------
# 取得ファイルの保存場所の確認
# --- 「-a」はallの略
# --- 現在指定しているブランチには「※」が付いている
$ git branch -a
* master
remotes/bak/master
remotes/origin/master ※ここにリモートリポジトリから取得した情報が格納されている
# ブランチの切り替え
$ git checkout remotes/origin/master
Note: switching to 'remotes/origin/master'.
# ファイルの確認
# --- 「git_fetch.txt」が存在する
$ ls
git_fetch.txt
# ワークツリーに反映 ---------------------
# 反映先のブランチに切り替え
# --- 「merge」コマンドは現在選択しているブランチに統合されるため
$ git checkout origin/master
# 取得ファイルをワークツリーに反映
$ ls
index.html git_fetch.txt
# **** 構文 ****
# リモートから情報を取得してマージする
$ git pull <リモート名> <ブランチ名>
$ git pull origin master
# 上記のコマンドは省略可能
$ git pull
# 上記のコマンドは以下のように書き換え可能
$ git fetch origin master
$ git merge origin/master
# **** ハンズオン ****
# 準備 --------------------------------
# ワークツリーのファイル確認
$ ls
index.html
# リモートにファイル追加
# --- 通常は他のメンバーが更新したリモートリポジトリを取り込む
# --- Githubから直接作成(ファイル名:git_pull.txt)
# リモートから取得&ワークツリーに反映 ---------------------
# 現在のブランチの確認
# --- マージするブランチにいることを確認
$ git branch
feature
* master
# 他のブランチにいたら「checkout」で移動
$ git checkout master
# リモートから取得&ワークツリーに反映
$ git pull origin master
# ワークツリーのファイル確認
# --- ファイルの追加が反映されている
$ ls
index.html git_pull.txt
<リセットレベル>
# **** 構文 ****
# コミットのみ取消
# --- ステージングとファイルの変更は維持
$ git reset --soft <コミットID>
# コミットとステージングを取消
# --- ファイルの変更は
$ git reset --mixed <コミットID>
# 全てを取消
# --- ファイルは削除される
$ git reset --hard <コミットID>
<処理の流れ>
# **** 構文 ****
# 特定のコミットを打ち消すコミットを作成
$ git revert <commit>
# 打ち消すだけでコミットは作成したくない場合
$ git revert -n <commit>
# マージコミットを打ち消す
$ git revert -m 1 <commit>
commit --amend
で追加&修正することができる<Git Krakenの場合>
# **** 構文 ****
# 直前のコミットを修正
$ git commit --amend
squash
を用いる<Git Krakenの場合>
shiftキー
を押しながら複数選択するsquash n commit
を選択する# **** 構文 ****
# 直前のコミット
git merge --squash <branch or commit>
git rebase -i [commit]
# **** 構文 ****
# ファイルの履歴のみを削除
$ git rm --cached <ファイル名>
# **** ハンズオン ****
# --- 準備 ----------
# ステージに追加
$ git init
# 新規ファイル作成
$ touch test.txt
# ステータス確認
# --- ファイルがステージに追加されていない
$ git status -s
?? test.txt
# ステージに追加
$ git add test.txt
# ステータス確認
# --- ファイルがステージに追加されている
$ git status -s
A test.txt
# --- ステージから取り消す ----------
# 履歴のみ削除
# --- ファイル自体は削除されない
$ git rm --cached test.txt
rm 'test.txt'
# ステータス確認
# --- ファイルがステージから削除されてGit管理から外れている
$ git status -s
?? test.txt
<Gitがやっていること>
# **** 構文 ****
# ファイルを元に戻す
# --- 最新のコミット(HEAD)の状態にリセットするという意味
$ git reset HEAD <ファイル名>
# ディレクトリを元に戻す
$ git reset HEAD <ディレクトリ名>
# 全変更を取り消す
$ git reset HEAD .
# **** ハンズオン:環境準備 ****
# --- 準備1:最初のコミット ----------
# ステージに追加
$ git init
# 新規ファイル作成
$ touch test.txt
# ステージに追加
$ git add test.txt
# コミット
$ git commit -m "Initial Commit"
[master (root-commit) 03e45f0] Initial Commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
# --- 準備2:別のファイルをステージに追加 ----------
# 新規ファイル作成
$ touch test2.txt
# ステージに追加
$ git add test2.txt
# ステータス確認
# --- ファイルが追加されている
$ git status -s
A test2.txt
# **** ハンズオン:ステージから取り消す ****
# HEADを一つ前に戻す
# --- ファイルを追加する前
$ git reset HEAD .
# ステータス確認
# --- ファイルがステージから削除されてGit管理から外れている
$ git status -s
?? test2.txt
# **** 構文 ****
# 直前のコミットを取り消す
# --- ワーキングディレクトリの内容も書き換える
$ git reset --soft HEAD^
# 直前のコミットを取り消す
# --- ワーキングディレクトリの内容も書き換える
$ git reset --hard HEAD^
# **** ハンズオン:準備 ****
# --- 準備1:最初のコミット ----------
# ステージに追加
$ git init
# 新規ファイル作成
$ touch test.txt
# ステージに追加
$ git add test.txt
# コミット
$ git commit -m "Initial Commit"
[master (root-commit) 03e45f0] Initial Commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
# --- 準備2:2回目のコミット(取り消し対象) ----------
# 新規ファイル作成
$ touch test2.txt
# ステージに追加
$ git add test2.txt
# コミット
$ git commit -m "Second Commit"
[master (root-commit) 03e45f0] Initial Commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
# --- 環境確認 ----------
# ログの確認
$ git log --oneline
78b3af3 (HEAD -> master) Second Commit
198f2bb Initial Commit
# **** ハンズオン:--soft ****
# 直前のコミットの取り消し
$ git reset --hard HEAD^
HEAD is now at 198f2bb Initial Commit
# ログの確認
# --- コミット履歴が削除されている
$ git log --oneline
198f2bb (HEAD -> master) Initial Commit
# ファイル確認
# --- ファイルが存在する(直前の操作が保存されている)
$ ls
test.txt test2.txt
# **** ハンズオン:--hard ****
# 直前のコミットの取り消し
$ git reset --hard HEAD^
HEAD is now at 198f2bb Initial Commit
# ログの確認
# --- コミット履歴が削除されている
$ git log --oneline
198f2bb (HEAD -> master) Initial Commit
# ファイル確認
# --- ファイルが削除されている(直前の操作も併せて取り消している)
$ ls
test.txt
commit --amend
で行うことができる<注意>
<Gitがやっていること>
# **** 構文 ****
# 直前のコミットを修正
$ git commit --amend
# **** ハンズオン ****
# --- 準備 ---
# ファイル内容の確認
$ cat test.txt
git commit
# ステージに追加
$ git add test.txt
# コミット
$ git commit -m "git commit --amend"
[master 5782cc7] git commit --amend
1 file changed, 1 insertion(+)
create mode 100644 test.txt
# --- 準備 ---
# ファイル内容の修正
$ cat test.txt
git commit --amend
# ステージに追加
$ git add test.txt
# コミットの修正
$ git commit --amend -m "git commit --amend"
[master 3d96e07] git commit --amend
Date: Sun Jan 26 07:44:18 2020 +0900
1 file changed, 1 insertion(+)
create mode 100644 test.txt
<注意>
<Gitがやっていること>
# **** 構文 ****
# 直前のコミットメッセージを修正
# --- エディタを使用
$ git commit --amend
# 直前のコミットメッセージを修正
# --- メッセージを直接入力
$ git commit --amend -m "メッセージ"
# **** ハンズオン ****
# --- 準備:コミット ----------
# 新規ファイル作成
$ touch test.txt
# ステージに追加
$ git add test.txt
# コミット
$ git commit -m "Initial Commit"
[master (root-commit) 03e45f0] Initial Commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
# --- 準備:コミットメッセージを修正 ----------
# コミットメッセージを修正
$ git commit --amend -m "Initial Commit Modify"
[master 728db75] Initial Commit Modify
Date: Sat Jan 25 10:16:52 2020 +0900
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
# ステータスの確認
$ git log --oneline
728db75 (HEAD -> master) Initial Commit Modify ※修正されている
master
という名前になっている(マスターブランチは削除しない)test/test01
というブランチを作成することができるtest
はブランチではないので、あくまでtest/test01
を操作する<Gitがやってること>
<GitKrakenの場合>
create branch here
を選択するcheckout
する# **** 構文 ****
# ブランチの作成
# --- 直前コミットに対して行われる
$ git branch <ブランチ名>
$ git branch feature
# ブランチの作成
# --- コミットを指定して行う
$ git checkout -b <new_branch> <commit_hash>
# **** ハンズオン ****
# 新しいブランチの作成
$ git branch new_branch
# ブランチの確認
$ git branch
feature
* master
new_branch ※新規作成されている
# **** 構文 ****
# ローカルのブランチの確認
$ git branch
# オプション:-a
# --- リモートリポジトリから取得したブランチを含めて表示する
$ git branch -a
# **** ハンズオン ****
# ローカルのブランチの確認
$ git branch
feature
* master
# リモートのブランチを含めて表示する
# --- 「※」がついているのが現在いるブランチ
$ git branch -a
feature
* master
remotes/origin/feature
remotes/origin/master
# 現在のブランチの確認(別の方法)
$ git log --oneline --decorate
6258bda (HEAD -> master) Merge branch 'master' of https://github.com/user_name/git_tutorial
# **** 構文 ****
# 現在のブランチの詳細情報
$ git branch -v
# 全ブランチの詳細情報
$ git branch -vva
# **** ハンズオン ****
# 現在のブランチの詳細情報
$ git branch -v
feature 8ae96dc featureを追記
* master 6258bda Merge branch 'master' of https://github.com/user_name/git_tutorial
# 全ブランチの詳細情報
$ git branch -vva
feature 8ae96dc Commit Message
* master 6258bda Merge branch 'master' of https://github.com/user_name/git_tutorial
remotes/origin/feature 5bc791b Commit Message
remotes/origin/master f347e5b Commit Message
# **** 構文 ****
# ブランチの削除(どちらでも可)
$ git branch --delete <ブランチ名>
$ git branch -d <ブランチ名>
# マージされていないブランチの強制削除
$ git branch -D <ブランチ名>
# **** ハンズオン ****
# ブランチの削除
$ git branch -d new_branch
Deleted branch new_branch (was 6258bda).
# ブランチの確認
$ git branch
feature
* master
# **** 構文 ****
# ブランチ名の変更
$ git branch -m <古いブランチ名> <新しいブランチ名>
# 現在のブランチの名前変更
$ git branch -m <新しいブランチ名>
# **** ハンズオン ****
# 準備:現在のブランチ確認
$ git branch
feature
* master
new_branch
# ブランチ名の変更
$ git branch -m new_branch new2_branch
# ブランチの確認
$ git branch
feature
* master
new2_branch
<GitKrakenの場合>
# **** 構文 ****
# ブランチの作成
$ git checkout <既存ブランチ名>
$ git checkout feature
# オプション:-b
# --- ブランチを新規作成して切り替える
# --- 「b」はbranchのことを意味する
$ git checkout -b <新ブランチ名>
# **** ハンズオン ****
# 準備:ブランチの状況確認
$ git branch
* feature
master
# ブランチの切り替え
$ git checkout master
# ブランチの状況確認
$ git branch
feature
* master
# **** ハンズオン ****
# featureブランチ ------------------
# ブランチの状況確認
$ git branch
* feature
master
# 存在するファイル
$ ls
feature.html
# 参考:現在のブランチID
$ git log --oneline -n 1
8ae96dc (HEAD -> feature) featureを追記
# masterブランチ ------------------
# ブランチの切り替え
$ git checkout master
# ブランチの状況確認
$ git branch
feature
* master
# 存在するファイル
# --- コマンド結果が変わるだけでなく、GUIで見えるファイルも変わっている
$ ls
master.html
# 参考:現在のブランチID
# --- ブランチIDが異なる点に注意
$ git log --oneline -n 1
6258bda (HEAD -> master)
<GitKrakenの場合>
# **** 構文 ****
# ブランチの作成
$ git merge <統合する側のブランチ>
develop
ブランチは統合されてしまい、もともと存在しなかったような仕上がりになるmaster
ブランチに統合のための最新ブランチが作成されることはない# **** 参考 ****
# リモートでコンフリクトが発生しても強制的にプッシュ
# --- 完全に履歴が壊れるので絶対NG
$ git push -f
<GitKrakenの場合>
# **** 構文 ****
# ブランチの作成
$ git rebase <統合する側のブランチ>
merge
やrebase
と異なり、ブランチを統合することはない# **** ハンズオン ****
# リモートの取り込み
$ git fetch
# ブランチの確認
# --- リモートから取得したものも表示
$ git branch -a
* master
remotes/origin/feature
remotes/origin/master
# **** ハンズオン ****
# ブランチの確認
# --- リモートから取得したものも表示
$ git branch -a
* master
remotes/origin/feature
remotes/origin/master
# リモートのマージ
# --- 「remote/」はつけない
$ git merge origin/master
# **** 構文 ****
# ブランチの結合
# --- ローカルのブランチ
$ git merge <ブランチ名>
# ブランチの結合
# --- リモートから取得したブランチ
$ git merge <リモート名/ブランチ名>
# **** ハンズオン ****
# ※以下のコマンドまでの流れは「ブランチを用いた開発」の「ローカルで完結した開発」を参照
# ブランチの統合
# --- 統合先の「master」のコミットが分岐時から更新されていない
$ git merge topic_1
Updating f6a6d41..1593ac4
Fast-forward ※「Fast Foward」マージになっている
# **** ハンズオン ****
# ※以下のコマンドまでの流れは「ブランチを用いた開発」の「ローカルで完結した開発」を参照
# ブランチの統合
# --- 統合先の「master」のコミットが分岐時から更新されている
$ git merge topic_2
Merge made by the 'recursive' strategy. ※「Auto」マージになっている
<GitKrakenの場合>
# **** 構文 ****
# 現在コミットにタグ付け ------------------------
# 注釈付きタグの作成
# オプション:-a (annotateの略)
# オプション:-m (messageの略)
# --- 「-m」をつけない場合はエディタで入力することになる
$ git tag -a <タグ名> -m <メッセージ>
# 軽量版タグの作成
# オプション:なし
$ git tag <タグ名>
# 過去コミットにタグ付け ------------------------
# 注釈付きタグの作成
$ git tag -a <タグ名> -m <メッセージ> <コミット名>
# 軽量版タグの作成
$ git tag <タグ名> <コミット名>
# **** ハンズオン ****
# 現在のブランチの確認
$ git branch
feature
* master
# 現在のコミットにタグをつける
$ git tag -a "20170528" -m "version 20170528"
<GitKrakenの場合>
# **** 構文 ****
# ブランチの作成
$ git tag
# オプション:-l
# --- 文字列でパターンを指定してタグを表示
$ git tag -l "201705"
# **** ハンズオン ****
# タグ一覧の表示
$ git tag
20170528
<GitKrakenの場合>
# **** 構文 ****
# タグの詳細を表示
$ git show <タグ名>
# **** ハンズオン ****
# タグの詳細表示
$ git show 20170528
tag 20170528
Tagger: user_name <user_name@git.com>
Date: Sun Jan 19 18:17:30 2020 +0900
version 20170528+
commit 72da0c63c2b348a1b597dd80c1c2b838dcfde8b1 (HEAD -> master, tag: 20170528, origin/master)
Author: user_name <user_name@git.com>
Date: Sun Jan 19 08:20:02 2020 +0900
<GitKrakenの場合>
# **** 構文 ****
# 注釈付きタグの作成
$ git tag -d <タグ名>
# **** ハンズオン ****
# 準備:ブランチの確認
# --- 削除するタグを持つブランチに移動しておく
$ git branch
feature
* master
# タグの削除
$ git tag -d show
Deleted tag '20170528' (was 06216cc)
# **** ハンズオン ****
# タグ一覧の確認
$ git tag
v2.0.0
# 新しいタグの追加
$ git tag v2.0.1 v2.0.0
# タグ一覧の確認
# --- 追加が反映されている
$ git tag
v2.0.0
v2.0.1 ※追加された
# 古いタグの削除
$ git tag -d v2.0.0
Deleted tag 'v2.0.0' (was 4d59bb8)
# **** 構文 ****
# タグ一覧の表示
$ git push <リモート名> <タグ名>
# オプション:--tags
# --- タグを一斉に送信する
$ git push origin --tags
# **** ハンズオン ****
# 準備:現在のタグ一覧を確認
$ git tag
20170528
# タグをリモートリポジトリにプッシュ
$ git push origin 20170528
<エラー事例>
# **** 構文 ****
# 作業の一時避難(Git管理下にあるファイルのみ)
# --- saveは省略可能
$ git stash
$ git stash save
# 作業の一時避難(Git管理下にないファイルも含める)
$ git stash -u
# **** ハンズオン:Git管理下のファイル ****
# ワークツリーの変更
# --- ファイル追加
$ touch git_stash.txt
# ステージ追加
$ git add git_stash.txt
# ステータス確認
# --- ステージに追加されている(Git管理下に置かれた)
$ git status -s
A git_stash.txt
# 作業の一時避難
$ git stash
Saved working directory and index state WIP on master: 06216cc Merge branch 'master' of
https://github.com/user_name/git_tutorial
# ステータス確認
# --- 表示なし(stashされた)
$ git status -s
# **** ハンズオン:Git管理外のファイル ****
# ワークツリーの変更
# --- ファイル追加
$ touch git_stash_2.txt
# ステータス確認
# --- Git管理下に置かれていない
$ git status -s
?? git_stash_2.txt
# 作業の一時避難
$ git stash -u
Saved working directory and index state WIP on master: 06216cc Merge branch 'master' of
https://github.com/user_name/git_tutorial
# ステータス確認
# --- 表示なし(stashされた)
$ git status -s
# **** 構文 ****
# 一時避難の一覧表示
$ git stash list
# **** ハンズオン ****
# 一時避難の一覧表示
$ git stash list
stash@{0}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
stash@{1}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
stash@{2}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
# **** 構文 ****
# 最新の作業を復元する
# --- ステージの状況は復元されない
$ git stash apply
# オプション:--index
# ステージの状況も併せて復元する
$ git stash apply --index
# 最新の作業を復元する
$ git stash apply <スタッシュ名>
# **** ハンズオン ****
# ステータスの確認
# --- スタッシュ後で何もない状態
$ git status
On branch master
nothing to commit, working tree clean
# 避難した作業の復元
$ git stash apply
Already up to date!
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
git_stash_2.txt
# ステータスの確認
# --- スタッシュした状態が復元している
$ git status -s
?? git_stash_2.txt
# **** 構文 ****
# 最新の作業を削除する
$ git stash drop
# 特定の作業を削除する
$ git stash drop <スタッシュ名>
$ git stash drop stash@{1}
# 全作業を削除する
$ git stash clear
# **** ハンズオン ****
# 一時避難の一覧表示
$ git stash list
stash@{0}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
stash@{1}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
stash@{2}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
# 最新のスタッシュを削除する
$ git stash drop
Dropped refs/stash@{0} (469b9368e9f364a83ac574567339f94545b9bdf6)
# 一時避難の一覧表示
# --- 削除されたことを確認
$ git stash list
stash@{0}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
stash@{1}: WIP on master: 06216cc Merge branch 'master' of https://github.com/user_name/git_tutorial
# 全てのスタッシュを削除する
$ git stash clear
# 一時避難の一覧表示
# --- 全て削除されたことを確認
$ git stash list
# **** ハンズオン ****
# Git管理の開始
$ git init
# リモートリポジトリの登録
$ git remote add origin https://github.com/user_name/git_tutorial.git
# ブランチの確認
# --- 何も表示されない
# --- 「master」ブランチで作業しているが、初回コミット前は明示的な表示はない
$ git branch
# **** ハンズオン ****
# Git環境に変化を与える
# --- ファイル作成
$ touch master.txt
# ステージに追加
$ git add master.txt
# 初回コミット
# --- 「master」ブランチにコミットしている
# --- トピックブランチはマスターブランチから派生するので、「master」ブランチでの初回コミットが必須
$ git commit -m "Initial commit"
[master (root-commit) f6a6d41]
# ブランチの確認
$ git branch
* master
# ログの確認
$ git log --oneline -n 1
f6a6d41 (HEAD -> master, topic_2) Initial commit
# **** ハンズオン:開発 ****
# ブランチを追加 ---------------------------------------
# ブランチの追加
$ git branch topic_1
$ git branch topic_2
# ブランチの確認
$ git branch
* master ※マスターブランチ
topic_1 ※開発ブランチ1
topic_2 ※開発ブランチ2
# 「topic_1」ブランチで開発 -------------------------------------------
# ブランチの切替
$ git checkout topic_1
Switched to branch 'topic_1'
# ファイル作成
# --- 開発
$ touch topic_1.txt
# ステージに追加
$ git add topic_1.txt
# コミット
$ git commit -m "Add topic_1.txt"
# ログ確認
$ git log --oneline -n 1
1593ac4 (HEAD -> topic_1) Add topic_1.txt
# ファイル確認
$ ls
master.txt topic_1.txt
# 「topic_2」ブランチで開発 -------------------------------------------
# ブランチの切替
$ git checkout topic_2
Switched to branch 'topic_2'
# ファイル作成
# --- 開発
$ touch topic_2.txt
# ステージに追加
$ git add topic_2.txt
# コミット
$ git commit -m "Add topic_2.txt"
# ログ確認
$ git log --oneline -n 1
37641b1 (HEAD -> topic_2) Add topic_2.txt
# ファイル確認
$ ls
master.txt topic_2.txt
# **** ハンズオン ****
# ログの確認
# --- コミットIDは動いていない(トピックごとに分岐して開発している)
$ git log --oneline -n 1
f6a6d41 (HEAD -> master) Initial commit
1593ac4 (HEAD -> topic_1) Add topic_1.txt
37641b1 (HEAD -> topic_2) Add topic_2.txt
# **** ハンズオン ****
# ブランチの確認
# --- 統合先となる「master」ブランチにいる
$ git branch
* master
topic_1
topic_2
# ファイル確認
# --- 他の開発のファイルは紛れ込んでいない
$ ls
master.txt
# ブランチの統合
$ git merge topic_1
Updating f6a6d41..1593ac4
Fast-forward ※「Fast Foward」マージになっている
# ログの確認
# --- コミットIDがtopic_1のコミットIDと等しい(Fast-Fowardマージ)
$ git log --oneline -n 2
1593ac4 (HEAD -> master, topic_1) Add topic_1.txt
f6a6d41 Initial commit
# ファイル確認
# --- topic_1で作成したファイルが追加されている
$ ls
master.txt topic_1.txt
# **** ハンズオン ****
# ブランチの確認
# --- 統合先となる「master」ブランチにいる
$ git branch
* master
topic_1
topic_2
# ファイル確認
# --- 他の開発のファイルは紛れ込んでいない
$ ls
master.txt topic_1.txt
# ブランチの統合
$ git merge topic_2
Merge made by the 'recursive' strategy. ※「Auto」マージになっている
# ログの確認
# --- コミットIDがtopic_1のコミットIDと等しい(Fast-Fowardマージ)
$ git log --oneline
d23d0d1 (HEAD -> master) Merge branch 'topic_2'
37641b1 (topic_2) Add topic_2.txt
1593ac4 (topic_1) Add topic_1.txt
f6a6d41 Initial commit
# ファイル確認
# --- topic_2で作成したファイルが追加されている
$ ls
master.txt topic_1.txt topic_2.txt
# **** ハンズオン ****
# ブランチの確認
# --- 統合先となる「master」ブランチにいる
$ git branch
* master
topic_1
topic_2
# ファイル確認
$ ls
master.txt topic_1.txt topic_2.txt
# ログの確認
$ git log --oneline
d23d0d1 (HEAD -> master) Merge branch 'topic_2'
37641b1 (topic_2) Add topic_2.txt
1593ac4 (topic_1) Add topic_1.txt
f6a6d41 Initial commit
# **** ハンズオン ****
# ブランチの確認
# --- マスターブランチにいることを確認
$ git branch
* master
topic_1
topic_2
# 最新のマスターから新たなブランチを作成
$ git branch topic_3
# ブランチの切り替え
$ git checkout topic_3
master
topic_1
topic_2
* topic_3
# コミットID
# --- topic_3でまだコミットしていないので、masterブランチのコミットIDを指している
$ git log --oneline
d23d0d1 (HEAD -> topic_3, master) Merge branch 'topic_2'
37641b1 (topic_2) Add topic_2.txt
1593ac4 (topic_1) Add topic_1.txt
f6a6d41 Initial commit
master
のコミットIDが分岐時から更新されているかどうかで「Fast Foward」「Auto」が決まる# **** ハンズオン ****
# ブランチの確認
$ git branch
master
topic_1
topic_2
* topic_3
# ファイル作成
# --- 開発
$ touch topic_3.txt
# ステージに追加
$ git add topic_3.txt
# コミット
# --- 新しいコミットIDが割当て
$ git commit -m "Add topic_3.txt"
[topic_3 e3384e7] Add topic_3.txt
# ログ確認
$ git log --oneline
e3384e7 (HEAD -> topic_3) Add topic_3.txt
d23d0d1 (master) Merge branch 'topic_2'
37641b1 (topic_2) Add topic_2.txt
1593ac4 (topic_1) Add topic_1.txt
f6a6d41 Initial commit
# ファイル確認
$ ls
master.txt topic_1.txt topic_2.txt topic_3.txt