如何将 Git 中的前4次提交删除,就好像第5次提交是第一次提交一样?

huangapple go评论60阅读模式
英文:

How to remove the first 4 commits in git as if 5th commit was the first one?

问题

我想上传项目到远程,但在前几次提交中无法上传。

例如,我有像这样的git提交:

...(许多新提交)

  • 可呈现的提交
  • 我不想分享的提交#4
  • 我不想分享的提交#3
  • 我不想分享的提交#2
  • 我不想分享的提交#2
  • 我不想分享的提交#1

我查看了一些建议使用 git rebase -i 的来源,我期望它只会移除我压缩的那些提交,但似乎会导致合并冲突,尽管我根本没有改变任何东西。我该如何只是切掉尾部,就好像我没有做过那4次提交呢?

英文:

I want to upload the project to remote but I can't with the first few commits.

For example I have git commits like this:

...(Many new commits)

  • Presentable Commit
  • Commit I don't want to share #4
  • Commit I don't want to share #3
  • Commit I don't want to share #2
  • Commit I don't want to share #2
  • Commit I don't want to share #1

I looked through sources that suggested using git rebase -i, I expected it to just remove the commits which I squashed but that seems to cause merge conflicts even though I am not changing anything at all. How can I just cut off the tail as if I didn't make those 4 commits?

答案1

得分: 2

如果这是一条直线历史,您可以这样做:

git branch --orphan new-branch commit5 # 创建一个新分支,从commit 5的内容开始,没有历史记录
git commit -m "开始项目的历史 _再次_"
git cherry-pick commit5..original-branch

这将创建一个与您想要的一样的历史记录... 如果您喜欢它,可以随意将任何分支放在那里,并删除new-branch:

git checkout -B main # 设置main分支在这里。如果已经存在,就切换到这里
git branch -d new-branch # 删除new-branch

如果不是一条直线历史(存在合并等情况),您可以使用我为此开发的脚本(https://github.com/eantoranz/git-replay):

git branch --orphan new-branch commit5 # 创建一个新分支,从commit 5的内容开始,没有历史记录
git commit -m "开始项目的历史 _再次_"
./git-replay.py HEAD commit5 the-original-branch

这将打印出一个具有您想要的历史记录的提交ID。使用git log查看一下或者切换到它... 如果您喜欢它,然后在提交上放一个分支,并删除new-branch,因为它不再需要了。

英文:

If it is a straight line history, you can do this:

git branch --orphan new-branch commit5 # create a new branch that starts with the content of commit 5, no history
git commit -m "Starting the history of the project _again_"
git cherry-pick commit5..original-branch

That will create a history like the one you want.... if you like it, feel free to place whatever branch you like there and delete new-branch

git checkout -B main # set main over here. If it exists already, move over here
git branch -d new-branch # delete new-branch

If is is not a straight line history (you have merges and stuff), you can use a script I have developed for this (https://github.com/eantoranz/git-replay):

git branch --orphan new-branch commit5 # create a new branch that starts with the content of commit 5, no history
git commit -m "Starting the history of the project _again_"
./git-replay.py HEAD commit5 the-original-branch

That will print the ID of a commit that has a history like the one you want. Take a look with git log or do a checkout on it.... if you like it, then place a branch on the commit and remove new-branch as there is no need for it anymore.

huangapple
  • 本文由 发表于 2023年7月7日 02:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定