How to modify only a range of commits with git filter-repo instead of the entire branch history?
Asked Answered
P

1

7

I want to use git filter-repo to automatically apply some scripted style-guide refactoring for my multi-commit pull requests.

Therefore I want to apply the operations only to the few latest new commits I'm pushing, but not touch anything else in older history.

Is there a way?

For the sake of concreteness, here's a test repo : https://github.com/cirosantilli/test-git-filter-repository and a sample blob operation:

# Install git filter-repo.
# https://superuser.com/questions/1563034/how-do-you-install-git-filter-repo/1589985#1589985
python3 -m pip install --user git-filter-repo

# Usage.
git clone https://github.com/cirosantilli/test-git-filter-repository
cd test-git-filter-repository
printf 'd1==>asdf
d2==>qwer
' > replace.txt
git filter-repo --replace-text replace.txt --refs HEAD

based on: How to replace a string in a whole Git history?

The above would affect all 3 commits of the test repo. Is there a way to affect only the last 2 commits instead for example?

A blob operation should work perfectly for my use case, as I only want to touch blobs that my commits modified.

I couldn't find it easily in the documentation.

I also asked on their issue tracker: https://github.com/newren/git-filter-repo/issues/157

Tested on git-filter-repo ac039ecc095d.

Pule answered 1/10, 2020 at 11:38 Comment(1)
Use an interactive rebase?Escapee
A
8

Try specifying a commit range as your --refs argument :

git filter-repo ... --refs HEAD~2..master

In the documentation (link), I haven't found an explicit statement saying that commit ranges are vaild values for --refs <refs+>, but the code says that it is passed straight to git fast-export, so they are in practice.

Accusative answered 1/10, 2020 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.