Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cherry-pick a commit and keep original SHA code

I would like to cherry-pick a commit on a fetched remote while keeping it's original SHA commit code (my current branch is based on this remote which I resetted to a previous state).

like image 660
blameless75 Avatar asked Feb 03 '13 20:02

blameless75


People also ask

Does a cherry-pick Create a new Sha?

So, you will not be able to generate the same SHA hash after a cherry-pick (unless you find a SHA collision ;) ).

How do you cherry-pick a commit SHA?

Cherry-pick using Git commit hash In order to see the commit hashes for your current branch, simply run the “git log” command with the “–oneline” option in order to make it more readable. By default, the log command will display the commits from the history beginning until the top of your current branch.

Does cherry-pick remove original commit?

This makes it useful for undoing changes, however, it's noteworthy to remember that cherry-picking will NOT delete the commit that was cherry-picked from the feature branch, so you will have to create a new feature branch and cherry-pick all your commits once more without the bug fix.

Does git cherry-pick change hash?

When you are performing a regular cherry-pick, you will get a new commit hash but the commit message will be the same. However, there is a way to append the origin of a cherry-pick to the commit message: by using the cherry-pick with the “-x” option.


2 Answers

A git SHA hash is computed from different pieces of information:

  1. The tree it refers to; basically, the current content of the repository in the branch in which the commit appears.
  2. The SHA of the parent commit(s).
  3. The commit message.
  4. The author information: name, email and timestamp.
  5. The committer information: name, email and timestamp.

Even if you edit a cherry-picked commit so that the tree, the commit message, the author and committer information are exactly the same, the SHA of the parent commit (or commits, if dealing with merge commits) will be always different. So, you will not be able to generate the same SHA hash after a cherry-pick (unless you find a SHA collision ;) ).

like image 68
Marco Leogrande Avatar answered Sep 19 '22 14:09

Marco Leogrande


The SHA commit hash is made of from the state of the repository, using the whole history up to the point of the commit (branches not included). This means that you cannot keep the original hash on cherry-picking unless the whole history is the same, and in that case cherry-picking would make no sense.

like image 36
eis Avatar answered Sep 21 '22 14:09

eis