GIT: How do I add a file to the first commit (and rewrite history in the process)?

前端 未结 3 1796
北恋
北恋 2020-12-29 06:35

I\'d like to add one file to git repository as if it was there from the start. I only found explanations how to remove a file from entire history, not how to add one.

<
3条回答
  •  余生分开走
    2020-12-29 07:20

    --index-filter provides a simple and fast solution:

    git filter-branch --index-filter "cp /abs/path/to/LICENSE.txt . && git add LICENSE.txt" --tag-name-filter cat --prune-empty -- --all
    

    Here is a very simple benchmark against other proposed methods.

    The first column (large) shows the timings in seconds for one run of each filter in copies of the Git project repository (45885 commits, checkout of ~30M). The rebase method does not apply since it does not handle merges automatically, even with the -c option.

    The second columns (medium) shows median times for three runs of each method in copies of a medium sized repository with a linear history and fairly large trees (2430 commits, checkout of ~80M).

    The third column (small) shows median times for three runs of each method in copies of a small repository (554 commits, checkout of ~100K).

                  large medium  small
    index-filter   1064     38     10
    tree-filter    4319     81     15
    rebase            -    116     28
    

    Also note that the rebase is not functionally equivalent to the filter-branch variants, as it updates the committer dates.

提交回复
热议问题