0

I have lost access to laptop where I pushed only the merged branch (onto master) but forgot to update the branch itself.

So I tried the following little experiment:

$ mkdir g
$ cd g
$ git init
$ echo 1 >> README.md
$ git add README.md 
$ git commit -m"First commit" -- README.md 
$ git checkout -b b1
$ echo 2 >> README.md
$ git commit -m"Second commit" -- README.md 
$ git checkout master
$ git merge b1
$ git checkout b1
$ git reset --hard HEAD~
$ git cherry-pick 365163b00fee76cdc6d3e34b139b0ed42b184437

The above does not re-create commit 365163b00fee76cdc6d3e34b139b0ed42b184437 identically but create another one (d6debaa050c5).

What is the right command to recreate a commit from an existing commit onto a branch (no access to reflog) ?


For my specific need, I can check that I do have the following informations:

$ git show --pretty=fuller 365163b00fee76cdc6d3e34b139b0ed42b184437
commit 365163b00fee76cdc6d3e34b139b0ed42b184437
Author:     Mathieu Malaterre <example@example.org>
AuthorDate: Tue Feb 25 14:02:06 2020 +0100
Commit:     Mathieu Malaterre <example@example.org>
CommitDate: Tue Feb 25 14:02:06 2020 +0100

    Second commit

diff --git a/README.md b/README.md
index d00491fd7e5b..1191247b6d9a 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 1
+2
  • Branch b1 has not changed, so parent sha1 is correct,
  • commit information is correct (message, author, committer and timestamp).
malat
  • 11,097
  • 10
  • 70
  • 131

1 Answers1

2

you can never exactly recreate the exact same commit hash. The content can be the same although the hash differs.

The following information is all used to determine the commit hash:

  • The source tree of the commit (which unravels to all the subtrees and blobs)
  • The parent commit sha1
  • The author info (with timestamp)
  • The committer info (right, those are different!, also with timestamp)
  • The commit message

(see this answer).

Chris Maes
  • 26,426
  • 4
  • 84
  • 113
  • Technically, you can reproduce the original commit, including its original timestamps, parent commit hashes, etc., and thus get the original commit back. But to do that, you have to have the original commit. So then you didn't make a new commit, you just made the original commit again yesterday, just now. :-) – torek Feb 25 '20 at 19:38