0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[memo] git を clone した後にコミット日付に直す

Posted at

はじめに

git コマンドで clone すると、タイムスタンプが clone した時点の日付になると思う。
それでは、make コマンドを使う時など、タイムスタンプがずれていると困ることがある。
そのような時のためのメモ。

git のタイムスタンプには author date と committer date の2つがあります。
commiter date は rebase など際にも書き換わることがあるようなので、基本は author date に変換します。

リポジトリ管理されているファイルの一覧を取得

git ls-files

指定ファイルの日付を取得

前述の通り author date を取得します。
あとで touch に渡したいので、それで扱える形式で出力します。

git log -1 --date=format-local:"%Y%m%d%H%M%S" --pretty=format:"%ad" "${file}"
  • -1 は最新 1 コミット分の出力
  • --date=format-local: は、ユーザーのローカルタイムゾーン、かつ指定フォーマットで出力する
  • --pretty=format: は、出力項目を指定(%ad で author date)

指定ファイルの日付を変更する

touch -t "${timestamp}" "${file}"
  • -t に渡すのはローカルタイムの値

まとめると

git-retouch
#!/bin/sh

for file in $(git ls-files); do
    timestamp=$(git log -1 --date=format-local:"%Y%m%d%H%M%S" --pretty=format:"%ad" "${file}")
    touch -t "${timestamp}" "${file}"
done
0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?