0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git】基礎コマンド22個を動かして一気に覚える【随時更新中】

Last updated at Posted at 2025-08-16

はじめに

順番にこなしていくだけで、主要なコマンドの役割を一通り学べる教材が欲しいという声があったので作成してみました。(逆に手順を飛ばしたりすることは想定していません。)
イメージ図なども貼っていますがあくまでイメージで内部の正確な動きとは異なる部分もあります。
※ 随時、分かりやすくするためにサンプル図など追加していきます。

前提

GitHubなど共有のGitリポジトリは用意しておいて下さい。
ローカルでGitが使えるようにしておいてください。

私の操作環境

Gitバージョン:2.44.0.windows.1
OS:Windows 11 Pro
リモートリポジトリ:GitHub
GitHub上にはSampleGitProjectという名前の空のリポジトリを用意しています。

主要コマンド22選

1. initコマンド

新規ローカルリポジトリを作成する

プロジェクトを格納するディレクトリを作成して、ディレクトリ内へ移動

$ mkdir SampleGitProject
$ cd SampleGitProject/

git init実行し、プロジェクトディレクトリ内にローカルレポジトリを作成

$ git init
Initialized empty Git repository in C:/Users/name/SampleGitProject/.git/

イメージ図
image.png

2. addコマンド

新規ファイルをステージングエリアに追加する

サンプルファイルを5つ新規作成

$ touch sample1 sample2 sample3 sample4 sample5

作成したファイルをステージングエリアに追加
「.」を指定することでカレントディレクトリ配下すべてのファイルを対象にできる

$ git add .

git statusでステージングエリアに追加されていることを確認。Changes to be committedに含まれているファイルがステージングされたファイル

$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   sample1
        new file:   sample2
        new file:   sample3
        new file:   sample4
        new file:   sample5

イメージ図
image.png

3. commitコマンド

ステージングエリアのファイルをコミットする

コミットを実行することで、ステージングエリアに追加されているファイルをコミットオブジェクトとして登録される
-mオプションを付けて、メッセージを引数に渡すことでコミットメッセージを登録できる

$ git commit -m "first commit"
[main (root-commit) e199740] first commit
 5 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 sample1
 create mode 100644 sample2
 create mode 100644 sample3
 create mode 100644 sample4
 create mode 100644 sample5

イメージ図
image.png

4. remotoコマンド

リモートリポジトリ先を登録する

originタグにリモートリポジトリのURLを設定する

git remote add origin https://github.com/<USER>/<REPO>.git

正常に設定できたか確認する

$ git remote -v
origin  https://github.com/xxxxxxx/SampleGitProject.git (fetch)
origin  https://github.com/xxxxxxx/SampleGitProject.git (push)

これでoriginタグを指定することは、リモートリポジトリのURLを指定することと同義になりました

5. pushコマンド

リモートリポジトリにローカルコミットをプッシュする

プッシュコマンドを実行しローカルコミットをプッシュする
-uを付けることでupstreamが作成される。upstreamが作成されることで次回以降origin main部分を省略できる

$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 231 bytes | 231.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/xxxxxxx/SampleGitProject.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

イメージ図
image.png

6. cloneコマンド

リモートリポジトリをローカルにクローンする

カレントディレクトリがSampleGitProjectの中であれば、その外に移動する

$ cd ..

リモートリポジトリのSampleGitProjectをクローンする
既にローカルのSampleGitProjectと区別するため、SampleGitProject2という名前のディレクトリにクローンする

$ git clone https://github.com/<USER>/<REPO>.git SampleGitProject2
Cloning into 'SampleGitProject2'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.

クローン結果確認

$ cd SampleGitProject2
$ ls -a
./  ../  .git/  sample1  sample2  sample3  sample4  sample5

.git/ディレクトリと作成したファイル群が展開されていることが確認できる

7. branchコマンド

ブランチを作成する

「6.」に続きSampleGitProject2で操作する

developブランチを作成

$ git branch develop

ブランチ一覧を確認

$ git branch
  develop
* main

developが作成されてることを確認。またカレントブランチはmainになっている。

カレントブランチをmainからdevelopに変更する

$ git switch develop
Switched to branch 'develop'

developブランチに修正を加えて、リモートにプッシュする(次のセクションの準備として)

$ touch sample6
$ git add .
$ git commit -m "add sample6"
$ git push -u origin develop

8. fetchコマンド

リモートの更新を取得する

SampleGitProject2からSampleGitProjectにカレントディレクトリを移動

$ cd ../SampleGitProject

ブランチ一覧を表示
--allを付けるとリモート追跡ブランチも表示される

$ git branch --all
* main
  remotes/origin/main

リモートにプッシュされたdevelopを取り込んでいないためmainしかないことを確認する。

fetchでリモートの更新を取り込む

$ git fetch
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0 (from 0)
Unpacking objects: 100% (2/2), 213 bytes | 19.00 KiB/s, done.
From https://github.com/xxxxxxx/SampleGitProject
 * [new branch]      develop    -> origin/develop

ブランチ一覧を表示

$ git branch --all
* main
  remotes/origin/develop
  remotes/origin/main

developのリモート追跡ブランチが追加されていることが確認できる。

9. switchコマンド

作業するブランチを切り替える

mainからdevelopブランチに切り替える

$ git switch develop
Switched to a new branch 'develop'
branch 'develop' set up to track 'origin/develop'.

ローカルブランチとしてdevelopが作成され、upstreamとして'origin/develop'が指定されました
今回は少し特殊な例で、リモート追跡ブランチに存在して、ローカルブランチに存在しないブランチを指定した場合は、ローカルブランチを作成して、リモート追跡ブランチに紐づける動きをします。

またローカルブランチにも、リモート追跡ブランチにも存在しないブランチを作成する場合
git switch -c [ブランチ名]のように-cを付けることでローカルブランチ作成と作成されたブランチへの切り替えを同時に行ってくれます。

10. rmコマンド

ファイルを削除する

sample3ファイルを削除

$ git rm sample3
rm 'sample3'

git rmコマンドを実行すると対象ファイルの削除と削除という変更をステージングに追加まで実施される

削除をコミット

$ git commit -m "delete sample3"
[develop 810b439] delete sample3
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 sample3

11. mvコマンド

ファイル名を変更する

sample6sample3にリネームする

$ git mv sample6 sample3

リネームされていることを確認する

$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    sample6 -> sample3

ファイル名が変更され、ステージングに追加もされているためこのままコミットする

$ git commit -m "rename sample6 to sample3"
[develop a62c4e8] rename sample6 to sample3
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename sample6 => sample3 (100%)

コミットの内容をプッシュ

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 429 bytes | 429.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/xxxxxxx/SampleGitProject.git
   39d2aba..a62c4e8  develop -> develop

12. pullコマンド

リモートの更新を取得してマージ

SampleGitProject2で作業するためカレントディレクトリをSampleGitProjectからSampleGitProject2に移動

$ cd ../SampleGitProject2

プル実行前に各ブランチのリファレンスを確認

$ git show-ref --abbrev
39d2aba refs/heads/develop
e199740 refs/heads/main
e199740 refs/remotes/origin/HEAD
39d2aba refs/remotes/origin/develop
e199740 refs/remotes/origin/main

heads/developremotes/origin/developのリファレンスが39d2abaであることを確認できる

プルでリモートの更新を取得してマージする

$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0 (from 0)
Unpacking objects: 100% (4/4), 409 bytes | 24.00 KiB/s, done.
From https://github.com/xxxxxxx/SampleGitProject
   39d2aba..a62c4e8  develop    -> origin/develop
Updating 39d2aba..a62c4e8
Fast-forward
 sample6 | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 sample6

各ブランチのリファレンスを確認

$ git show-ref --abbrev
a62c4e8 refs/heads/develop
e199740 refs/heads/main
e199740 refs/remotes/origin/HEAD
a62c4e8 refs/remotes/origin/develop
e199740 refs/remotes/origin/main

元々heads/developremotes/origin/developのリファレンスが39d2abaだったが
a62c4e8となっていることが確認できる
つまり、pullでリモート追跡ブランチの更新(fetch)とローカルブランチの更新(merge)が両方実行されていることが分かった
次項でa62c4e8が最新コミットであることを確認する

13. logコマンド

コミット履歴を確認

コミットログを確認

$ git log --oneline
a62c4e8 (HEAD -> develop, origin/develop) rename sample6 to sample3
810b439 delete sample3
39d2aba add sample6
e199740 (origin/main, origin/HEAD, main) first commit

前項でプルした時に反映されたa62c4e8が最新コミットであることを確認できた

14. mergeコマンド

他のブランチの変更を現在のブランチに統合

mainブランチに切り替え

$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

マージ前のコミットフローを確認

$ git log --oneline --graph --all
* a62c4e8 (HEAD -> develop, origin/develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 (origin/main, origin/HEAD, main) first commit

developmainブランチにマージ

$ git merge develop
Updating e199740..a62c4e8
Fast-forward

マージ後のコミットフローを確認

$ git log --oneline --graph --all
* a62c4e8 (HEAD -> main, origin/develop, develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 (origin/main, origin/HEAD) first commit

maindevelopのコミットの位置が一緒になっていることが分かる
Fast-forwardマージのためマージコミットは作成されていません

マージされた情報をプッシュする

$ git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/hiroto-uchida/SampleGitProject.git
   e199740..a62c4e8  main -> main

プッシュ後のコミットフローを確認

$ git log --oneline --graph --all
* a62c4e8 (HEAD -> main, origin/main, origin/develop, origin/HEAD, develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 first commit

origin/maindevelopとコミットの位置が一緒になっていることが分かる

15. diffコマンド

コミットやステージングエリア、ワーキングディレクトリの間の差分を表示

SampleGitProjectで作業するためカレントディレクトリをSampleGitProject2からSampleGitProjectに移動

$ cd ../SampleGitProject

mainブランチに切り替えて、リモートの最新を取り込む

$ git switch main
$ git pull

以下の修正をいれる(ステージングには追加しない)
・sample1ファイルに適当なテキスト情報を入れる
・sample2ファイルを削除する

ワーキングディレクトリとステージングエリアの差分確認

$ git diff
diff --git a/sample1 b/sample1
index e69de29..4c324ee 100644
--- a/sample1
+++ b/sample1
@@ -0,0 +1 @@
+サンプルファイル1
\ No newline at end of file
diff --git a/sample2 b/sample2
deleted file mode 100644
index e69de29..0000000

sample1に修正が入り、sample2が削除されていることが分かる

16. showコマンド

特定の Git オブジェクト(主にコミット)の詳細表示

$ git show
commit a62c4e8106e1e51e567837bc9e530bf6a8bcb2cb (HEAD -> main, origin/main, origin/develop, develop)
Author: xxxxxxxx <xxxxx@xxxxx.com>
Date:   Sat Aug 16 02:35:31 2025 +0900

    rename sample6 to sample3

diff --git a/sample6 b/sample3
similarity index 100%
rename from sample6
rename to sample3

17. restoreコマンド

作業ディレクトリの変更を破棄

作業ディレクトリの状態を確認

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   sample1
        deleted:    sample2

sample2の削除を取り消す

$ git restore sample2

再度作業ディレクトリの状態を確認

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   sample1

sample2削除の変更が消えていることを確認できた

18. stashコマンド

作業中の変更を一時的に退避

作業ディレクトリの状態を確認

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   sample1

変更を退避

git stash push -m "stash modified sample1"
Saved working directory and index state On main: stash modified sample1

スタッシュ済みリスト

$ git stash list
stash@{0}: On main: stash modified sample1

最新のstashを適用し、stash一覧から削除

$ git stash pop
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   sample1

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (5fc1f1c84b7c4ac545641aa4fe92d890818b5597)

19. rebaseコマンド

ブランチの変更履歴を別のブランチの先頭に付け替える

前項でスタッシュの適用したsample1修正分をコミットする

$ git add .
$ git commit -m "edit sample1"
[main 64eeba0] edit sample1
 1 file changed, 1 insertion(+)

developブランチに切り替える

$ git switch develop
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.

sample2に適当な文言を入れてコミットする(文言修正は適当なエディタで実施してください)

$ git add .
$ git commit -m "edit sample2"
[develop c041fd8] edit sample2
 1 file changed, 1 insertion(+)

コミット履歴を確認

$ git log --oneline --graph --all
* c041fd8 (HEAD -> develop) edit sample2
| * 64eeba0 (main) edit sample1
|/
* a62c4e8 (origin/main, origin/develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 first commit

developの分岐開始地点なったmainのコミットから、それぞれ新しいコミットが追加されていることが確認できる

developの新規コミットの分岐開始地点をmainの最新コミットに移動する

$ git rebase main
Successfully rebased and updated refs/heads/develop.

コミット履歴を確認

$ git log --oneline --graph --all
* 51ce84b (HEAD -> develop) edit sample2
* 64eeba0 (main) edit sample1
* a62c4e8 (origin/main, origin/develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 first commit

developの分岐開始地点がmainの最新コミットに移動している
またdevelopのコミットは作成されなおしているため、SHA1が変わっている(c041fd851ce84b)

20. reflogコマンド

ブランチやHEADの移動履歴を表示

HEADの移動履歴を表示

$ git reflog
51ce84b (HEAD -> develop) HEAD@{0}: rebase (finish): returning to refs/heads/develop
51ce84b (HEAD -> develop) HEAD@{1}: rebase (pick): edit sample2
64eeba0 (main) HEAD@{2}: rebase (start): checkout main
c041fd8 HEAD@{3}: commit: edit sample2
a62c4e8 (origin/main, origin/develop) HEAD@{4}: checkout: moving from main to develop
64eeba0 (main) HEAD@{5}: commit: edit sample1
a62c4e8 (origin/main, origin/develop) HEAD@{6}: reset: moving to HEAD
a62c4e8 (origin/main, origin/develop) HEAD@{7}: pull: Fast-forward
e199740 HEAD@{8}: checkout: moving from develop to main
a62c4e8 (origin/main, origin/develop) HEAD@{9}: commit: rename sample6 to sample3
810b439 HEAD@{10}: commit: delete sample3
39d2aba HEAD@{11}: checkout: moving from main to develop
e199740 HEAD@{12}: checkout: moving from develop to main
39d2aba HEAD@{13}: checkout: moving from main to develop
e199740 HEAD@{14}: checkout: moving from main to main
e199740 HEAD@{15}: commit (initial): first commit

これまでの、HEADの履歴が確認できる
どのブランチにも属していないコミット(例えばc041fd8)なども表示されるため、誤ってブランチを削除した場合でも紐づいていたコミットを復元できる可能性がある

21. resetコマンド

コミットやステージングを取り消し

[19. rebaseコマンド]で実施したrebaseを取り消す
前項のreflogでrebase実行前のSHA1がc041fd8であることは確認済みのため、c041fd8を対象にresetコマンドを実行する
c041fd8はあくまで私の環境での話です。実行する際はご自身でreflogを使い対象のコミットを探して下さい

$ git reset --hard c041fd8
HEAD is now at c041fd8 edit sample2

コミット履歴を確認

$ git log --oneline --graph --all
* c041fd8 (HEAD -> develop) edit sample2
| * 64eeba0 (main) edit sample1
|/
* a62c4e8 (origin/main, origin/develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 first commit

rebase実行前に戻っていることを確認できる

22. cherry-pickコマンド

特定コミットを現在のブランチに適用

mainブランチへ切り替える

$ git switch main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

developの最新コミットであるsample2の修正をmainに取り込む
※私の環境ではc041fd8developブランチの最新ですが、ご自身の環境で実行する際はgit logを利用して確認して下さい

$ git cherry-pick c041fd8
[main 4b1c9ca] edit sample2
 Date: Sun Aug 17 00:29:13 2025 +0900
 1 file changed, 1 insertion(+)

ログを確認する

$ git log --oneline --graph --all
* 4b1c9ca (HEAD -> main) edit sample2
* 64eeba0 edit sample1
| * c041fd8 (develop) edit sample2
|/
* a62c4e8 (origin/main, origin/develop) rename sample6 to sample3
* 810b439 delete sample3
* 39d2aba add sample6
* e199740 first commit

main4b1c9ca (HEAD -> main) edit sample2が追加されていることが確認できる

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?