"git reset"和"git reset"之间有什么区别?对比"git rebase"? [英] What is the difference between "git reset" vs "git rebase"?

查看:265
本文介绍了"git reset"和"git reset"之间有什么区别?对比"git rebase"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been playing around with git (still very noob) and I wanted to know the difference between "reset" and "rebase". Is the one more powerful than the other?

说我想从历史记录中删除以粗体显示的3个提交,哪个会更好用?还是应该标记它,然后用git tag -d <tagname>删除它?

Say I wanted to delete the 3 commits in bold from the history, which one will be better to use, or should I tag it and then delete it with git tag -d <tagname>?

17a64df 2012-06-21 | Hello使用style.css(HEAD,起源/样式,母版),
a6792e4 2012-06-21 |添加了CSS样式表
801e13e 2012-06-21 |添加了自述文件
5854339 2012-06-21 |添加了index.html
0b1dd4c 2012-06-21 |已将hello.html移至lib
55649c3 2012-06-21 |添加作者/电子邮件评论
9b2f3ce 2012-06-21 |添加了作者评论
cdb39b0 2012-06-21 |使用文本(v1.1)提交p标签
b7b5fce 2012-06-21 |这会还原提交a6faf60631b5fbc6ee79b52a1bdac4c971b69ef8.
a6faf60 2012-06-21 |还原为糟糕,我们不希望该提交"
a006669 2012-06-21 |糟糕,我们不希望此提交
262d1f7 2012-06-21 |添加了HTML标头(v1)
b1846e5 2012-06-21 |添加了标准HTML页面标签(v1-beta)
bf1131e 2012-06-21 |添加了HI TAG
02b86d0 2012-06-21 |第一次提交

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 可以在工作目录和索引上使用引用,而无需进行任何提交对象(或其他对象).另一方面, git-rebase 用于重写先前创建的提交对象.

They are completely different. git-reset works with refs, on your working directory and the index, without touching any commit objects (or other objects). git-rebase on the other hand is used to rewrite previously made commit objects.

因此,如果要重写历史记录,则要使用git-rebase.请注意,您应该从不重写被推送并可供其他人使用的历史记录,因为重新基准化将重写对象,使它们与旧对象不兼容,从而使所涉及的其他任何人陷入混乱.

So if you want to rewrite the history, git-rebase is what you want. Note that you should never rewrite history that was pushed and was available to someone else, as rebasing rewrites the objects making them incompatible with the old objects, resulting in a mess for anyone else involved.

话虽这么说,您想做的是交互式变基.使用git rebase -i 262d1f7调用它,您应该看到如下提示:

That being said, what you want to do is interactive rebasing. Invoke it using git rebase -i 262d1f7 and you should get a prompt looking like this:

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就会重写您的历史记录.同样,如果您已经推送了更改,请不要这样做.总的来说,在历史上拥有这样的承诺是完全可以的.

There, simply delete the lines for the commits you want to remove, save and exit the editor and Git will rewrite your history. Again, don’t do this if you already pushed the changes. In general having such commits in the history is perfectly fine.

这篇关于"git reset"和"git reset"之间有什么区别?对比"git rebase"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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