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 撤销更改

git 撤销更改_如何撤消Git中的更改相关推荐

  1. git 撤销刚才的rebase_撤消git rebase

    回答(15) 2 years ago 最简单的方法是找到分支的头部提交,因为它是在reflog中的rebase开始之前... git reflog 并将当前分支重置为它(关于在使用 --hard 选项 ...

  2. git 撤销刚才的rebase_撤消git pull –rebase

    嘿,我是git的新手,我需要撤消pull命令,有人可以帮忙吗?!? 所以我要做的是... git提交 git stash git pull --rebase git stash pop 这造成了一堆冲 ...

  3. git 撤销全部的commit_git 撤消操作所有

    概念 工作区:即自己当前分支所修改的代码,git add xx 之前的!不包括 git add xx 和 git commit xxx 之后的. 暂存区:已经 git add xxx 进去,且未 gi ...

  4. git 32位_完整的GIT笔记 快速上手小白教程

    GIT 是什么? Git 是目前世界上最先进的分布式版本控制系统.并且它是一个免费的.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 什么是版本控制系统 版本控制是一种系统,它跟踪一 ...

  5. git操作手册_基本的Git手册

    git操作手册 介绍 (Introduction) Hi! I am Sanjula, and in this guide I hope to teach you a little bit about ...

  6. idea查看git分支快捷键_开发常用Git/Linux/idea命令快捷键总结(持续更新)

    在开发过程中,会使用越来越多的命令,或快捷键,来帮助我们提高工作效率.本文记录了我在平时积累的常用命令,分享给大家. git命令 基本命令 set LESSCHARSET=utf-8 --idea T ...

  7. Git——撤销和删除操作【git restore / git rm 】

    前言:针对文件的撤销修改或者删除文件操作一般有以下几种情况: 1. 撤销文件修改 1.1 git restore – filename - 工作区文件改动未添加到暂存区   当修改后的文件还未放到暂存 ...

  8. git .git目录提交_压扁Git提交

    git .git目录提交 I use Git every day and I wrote a Git guide and a Git Cheat Sheet in the past. 我每天都使用Gi ...

  9. git 查看分支_系统掌握Git之—探索.git

    文章概述 上文中,我们聊了聊git的配置与一些基本概念,今天来看看.git文件夹里面有什么内容.通过阅读本文,你将获得下面的知识: .git的内部信息. git的分支操作. 图形化git工具. 新建分 ...

最新文章

  1. java 赋值,算术,一元操作符(翻译自Java Tutorials)
  2. 产品经理必了解的3个app技术框架
  3. OSChina 周日乱弹 ——科普逗逼行为准则
  4. 答与微博前端教主在吃饭时讨论到的一道微软面试题
  5. QT中播放音乐遇到的问题合集
  6. 截取字符串的函数_常用的Excel函数
  7. Linux浏览器libgtk-3,终于把WebKit(基于GTK)移植到ARM上
  8. 生物信息学的研究方向
  9. 【自动驾驶】Stanley(前轮反馈)实现轨迹跟踪
  10. 蒟蒻的第一篇博客(洛谷P1113)
  11. LOJ#6198. 谢特(SAM+01Trie树合并)
  12. 小型的代码管理仓库Gitea安装指南
  13. Qt边框border概述
  14. Linux / Windows系统中安装最新版 ElasticSearch (es)搜索引擎 超详细图文教程【一看就懂】
  15. pandas数据分析基础之时间序列
  16. java习题4_6水仙花数是指其个位、十位和百位三个数字的立方和等于这个三位数本身,求出所有的水仙花数。
  17. 前端加密方法sha256.js源码+用法(亲测可用)
  18. 【摄影教程】拍出酷照有什么摄影技…
  19. nginx--中文域名解析
  20. oracle数据库 uga,Oracle内存结构

热门文章

  1. 一名非计算机专业,却走着IT行业--自学之路
  2. mysql单表数据列_MySQL 之 数据操作及单表查询
  3. 压缩解压和tar命令
  4. EA公司CFO简森将于月底离职
  5. Uncaught SyntaxError: Unexpected token ' in JSON at position 1
  6. 自学-ES6篇-正则的扩展
  7. Three.js所有材质的属性及实例应用
  8. Error updating database. Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘experience_openne
  9. NSIS 脚本知识汇总
  10. js逆向-金沙赌场参数k值