LoginSignup
6
1

More than 3 years have passed since last update.

【Git】git fetchしたデータはどこにある?fetchによるデータ取得と取得後のコミット履歴を確認する方法

Posted at

git fetchの処理が何をしているかについて。

git fetchを実行したら、何かしらがダウンロードされたのに、git logしても何も変更されていない、、取得したデータはどこにあるんだ!?となったので、データのありかや、確認方法について確認してみた。

目次

  1. git fetchとは?
  2. 引数・オプションなしの場合
  3. git fetchの実例
  4. git fetchで取得してきたデータを確認する方法
    1. コミット履歴の確認
    2. ファイルの差分を確認
  5. リモートレポジトリとブランチを指定する
  6. git fetch --allとは?

git fetchとは?

git fetchとは、他のレポジトリから情報を取得するコマンド。

通常はgithubのリモートレポジトリからコミットを取得してくる目的で使う。

git pullだと、リモートレポジトリからコミットの情報を取得して、差分があればマージまでしてしまうが、git fetchは情報の取得までで止まる。

引数・オプションなしの場合

単にgit fetchを実行した場合は、リモートレポジトリoriginの現在のブランチのデータを取得し更新する。

$ git fetch 

↓ 同じ

masterブランチの場合
$ git fetch origin

ローカル側で更新されるのはremotes/origin/masterブランチ(リモート追跡ブランチと呼ぶ)。

リモート追跡ブランチの一覧はgit branch -aで確認できる。



▼注意点
現在のブランチや、コマンドを実行したブランチの情報が更新されるわけではない。ローカルのリモート追跡ブランチのログが更新される。

git fetchの実例

git fetchをすると登録してある、originで追跡しているすべてのレポジトリの内容を確認して、差分を取得してくる。

submoduleがある場合は、submoduleの情報も更新する。

$ git fetch

remote: Enumerating objects: 107, done.
remote: Counting objects: 100% (107/107), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 108 (delta 70), reused 84 (delta 53), pack-reused 1
Receiving objects: 100% (108/108), 30.28 KiB | 6.05 MiB/s, done.
Resolving deltas: 100% (70/70), completed with 19 local objects.
From github.com:test/app
   27c38b55..9c3de210  master     -> origin/master
   ea99240d..1108db61  aaa     -> origin/aaa
   d1f87062..27c38b55  bbb     -> origin/bbb
   4cad4e31..3c14bc6a  ccc      -> origin/ccc
Fetching submodule app-sub
From github.com:test/app
   a9e55dd..7975898  master     -> origin/master
 + fd77a0d...5213eea xxx      -> origin/xxx  (forced update)

git fetchで取得してきたデータを確認する方法

コミット履歴の確認

git fetchで更新されるのはリモート追跡ブランチ(origin/ブランチ)。このブランチのログを表示すればいい。

$ git log origin/ブランチ名 

--oneline--graphをつけると見やすくなる。

▼実例

$ git log origin/master --oneline --graph

*   9c3de210 (origin/master) Merge branch 'master' of github.test/app
|\  
| *   d44f7bb3 Merge pull request #1144 from aaa
| |\  
| | * d31b7483 (origin/aaa) [F]CSV download category 
| |/  
| *   9d164354 (origin/xxx) Merge pull request #114
| |\  


ファイルの差分を確認

現在のブランチとの差分を確認する場合は、

$ git diff origin/branch名


リモートレポジトリとブランチを指定する

取得したいブランチを指定することもできる。

git fetch <リモートレポジトリ名> <ブランチ名>

※レポジトリとブランチ名の間にスラッシュは入らない

$ git fetch origin master
From github.com:test/app
 * branch              master     -> FETCH_HEAD



▼スラッシュがあるとエラーになる

エラー
$ git fetch origin/master
fatal: 'origin/master' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

origin/masterというレポジトリを探すため、そんなのはないとエラーが出る。

git fetch --allとは?

--allオプションを使うとすべてのリモートレポジトリのすべてのブランチからデータを取得してくる。

リモートレポジトリがoriginしか登録されていない場合は、git fetch(git fetch origin)と同じになる。

//originしかない
$ git remote
origin


//originのデータを取得
$ git fetch --all
Fetching origin
6
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
6
1