LoginSignup
1
1

More than 3 years have passed since last update.

gitで最新コミットの差分ファイルだけをディレクトリ構造ごと抜き出したい

Posted at

「あー、gitで変更のあったファイルだけディレクトリ構造はそのままに抜き出したいー」ということが結構ありますよね? 僕は今まであまりなかったのですが、まぁ、そのような業務が発生することがありましたので、調べました。

最終コミットで変更があったファイルをシンプルに表示する
git log --pretty="" --name-status -1

これとシェルスクリプトを組み合わせて、変更のあったファイルだけを抽出する。

#!/bin/bash -u

SCRIPT_DIR=$(cd $(dirname $0); pwd)
LOCAL_GIT_REPOSITORY=/path/to
WORK_DIR=$SCRIPT_DIR/$(date +"%Y%m%d%I%M%S")_$(basename $LOCAL_GIT_REPOSITORY)

cd $LOCAL_GIT_REPOSITORY

# git リポジトリじゃない場合はexit
if [ ! -e .git ]; then
    echo 'ERROR: Here is not git repository.'
    exit 1
fi

for file in $(git log --pretty="" --name-status -1 | grep -v "^D" | cut -f 2-)
do
    result_dir=$WORK_DIR/$(dirname $file)
    mkdir -p $result_dir
    cp $file $result_dir
done
1
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
1
1