R.A. Epigonos et al.

[git] 空リポジトリ(init)、空ブランチ(checkout --orphan)、空コミット(commit --allow-empty) の作成

テスト用にリポジトリ、ブランチ、コミットを作りたい時に。

空リポジトリ (init)

空リポジトリを作るには init を使う。

$ git init
Initialized empty Git repository in *******************************************/.git/

空ブランチ (checkout --orphan)

空ブランチを作るには checkout --orphan を使う。

$ git checkout --orphan dummy
Switched to a new branch 'dummy'

空コミット (commit --allow-empty)

空コミットを作るには commit --allow-empty を使う。

$ git commit --allow-empty --message 'initial commit'
[dummy (root-commit) *******] initial commit
$ git log --color --stat --pretty=fuller -1
commit ****************************************
Author:     **************** <****@*******>
AuthorDate: *** *** ** **:**:** **** +****
Commit:     **************** <****@*******>
CommitDate: *** *** ** **:**:** **** +****

    initial commit

上では隠したい情報を手作業でマスクしたわけだけど、そんなめんどくさいことはしたくない。コミットメッセージ (--allow-empty-message --message '') や作者 (--author='foo <>') と作者日時 (--date '1970-01-01 00:00:00') を指定するには以下。

$ git commit --allow-empty --allow-empty-message --message '' --author 'foo <>' --date '1970-01-01 00:00:00'
[dummy *******]
 Author: foo <>
 Date: Thu Jan 1 00:00:00 1970 +0000
$ git log --color --stat --pretty=fuller -1
commit ****************************************
Author:     foo <>
AuthorDate: Thu Jan 1 00:00:00 1970 +0000
Commit:     **************** <****@*******>
CommitDate: *** *** ** **:**:** **** +****

コミッタ(GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL)とコミット日時(GIT_COMMITTER_DATE)を変更するには以下のように環境変数を使う。上記の情報は環境変数を使って変更することも可能。最終的に隠さなければいけない情報はコミットハッシュだけになった。

$ GIT_AUTHOR_NAME='author' \
	GIT_AUTHOR_EMAIL='author@example.com' \
	GIT_AUTHOR_DATE="$(TZ=GMT date --date='@0')" \
	GIT_COMMITTER_NAME='comitter' \
	GIT_COMMITTER_EMAIL='comitter@example.com' \
	GIT_COMMITTER_DATE="$(TZ=GMT date --date '@1')" \
	git commit --allow-empty --allow-empty-message --message ''
[dummy *******]
 Author: author <author@example.com>
$ git log --color --stat --pretty=fuller -1
commit ****************************************
Author:     author <author@example.com>
AuthorDate: Thu Jan 1 00:00:00 1970 +0000
Commit:     comitter <comitter@example.com>
CommitDate: Thu Jan 1 00:00:01 1970 +0000

リファレンス

  1. Git - git-checkout Documentation
  2. Git - git-commit Documentation
  3. RFC 3092 - Etymology of "Foo"
  4. RFC 2606 - Reserved Top Level DNS Names
  5. Man page of DATE
  6. hash - How does git compute file hashes? - Stack Overflow
  7. Gitはsha1の計算をどのようにやっているのか · DQNEO起業日記
  8. Git - Git Objects
  9. The anatomy of a Git commit
  10. Gitのリビジョン番号(SHA-1のアレ)の正体をソースコードを読んで調べてみた - Qiita

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2008-03-29T06:22:56+09:00
  2. Modified: 2008-03-29T06:22:56+09:00
  3. Generated: 2023-08-27T23:09:16+09:00