🔄

【Git】git restoreを用いて、ブランチ上の変更ファイルを他ブランチに持ってくる

2021/04/18に公開

ポイント・結論

branch_aにある変更ファイルを他ブランチにまとめて持っていきたい場合、他ブランチで以下コマンドを打つ。

git restore --source=branch_a .

部分的に持ってきたい場合、以下コマンドで変更ファイルの一覧を確認すると便利。

$ git diff main..branch_a --name-only

なぜgit restoreを使うのか?

なんか使えるとカッコよくない?
もちろんgit checkoutでも同じ機能は実現できます。
ただ、git checkoutは責務が広く、ブランチの切り替えなどでも使用します。
ブランチの変更はgit switch, ファイルの変更・復旧にはgit restoreと使い分けた方が、責務が分かりやすいです。

例をあげながらの説明

ディレクトリ構成

└── GitWorkTreeFromOtherBranch
    ├── 1.txt
    ├── 2.txt
    ├── 3.txt
    ├── 4.txt
    └── 5.txt

準備

mainから切り替えてbranch_aを作ります。

$ git branch                   
* main

$ git switch -c branch_a
Switched to a new branch 'branch_a'

変更ファイルを作りcommitします。

$ for i in 1 2 3 4 5
for> do
for> echo $i >> GitWorkTreeFromOtherBranch/$i.txt
for> done

$ ls GitWorkTreeFromOtherBranch 
1.txt                   3.txt                   5.txt
2.txt                   4.txt

$ git add .
$ git commit -m 'create 1-5 text'
[branch_a fdafbec] create 1-5 text
 5 files changed, 5 insertions(+)
 create mode 100644 GitWorkTreeFromOtherBranch/1.txt
 create mode 100644 GitWorkTreeFromOtherBranch/2.txt
 create mode 100644 GitWorkTreeFromOtherBranch/3.txt
 create mode 100644 GitWorkTreeFromOtherBranch/4.txt
 create mode 100644 GitWorkTreeFromOtherBranch/5.txt

mainからbranch_bを作ります。

$ git switch main
Switched to branch 'main'

$ git switch -c branch_b
Switched to a new branch 'branch_b'

$ git branch
  branch_a
* branch_b
  main

$ ls GitWorkTreeFromOtherBranch
$ git status                                                               
On branch branch_b
nothing to commit, working tree clean

全部持ってくる

git restore対象パスを、カレントパス(.)に指定すれば、全部持って来ることができます。

$ git restore --source=branch_a .
$ git status
On branch branch_b
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        GitWorkTreeFromOtherBranch/1.txt
        GitWorkTreeFromOtherBranch/2.txt
        GitWorkTreeFromOtherBranch/3.txt
        GitWorkTreeFromOtherBranch/4.txt
        GitWorkTreeFromOtherBranch/5.txt

nothing added to commit but untracked files present (use "git add" to track)

$ ls GitWorkTreeFromOtherBranch 
1.txt                   3.txt                   5.txt
2.txt                   4.txt

部分的に持ってくる

git diff--name-onlyオプションを付けると、ファイル名(Gitのルートディレクトリからの相対パス)の一覧を確認できます。

$ git diff main..branch_a --name-only
GitWorkTreeFromOtherBranch/1.txt
GitWorkTreeFromOtherBranch/2.txt
GitWorkTreeFromOtherBranch/3.txt
GitWorkTreeFromOtherBranch/4.txt
GitWorkTreeFromOtherBranch/5.txt

持ってきたいファイルだけをgit restoreの対象パスに指定すればOKです。

$ git restore --source=branch_a GitWorkTreeFromOtherBranch/1.txt GitWorkTreeFromOtherBranch/4.txt
$ git status
On branch branch_b
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        GitWorkTreeFromOtherBranch/1.txt
        GitWorkTreeFromOtherBranch/4.txt

nothing added to commit but untracked files present (use "git add" to track)

$ ls GitWorkTreeFromOtherBranch 
1.txt   4.txt

参考

Git - git-restore Documentation

Discussion