0
1

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.

Gitのコミットの日付を書き換える

Last updated at Posted at 2023-01-30

やりたいこと

  • コミット毎に記録されている日付を手動で修正したい

前提知識

  • Gitのコミットにはコミットの著者である Author とコミットを取り込んだ人を表す Committer の2つの属性が記録されている
  • そしてそれぞれに Date(AuthorDate) と CommiterDate という2種類の日付も記録されている
  • デフォルトの git log では CommitterCommitterDate は表示されない
$ git log
commit 264d947583584b21164580d07defc5a460073cb8
Author: Kazuki Tobita <kazukiti201@gmail.com>
Date:   Mon Jan 30 15:38:14 2023 +0900

    Add c.txt

commit 03003ecd20b6cc1ec960a59a6f751b248b4fda0c
Author: Kazuki Tobita <kazukiti201@gmail.com>
Date:   Mon Jan 30 15:37:55 2023 +0900

    Add b.txt

commit 57ea3e9bb2a102d012b407f480b53c9902846660
Author: Kazuki Tobita <kazukiti201@gmail.com>
Date:   Mon Jan 30 15:36:40 2023 +0900

    Add a.txt
  • --pretty=fuller オプションを付与すると CommitterCommitterDate も表示される
$ git log --pretty=fuller
commit 264d947583584b21164580d07defc5a460073cb8
Author:     Kazuki Tobita <kazukiti201@gmail.com>
AuthorDate: Mon Jan 30 15:38:14 2023 +0900
Commit:     Kazuki Tobita <kazukiti201@gmail.com>
CommitDate: Mon Jan 30 15:38:14 2023 +0900

    Add c.txt

commit 03003ecd20b6cc1ec960a59a6f751b248b4fda0c
Author:     Kazuki Tobita <kazukiti201@gmail.com>
AuthorDate: Mon Jan 30 15:37:55 2023 +0900
Commit:     Kazuki Tobita <kazukiti201@gmail.com>
CommitDate: Mon Jan 30 15:37:55 2023 +0900

    Add b.txt

commit 57ea3e9bb2a102d012b407f480b53c9902846660
Author:     Kazuki Tobita <kazukiti201@gmail.com>
AuthorDate: Mon Jan 30 15:36:40 2023 +0900
Commit:     Kazuki Tobita <kazukiti201@gmail.com>
CommitDate: Mon Jan 30 15:36:48 2023 +0900

    Add a.txt

書き換えてみる

  • 直前2つのコミットを対象に、 Date(AuthorDate) を書き換えてみる
$ git rebase -i HEAD~2
Stopped at 03003ec...  Add b.txt
You can amend the commit now, with

  git commit --amend '-S'

Once you are satisfied with your changes, run

  git rebase --continue
  • Vim(環境によっては別のエディタ)が起動する
pick 03003ec Add b.txt
pick 264d947 Add c.txt
  • pickedit に変更して保存する
edit 03003ec Add b.txt
edit 264d947 Add c.txt
  • date コマンドの -R オプションは、RFC 2822形式で日付を出力するオプション
  • 他の形式でもGitが上手くパースしてくれる可能性もある(が、日本語の日付だと上手くパースしてくれなかったりする)
  • 基本的に date -Rgit commit --ammend --date="xxxx"git rebase --continue を繰り返して書き換えていくイメージ
$ date -R
Mon, 30 Jan 2023 16:54:23 +0900

$ git commit --amend --date="Mon, 30 Jan 2023 16:54:23 +0900"
[detached HEAD e8a0462] Add b.txt
 Date: Mon Jan 30 16:54:23 2023 +0900
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt

$ git rebase --continue
Stopped at 264d947...  Add c.txt
You can amend the commit now, with

  git commit --amend '-S'

Once you are satisfied with your changes, run

  git rebase --continue

$ date -R
Mon, 30 Jan 2023 16:57:21 +0900

$ git commit --amend --date="Mon, 30 Jan 2023 16:57:21 +0900"
[detached HEAD cd69867] Add c.txt
 Date: Mon Jan 30 16:57:21 2023 +0900
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.txt
  • Successfully rebased and updated refs/heads/master. が出力されれば終了
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
  • ちなみに以下のようにワンライナーにすることもできる
$ git commit --amend --date="$(date -R)" && git rebase --continue
  • CommitterDate をまとめて書き換える
$ git rebase HEAD~2 --committer-date-is-author-date
Current branch master is up to date, rebase forced.
Successfully rebased and updated refs/heads/master.
  • コミットの日付が書き換えられたことが確認できる
$ git log --pretty=fuller
commit e80caa5e879eb47c0c6923e403b0729764214e6d (HEAD -> master)
Author:     Kazuki Tobita <kazukiti201@gmail.com>
AuthorDate: Mon Jan 30 16:57:21 2023 +0900
Commit:     Kazuki Tobita <kazukiti201@gmail.com>
CommitDate: Mon Jan 30 16:57:21 2023 +0900

    Add c.txt

commit 8f3dfafd7192b139cbed755d84579aecd80d2343
Author:     Kazuki Tobita <kazukiti201@gmail.com>
AuthorDate: Mon Jan 30 16:54:23 2023 +0900
Commit:     Kazuki Tobita <kazukiti201@gmail.com>
CommitDate: Mon Jan 30 16:54:23 2023 +0900

    Add b.txt

commit 57ea3e9bb2a102d012b407f480b53c9902846660
Author:     Kazuki Tobita <kazukiti201@gmail.com>
AuthorDate: Mon Jan 30 15:36:40 2023 +0900
Commit:     Kazuki Tobita <kazukiti201@gmail.com>
CommitDate: Mon Jan 30 15:36:48 2023 +0900

    Add a.txt
0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?