gpt4 book ai didi

git - "git reset"和 "git rebase"有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 00:44:33 24 4
gpt4 key购买 nike

我一直在玩弄 git(仍然很菜鸟),我想知道“reset”和“rebase”之间的区别。一个比另一个更强大吗?

假设我想从历史中删除 3 个粗体提交,使用哪个更好,或者我应该标记它然后用 git tag -d <tagname> 删除它?

17a64df 2012-06-21 | Hello uses style.css (HEAD, origin/style, master),
a6792e4 2012-06-21 | Added css stylesheet
801e13e 2012-06-21 | Added README
5854339 2012-06-21 | Added index.html
0b1dd4c 2012-06-21 | Moved hello.html to lib
55649c3 2012-06-21 | Add an author/email comment
9b2f3ce 2012-06-21 | Added an author comment
cdb39b0 2012-06-21 | Commit p tags with text (v1.1)
b7b5fce 2012-06-21 | This reverts commit a6faf60631b5fbc6ee79b52a1bdac4c971b69ef8.
a6faf60 2012-06-21 | Revert "Oops, we didn't want this commit"
a006669 2012-06-21 | Oops, we didn't want this commit
262d1f7 2012-06-21 | Added HTML header (v1)
b1846e5 2012-06-21 | Added standard HTML page tags (v1-beta)
bf1131e 2012-06-21 | Added HI TAG
02b86d0 2012-06-21 | First Commit

最佳答案

它们完全不同。 git-reset在您的工作目录和索引上与 refs 一起工作,而无需触及任何提交对象(或其他对象)。 git-rebase另一方面用于重写以前创建的提交对象。

所以如果你想重写历史,git-rebase 就是你想要的。请注意,您应该永远不要重写已推送并可供其他人使用的历史记录,因为 rebase 会重写对象,使它们与旧对象不兼容,从而给其他相关人员造成困惑。

话虽这么说,你想要做的是interactive rebasing .使用 git rebase -i 262d1f7 调用它,您应该会得到如下所示的提示:

pick 262d1f7 Added HTML header (v1)
pick a006669 Oops, we didn't want this commit
pick a6faf60 Revert "Oops, we didn't want this commit"
pick b7b5fce This reverts commit a6faf60631b5fbc6ee79b52a1bdac4c971b69ef8.
pick cdb39b0 Commit p tags with text (v1.1)
pick 9b2f3ce Added an author comment
pick 55649c3 Add an author/email comment
pick 0b1dd4c Moved hello.html to lib
pick 5854339 Added index.html
pick 801e13e Added README
pick a6792e4 Added css stylesheet
pick 17a64df Hello uses style.css (HEAD, origin/style, master),

在那里,只需删除您要删除的提交行,保存并退出编辑器,Git 就会重写您的历史记录。同样,如果您已经推送了更改,不要这样做。一般来说,在历史记录中有这样的提交是完全没问题的。

关于git - "git reset"和 "git rebase"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11225293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com