Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running filter-branch over a range of commits

git filter-branch --env-filter '
export GIT_AUTHOR_EMAIL="[email protected]"
export GIT_AUTHOR_NAME="foo"' -- commita..commitb

Results in Which ref do you want to rewrite?

So it seems that filter-branch doesn't allow you to use range notation use a range between two arbitrary refs.

What is the most straight forward way of running a filter over a range of consecutive commits (somewhere within the history of a branch) if this approach isn't possible.

like image 654
Acorn Avatar asked Mar 06 '13 14:03

Acorn


4 Answers

You cannot apply the filter-branch in the middle of the history, as said by @kan. You must apply from your known commit to the end of the history

git filter-branch --env-filter '...' SHA1..HEAD

Filter-branch can check for the commit author or other information, to chose to change or not the commit, so there are ways to accomplish what you want, see https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History, look for "Changing Email Addresses Globally"

Remember: if you have pushed the commits to a public repository you should not user filter-branch

like image 106
Pedro Sanção Avatar answered Nov 17 '22 14:11

Pedro Sanção


The cleanest solution I found was to do the following:

  1. Create a temporary branch at refb.
  2. Apply the branch filter on refa..temp.
  3. Rebase onto the temporary branch and delete it.

ie.

git branch temp refb

git filter-branch --env-filter '
export GIT_AUTHOR_EMAIL="[email protected]"' refa..temp

git rebase temp
git branch --delete temp
like image 30
Acorn Avatar answered Nov 17 '22 14:11

Acorn


git filter-branch does accept range notation, but the end of the range needs to be a reference, not the ID of a commit.

git checkout -b tofilter commitb
git filter-branch .... commita..tofilter

If given just commits, it would not know what ref to update with the filtered branch.

like image 12
qqx Avatar answered Nov 17 '22 15:11

qqx


Enclose you filter commands in an if-statement that checks for that range. You can check whether a commit is within a given range with this command:

git rev-list start..end | grep **fullsha**

The current commit will be stored in $GIT_COMMIT in your filter. So your filter becomes:

git filter-branch --env-filter '
  if git rev-list commita..commitb | grep $GIT_COMMIT; then
    export GIT_AUTHOR_EMAIL="[email protected]"
    export GIT_AUTHOR_NAME="foo"
  fi' -- ^commita --all

If you want to only rewrite your current branch, replace --all with HEAD

like image 7
Chronial Avatar answered Nov 17 '22 15:11

Chronial