git 撤销更改_如何撤消Git中的更改

git 撤销更改

You may already know that Git is like a save point system. What you generally learn with Git initially is to learn to save your changes and commit them to a remote repository. But how do you undo a change, and go back to a previous state?

您可能已经知道Git就像一个保存点系统。 通常,您最初使用Git所学的是学习保存更改并将其提交到远程存储库。 但是,如何撤消更改并返回到先前的状态?

That’s what we’re going to cover within this article.

这就是我们将在本文中介绍的内容。

I’ve covered the contents in this article in a video if you like learning by watching instead of reading.

如果您喜欢通过观看而不是阅读来学习,那么我已经在视频中介绍了本文的内容。

本地与远程 (Local vs Remote)

It’s more complicated to undo something that’s already on the remote. This is why you want to keep things on your local until they’re kind of confirmed.

撤消遥控器上已经存在的内容更为复杂。 这就是为什么您希望在事物被确认之前将其保留在本地的原因。

四种常见情况 (Four common scenarios)

We’ll be covering the following four common scenarios

我们将介绍以下四种常见情况

  1. Discarding local changes

    放弃本地更改
  2. Amending the previous commit

    修改上一次提交
  3. Rolling back to a previous commit

    回滚到上一个提交
  4. Reverting a commit that has been pushed to the remote

    还原已推送到远程的提交

Note: In the screen shots below, I’ve used the Fork for Mac OS Git Client. You can do the same in other similar Git clients.

注意:在下面的屏幕截图中,我使用了For Mac OS Git Client的Fork 。 您可以在其他类似的Git客户端中执行相同的操作。

方案1:放弃本地更改 (Scenario 1: Discarding local changes)

The first scenario is when you’ve created some changes. They’re not committed yet. And you want to delete these changes.

第一种情况是创建一些更改后。 他们尚未承诺。 您要删除这些更改。

Let’s say we want to create a new feature. We’re going to add some HTML and CSS into the project:

假设我们要创建一个新功能。 我们将在项目中添加一些HTML和CSS:

<!--In index.html-->
<div class="feature"></div>
```

```css
/* In CSS file */
.feature {
  font-size: 2em; 
  /* Other styles */
}

To discard these changes:

放弃这些更改:

  1. Go to the staging area

    转到暂存区
  2. Select the files where you want to discard changes

    选择您要放弃更改的文件
  3. Right click on the files

    右键单击文件
  4. Select discard changes

    选择放弃更改
方案2:修改上一次提交 (Scenario 2: Amending the previous commit)

When you have created a commit and you missed out some changes and you want to add these changes in the previous commit message.

创建提交后,您错过了一些更改,并且希望将这些更改添加到上一个提交消息中。

  1. Go to the staging area

    转到暂存区
  2. Stage the files to commit

    暂存文件以提交
  3. Click on the amend checkbox

    单击修改复选框
  4. Edit your commit message

    编辑您的提交消息
  5. Commit

    承诺
方案3:回滚到上一个提交 (Scenario 3: Rolling back to a previous commit)

You already have a few commits in your local repository. You decide that you don’t want these commits anymore and you want to “load” your files from a previous state.

您的本地存储库中已经有一些提交。 您决定不再需要这些提交,而要从先前的状态“加载”文件。

  1. Go into the Git History

    进入Git历史
  2. Right click the commit you want to roll back to

    右键单击要回滚的提交
  3. Select reset branch to here

    选择重置branch到这里

Note: You can only reset to a commit that hasn’t been pushed into the remote.
注意:您只能将其重置为尚未推送到远程的提交。
方案4:还原已推送到远程的提交 (Scenario 4: Reverting a commit that has been pushed to the remote)

If you have a commit that has been pushed into the remote branch, you need to revert it.

如果提交已被推送到远程分支,则需要将其还原。

Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line. If you removed a line, this revert commit will add the line back.
还原意味着通过创建新的提交来撤消更改。 如果添加了一行,则此还原提交将删除该行。 如果您删除了一行,此还原提交将把该行重新添加回去。

To revert, you can:

要还原,您可以:

  1. Go to the Git history

    转到Git历史
  2. Right click on the commit you want to revert

    右键单击要还原的提交
  3. Select revert commit

    选择还原提交
  4. Make sure commit the changes is checked.

    确保commit the changes已检查。

  5. Click revert

    点击还原

其他情况 (Other scenarios)

GitHub has a useful article that shows you how to undo almost everything with Git. It will be helpful if you face other scenarios. Read it here.

GitHub上有一篇有用的文章,向您展示了如何使用Git撤消几乎所有内容。 如果您面对其他情况,将对您有所帮助。 在这里阅读。

Thanks for reading. Did this article help you in any way? If it did, I hope you consider sharing it. You might help someone out. Thank you!

谢谢阅读。 本文对您有任何帮助吗? 如果确实如此, 希望您考虑共享它 。 您可能会帮助某人。 谢谢!

This article was originally posted at my blog.Sign up for my newsletter if you want more articles to help you become a better frontend developer.

本文最初发布于 我BLO g.Sign为我的通讯 ,如果你想帮助更多的文章,你成为一个更好的前端开发者。

翻译自: https://www.freecodecamp.org/news/how-to-undo-changes-in-git-e1da7930afdb/

git 撤销更改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值