Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git-LFS migrate using the new .gitattributes file

Tags:

git

git-lfs

I have a largish (32k commits) git repository in which I need to rewrite history in one branch to remove a bunch of large files as described by a .gitattributes file. This branch is entirely local and has never hit a remote (in fact our remote is rejecting it because of the large files in history).

I know that the following command will go through the history of the branch and remove all .dll files:

$ git lfs migrate import --include='*.dll'

but since the .gitattributes file exists and is rather extensive, is there a command that simply replays the work that would have been done to pointer-ize those files, if the .gitattributes file had existed back when the branch was created?

like image 767
Adam Smith Avatar asked Sep 30 '19 07:09

Adam Smith


People also ask

Where do I put the Gitattributes file?

These path-specific settings are called Git attributes and are set either in a . gitattributes file in one of your directories (normally the root of your project) or in the . git/info/attributes file if you don't want the attributes file committed with your project.


1 Answers

I would first insert the correct .gitattributes at the beginning of the branch (using git rebase for example) :

*--*--x--*--*--*--*--* <- master
       \
        *--*--*--*--*--*--a--* <- my/branch
                          ^
                          commit with updated .gitattributes

# with the commits identified as above, from branch my/branch, run :
$ git rebase -i x
   ...
   # in the opened editor, move commit 'a' at the beginning of the list
   # save & close

# you should obtain :
*--*--x--*--*--*--*--* <- master
       \
        a'--*--*--*--*--*--*--* <- my/branch (rewritten)
        ^
      rewritten commit

After that :

You can use git filter-branch --tree-filter to have git replay the commits one after another, and apply the filters described in .gitattributes :

# first arg is the name of a script to execute on each commit
#   you have nothing to edit : just use 'true' as an action
#   the only action you expect is that git applies the filters
#   when re-staging files for each individual commit

git filter-branch --tree-filter true a'..my/branch

You may want to add the --prune-empty option, or you can remove the empty commits after the rewriting, using once again git rebase -i for example.

like image 139
LeGEC Avatar answered Sep 20 '22 12:09

LeGEC