1

I got a master branch and develop branch in my GIT. I am trying to pull the commits from develop branch to master branch (as is) along with commit hashes. I see the commits are transferred to master branch with Same Commit message but commit hash. Is there any way I can retain the commit hash when doing cherry-pick or rebase ?

For example:

develop branch has below commits

commitHash   | Commit Message
--------------------------
commitHash1  A
commitHash2   B

master branch has below commits

commitHash3  C

I tried the following methods:

Method 1

From Master :

git cherry-pick --ff <commitHash1> 
git cherry-pick -ff <commitHash2>

I see now Master has

commitHash4 A
commitHash5 B
commitHash3 C 

instead of (Expecting output in Master branch)

commitHash1 A
commitHash2 B
commitHash3 C

Any suggestions ?

Pls note: This topic is little bit different from :Specifying Git commit hash I never thought about generating a new hash, but to retain hash from old branch. so I can compare all the branches and their commits using git show-branch --topo-order --sparse --color --sha1-name origin/master origin/stage origin/develop

  • 1
    No, a commit with the same hash would have to have exactly the same content and history (i.e. be completely unchanged). That’s the purpose of the hash. – Ry- Mar 28 '18 at 21:57
  • Thanks Ryan, Is there any way I can compare multiple branches for same commit went in. for example master, stage, develop. I want to ensure all 3 branches are in sync. If different commit hashes, there is no way for me to compare branches. – Sivaswami Jeganathan Mar 28 '18 at 22:01
  • Possible duplicate of [Specifying Git commit hash](https://stackoverflow.com/questions/48226453/specifying-git-commit-hash) – phd Mar 28 '18 at 22:09
  • hi @phd , Thanks, but this topic is little bit different. I never thought about generating a new hash, but to retain hash from old branch. so I can compare all the branches and their commits using `git show-branch --topo-order --sparse --color --sha1-name origin/master origin/stage origin/develop` – Sivaswami Jeganathan Mar 28 '18 at 22:16
  • 1
    As you've seen, the answer is no—but there is an existing Git tool for achieving your ultimate goal: see `git cherry` (not cherry-pick!). – torek Mar 28 '18 at 22:29
  • Thanks @torek . will try with cherry. – Sivaswami Jeganathan Mar 28 '18 at 23:43

1 Answers1

6

No, you can't do that. The commit hash effectively embodies the history of the repo at that commit. It's not possible for two commits to have the same hash but different (directly or indirectly) preceding commits. They are actually different commit objects, just with the same or similar diffs.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thanks Ken, The same answer from @Ryan. Is there any way I can compare multiple branches for same commit went in. for example master, stage, develop. I want to ensure all 3 branches are in sync. If different commit hashes, there is no way for me to compare branches. Because the commit messages can change when I do re-base or merge with extra commits. – Sivaswami Jeganathan Mar 28 '18 at 22:04