git filter-branch
can do this but is probably a lot heavier weight than needed.
How big and branch-y is your history? If it's small and short, the easiest way to do this would be add the new file now, then use git rebase -i --root
to move the new commit to the 2nd position and squash it into the root commit.
For instance, let's say you have:
$ git log --oneline --graph --decorate --all
* e8719c9 (HEAD, master) umlaut
* b615ade finish
* e743479 initial
(your SHA-1 values will differ of course) and you want to add LICENSE.txt
(already in the work dir) to the tree as part of the root commit. You can just do it now:
$ git add LICENSE.txt && git commit -m 'add LICENSE, for fixup into root'
[master 924ccd9] add LICENSE, for fixup into root
1 file changed, 1 insertion(+)
create mode 100644 LICENSE.txt
then run git rebase -i --root
. Grab the last line (pick ... add LICENSE, ...
) and move it to the second line, changing pick
to fixup
, and write the rebase-commands file out and exit the editor:
".git/rebase-merge/git-rebase-todo" 22L, 705C written
[detached HEAD 7273593] initial
2 files changed, 4 insertions(+)
create mode 100644 LICENSE.txt
create mode 100644 x.txt
Successfully rebased and updated refs/heads/master.
The (new, completely-rewritten) history now looks more like this:
git log --oneline --graph --decorate --all
* bb71dde (HEAD, master) umlaut
* 7785112 finish
* 7273593 initial
and LICENSE.txt
is in all commits.
If you do have a more complicated (branchy) history and want to use git filter-branch
to get it all updated, the --tree-filter
you need is not:
'git add LICENSE.txt'
but rather:
'cp /somewhere/outside/the/repo/LICENSE.txt LICENSE.txt'
to copy the new file into the tree each time. (A faster method would be to use the --index-filter
but this is more complex.)