git 基本的なコマンド3 fetch, pull

git fetch git mergeリモートから取得

git fetch <リモート名>と入力すると、リモートリポジトリからローカルリポジトリに情報を落とすことができます。ここで注意したいのが、自分のワークツリー(手元の作業場)には情報は反映されません。

じゃあ自分の手元の作業場に持ってくるには?

git mergeをしてあげます。


git hub上でhome.htmlという新しいファイルを作成し、文字を入力します。その後mainブランチにmerge。

Image from Gyazo

ターミナル上でgit fetch

% git fetch origin
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 678 bytes | 169.00 KiB/s, done.
From https://github.com/takuya178/git_tutorial
   69fcc61..299fed5  main       -> origin/main

では、git fetchで取得した情報が保存されているか確認します。

% git branch -a
* main
  remotes/origin/main

現在いるmainブランチに米印がついていて、remotes/origin/mainに先程の変更内容が保存されています。


% git checkout remotes/origin/main
% ls
home.html   index.html

home.htmlがあることが確認できました。

メインブランチに切り替えてから、home.htmlをmergeします。

% git checkout main
% git merge origin/main
% ls
home.html   index.html



git pull

git pull origin main

fetchとmergeを一度にしてくれる。