GIT:如何将文件添加到第一次提交(并在过程中重写历史记录)? [英] GIT: How do I add a file to the first commit (and rewrite history in the process)?

查看:321
本文介绍了GIT:如何将文件添加到第一次提交(并在过程中重写历史记录)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个文件添加到git存储库,就像它从一开始就一样。我只找到解释如何从整个历史中删除文件,而不是如何添加文件。



我试过 git filter-branch --tree-过滤器'git add LICENSE.txt'但我得到的错误是无法找到该文件。

解决方案

git filter-branch can 可以做到这一点,但可能比需要的重得多。

你的历史有多大和分支?如果它小而短,最简单的方法是现在添加新文件,然后使用 git rebase -i --root 将新提交移动到第二个例如,假设您有:

   $ git log --oneline --graph --decorate --all 
* e8719c9(HEAD,master)变音符
* b615ade完成符
* e743479初始符

(您的SHA-1值当然会有所不同),您想要添加 LICENSE。作为根提交的一部分,txt (已经在工作目录中)作为树的一部分。您现在可以这样做:

  $ git add LICENSE.txt&& git commit -m'添加LICENSE,用于修复根目录'
[master 924ccd9]添加LICENSE,用于修复根目录
1文件已更改,1个插入(+)
创建模式100644 LICENSE。 txt

然后运行 git rebase -i --root 。抓住最后一行( pick ... add LICENSE,... )并将它移动到第二行,改变 pick fixup ,然后写出rebase-commands文件并退出编辑器:

 .git / rebase-merge / git-rebase-todo22L,705C写入
[detached HEAD 7273593]初始
2个文件已更改,4个插入(+)
创建模式100644 LICENSE.txt
创建模式100644 x.txt
成功重新设计并更新了裁判/头像/主控。

(新的,完全重写的)历史现在看起来更像这样:

  git log --oneline --graph --decorate --all 
* bb71dde(HEAD,master)变音符
* 7785112完成
* 7273593初始

LICENSE.txt 在所有提交中。






如果你确实有一个更复杂 git filter-branch 让它全部更新,你需要的 - tree-filter 不是:

 'git add LICENSE.txt'

而是:

 'cp /somewhere/outside/the/repo/LICENSE.txt LICENSE。 txt'

可以每次将新文件复制到树中。 (更快的方法是使用 - index-filter 但这更复杂。)


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.

I tried git filter-branch --tree-filter 'git add LICENSE.txt' but I got error that the file can't be found.

解决方案

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.)

这篇关于GIT:如何将文件添加到第一次提交(并在过程中重写历史记录)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆