为了账号安全,请及时绑定邮箱和手机立即绑定

用Git更改项目的第一次提交?

用Git更改项目的第一次提交?

Git
慕姐4208626 2019-07-27 15:22:06
用Git更改项目的第一次提交?我希望在我的项目的第一次提交中更改某些内容而不会丢失所有后续提交。有没有办法做到这一点?我不小心在源代码中的评论中列出了我的原始电子邮件,我想改变它,因为我从机器人索引GitHub收到垃圾邮件。
查看完整描述

3 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

正如提到的ecdpalma 下面GIT中1.7.12+(2012年8月)具有增强的选项--rootgit rebase

“ git rebase [-i] --root $tip”现在可用于重写所有导致“ $tip”到根提交的历史记录。

这个新行为最初在这里讨论

我个人认为“ git rebase -i --root”应该只是工作而不需要“ --onto”让你“编辑”甚至是历史上的第一个。
可以理解的是,没有人会感到困扰,因为人们在历史的最初阶段重写的次数要少得多。

随后是补丁


(原答案,2010年2月)

正如Git FAQ(和这个SO问题)中提到的,这个想法是:

  1. 创建新的临时分支

  2. 将其回滚到您要使用的更改提交 git reset --hard

  3. 更改提交(它将是当前HEAD的顶部,您可以修改任何文件的内容)

  4. Rebase分支在更改提交之上,使用:

    git rebase --onto <tmp branch> <commit after changed> <branch>`

诀窍是确保您要删除的信息不会被文件中其他位置的后续提交重新引入。如果您怀疑,那么您必须使用filter-branch --tree-filter以确保该文件的内容在任何提交中都不包含敏感信息。

在这两种情况下,您最终都会重写每次提交的SHA1,因此如果您已经发布了要修改其内容的分支,请务必小心。你可能不应该这样做,除非你的项目尚未公开,而其他人没有基于你即将重写的提交工作。


查看完整回答
反对 回复 2019-07-27
?
函数式编程

TA贡献1807条经验 获得超9个赞

1.7.12发行说明中所述,您可以使用

$ git rebase -i --root


查看完整回答
反对 回复 2019-07-27
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

git rebase -i允许您方便地编辑除根提交之外的任何先前提交。以下命令显示如何手动执行此操作。


# tag the old root, "git rev-list ..." will return the hash of first commit

git tag root `git rev-list HEAD | tail -1`


# switch to a new branch pointing at the first commit

git checkout -b new-root root


# make any edits and then commit them with:

git commit --amend


# check out the previous branch (i.e. master)

git checkout @{-1}


# replace old root with amended version

git rebase --onto new-root root


# you might encounter merge conflicts, fix any conflicts and continue with:

# git rebase --continue


# delete the branch "new-root"

git branch -d new-root


# delete the tag "root"

git tag -d root


查看完整回答
反对 回复 2019-07-27
  • 3 回答
  • 0 关注
  • 765 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信