Git Revert,Checkout和Reset有什么区别? [英] What's the difference between Git Revert, Checkout and Reset?

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

问题描述

我试图学习如何将文件和项目恢复或回滚到以前的状态,但不明白 git revert 结帐重置。为什么有3个不同的命令用于看似相同的目的,何时应该有人选择另一个呢?

解决方案

这三个命令有完全不同的目的。它们甚至不是很相似。



git revert



这个命令创建一个新的提交,撤销之前提交的更改。这个命令将新的历史添加到项目中(它不会修改现有的历史记录)。 h2>

该命令检出存储库中的内容并将其放入工作树中。它也可以具有其他效果,具体取决于命令的调用方式。例如,它也可以更改您当前正在处理的分支。此命令不会对历史进行任何更改。



git reset



这个命令稍微复杂一点。它根据调用的方式实际上做了几件不同的事情。它修改了索引(所谓的暂存区域)。或者它改变分支头当前指向的提交。这个命令可以改变现有的历史(通过改变分支引用的提交)。

使用这些命令



如果在项目历史的某个地方提交了一个提交,并且之后你决定提交是错误的并且不应该完成,那么<​​code> git revert 是作业的工具。它将撤消由错误提交引入的更改,并在历史记录中记录撤消。

如果您修改了工作树中的文件,但没有提交更改,然后您可以使用 git checkout 来检出文件的新存储副本。



如果你已经提交了一个提交,但没有与其他人分享,并且你决定不需要它,那么你可以使用 git reset 来重写历史记录所以它看起来好像你从未做过提交。



这些只是一些可能的使用场景。还有其他的命令在某些情况下很有用,上面的三个命令也有其他用途。


I am trying to learn how to restore or rollback files and projects to a prior state, and don't understand the difference between git revert, checkout, and reset. Why are there 3 different commands for seemingly the same purpose, and when should someone choose one over the other?

解决方案

These three commands have entirely different purposes. They are not even remotely similar.

git revert

This command creates a new commit that undoes the changes from a previous commit. This command adds new history to the project (it doesn't modify existing history).

git checkout

This command checks-out content from the repository and puts it in your work tree. It can also have other effects, depending on how the command was invoked. For instance, it can also change which branch you are currently working on. This command doesn't make any changes to the history.

git reset

This command is a little more complicated. It actually does a couple of different things depending on how it is invoked. It modifies the index (the so-called "staging area"). Or it changes which commit a branch head is currently pointing at. This command may alter existing history (by changing the commit that a branch references).

Using these commands

If a commit has been made somewhere in the project's history, and you later decide that the commit is wrong and should not have been done, then git revert is the tool for the job. It will undo the changes introduced by the bad commit, recording the "undo" in the history.

If you have modified a file in your working tree, but haven't committed the change, then you can use git checkout to checkout a fresh-from-repository copy of the file.

If you have made a commit, but haven't shared it with anyone else and you decide you don't want it, then you can use git reset to rewrite the history so that it looks as though you never made that commit.

These are just some of the possible usage scenarios. There are other commands that can be useful in some situations, and the above three commands have other uses as well.

这篇关于Git Revert,Checkout和Reset有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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