Updates recent tutorial post about Git advanced history rewriting :

* de-indents `msg-filter` script
* mitigates collisions by checking if old SHA candidate could be mapped to a new commit reference
* adds a note about filter-repo supporting this feature by default
This commit is contained in:
Samuel FORESTIER 2022-03-29 19:41:31 +02:00
parent f2f9bfd908
commit fdae3c4570

@ -1,6 +1,7 @@
---
title: "How to rewrite Git history while keeping message commit references"
date: 2022-03-26 12:41
modified_date: 2022-03-29 19:17
url: how-to-rewrite-git-history-while-keeping-message-commit-references
layout: post
category: Tutorials
@ -58,10 +59,14 @@ git filter-branch \
message="$(cat)"
commit_refs="$(echo "$message" | LC_ALL=C grep -oE "\b[0-9a-fA-F]{7,40}\b")"
for commit_ref in $commit_refs; do
new_sha="$(grep "^${commit_ref}" ../commits_mapping | cut -d, -f2)"
commit_len="$(printf "%s" "$commit_ref" | wc -m)"
sha="$(echo "$new_sha" | cut -c "1-${commit_len}")"
message="$(echo "$message" | sed "s/${commit_ref}/${sha}/g")"
new_sha="$(grep "^${commit_ref}" ../commits_mapping | cut -d, -f2)"
if test -z "$new_sha"
then
continue;
fi
commit_ref_len="$(printf "%s" "$commit_ref" | wc -m)"
new_commit_ref="$(echo "$new_sha" | cut -c "1-${commit_ref_len}")"
message="$(echo "$message" | sed "s/${commit_ref}/${new_commit_ref}/g")"
done
echo "$message"
@ -119,7 +124,10 @@ decadde
### Last words
Please also note that `git filter-branch` usage is [deprecated since Git v2.24.0](https://github.com/git/git/commit/9df53c5de6e687df9cd7b36e633360178b65a0ef), and [filter-repo](https://github.com/newren/git-filter-repo/) should be preferred.
If you managed to adapt the solution described in this post with this tool, feel free to post a comment below !
~~If you managed to adapt the solution described in this post with this tool, feel free to post a comment below !~~
It actually appeared that [filter-repo supports this feature by default](https://github.com/newren/git-filter-repo/#design-rationale-behind-filter-repo) ! :tada:
So it definitely should be preferred over `git filter-branch`, but sometimes, only legacy tools are available...
---