Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update time when git cherry pick

Tags:

git

  1. Am I understanding correctly? When cherry pick a commit, the commit hash id and commit date will change, but AuthorDate remains the same as previous commit.

  2. If 1 is true, how can I update AuthorDate too when cherry pick?

like image 936
Sato Avatar asked Dec 21 '25 20:12

Sato


1 Answers

  1. Yes: the new commit is a new commit, so it necessarily has a different hash ID. (The existing commit remains in the repository.) Since it's a new commit, Git will assign your name as the committer, and "now" (or the computer's best guess for "now") as the time of the new commit.

    Meanwhile, git commit checks for the existence of the file CHERRY_PICK_HEAD to see if the commit is due to a cherry-pick. If so, Git extracts the original author and author-date from the original commit.

    If you supply --reset-author as a command line flag, git commit will reset the author to yourself, or to whomever you name. You can also specify an author-date at this point in this way.

  2. The git cherry-pick command won't pass --reset-author to git commit, so in a way, you can't. But there are two things you can do. Let's exit this one-item-at-a-time recap at this point.

If you run git cherry-pick -n instead of git cherry-pick, the cherry-pick command won't run git commit. You'll have to run it yourself. You can run it with --reset-author, and hence adjust the author that way.

Alternatively, having run git cherry-pick and let it build a commit, you can run git commit --amend --reset-author. This copies the existing commit to yet another new commit, whose parent is the parent of the existing commit. The new commit has you (or whoever you specify) as the author.

Note that "amending" a commit really does copy it! The commit that git cherry-pick made remains in the repository; it's just "shoved out of the way" so that you won't see it, and you won't send it to anyone else with git push. There's very little cost to this extra commit (basically one minimal-size file for some time) so there's no strong reason to prefer git cherry-pick -n over git commit --amend --reset-author.

like image 142
torek Avatar answered Dec 24 '25 10:12

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!