文档章节

When to Use Git Reset, Git Revert & Git Checkout

Kolosek
 Kolosek
发布于 2018/05/21 17:51
字数 954
阅读 2
Git">收藏 0

Git Reset, Revert and Checkout

Git toolbox provides multiple unique tools for fixing up mistakes during your development. Commands such as git resetgit checkout, and git revert allow you to undo erroneous changes in your repository.

Because they perform similar operations, it is very easy to mix them up. There are a few guidelines and rules for when each command should and should not be used. Let's take a look!

Be careful! You can't always redo after an undo. This is one of the few areas in Git where you may lose some work if you do it wrong.

Undoing with Git Commands

We will start off by clarifying the main differences between these three commands.

Checkout:

  • Use this to move the HEAD pointer to a specific commit or switch between branches.
  • It rollbacks any content changes to those of the specific commit.
  • This will not make changes to the commit history.
  • Has potential to overwrite files in the working directory.

Revert:

  • Rollback changes you have committed.
  • Creates a new commit from a specified commit by inverting it. Hence, adds a new commit history to the project, but it does not modify the existing one.
  • Has the potential to overwrite files in the working directory.

Reset:

  • Use this to return the entire working tree to the last committed state. This will discard commits in a private branch or throw away uncommitted changes!
  • Changes which commit a branch HEAD is currently pointing at. It alters the existing commit history.
  • Can be used to unstage a file.

Every command lets you undo some kind of change in your repository, only checkout and reset can be used to manipulate either commits or individual files.

Using the Commands

There are many different ways you can undo your changes, it all depends on the current scenario. Selecting an appropriate method depends on whether or not you have committed the change by mistake, and if you have committed it, whether you have shared it or not.

Undo Public Changes

Scenario: Image that you did git push in hotfix branch for commits you didn't want to make yet.

Solution: The safest way to fix this is by reverting your changes since it doesn't re-write the commit history.

git checkout hotfix
git revert HEAD~1

Result: You have successfully undone committed changes! Everything that was changed in the old commit will be reverted with this new commit. Git forces you to commit or stash any changes in the working directory that will be lost during the checkout.

git-revert

You can think of git revert as a tool for undoing committed changes, while git reset HEAD is for undoing uncommitted changes.

Undo Local Changes

Scenario: You started working on a feature, but you didn't like the end result. These changes haven't been shared with anyone else.

Solution: You want to undo everything in that files to the previous state, just the way it looked in the last commit.

git checkout file_name.rb

Result: File file_name.rb has been reverted to a state previously known to Git. Note that this removes all of the subsequent changes to the file!

You can use git checkout branch_name to switch between branches. Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation.

Undo Private Changes

Scenario: You've made some commits locally in the hotfix branch but everything is terrible! You want to remove the last two commits from the current branch.

Solution: Reset the hotfix branch backward by two commits as if those commits never happened.

git checkout hotfix
git reset HEAD~2

Result: Our git repository has been rewinded all the way back to the specified commit. Those left out commits are now orphaned and will be removed the next time Git performs a garbage collection. For now, then their contents are still on disk.

git-reset

You can tell Git what to do with your index (set of files that will become the next commit) and working directory when performing git reset by using one of the parameters:

  • --soft: Tells Git to reset HEAD to another commit, so index and the working directory will not be altered in any way. All of the files changed between the original HEAD and the commit will be staged.
  • --mixed: Just like the soft, this will reset HEAD to another commit. It will also reset the index to match it while working directory will not be touched. All the changes will stay in the working directory and appear as modified, but not staged.

The main difference between --mixed and --soft is whether or not your index is also modified. Check more on git-reset-guide.

  • --hard: This resets everything - it resets HEAD back to another commit, resets the index to match it, and resets the working directory to match it as well.

Tips and Tricks

We will cover two additional things that can come in handy during your Git adventures.

Fix the Previous Commit Message

Scenario: Everyone makes typo mistakes when writing commits and this is completely fine! It can be easily fixed before you do a git push.

Solution: Just run git commit --amend or git commit --amend -m 'The new message'. This will update and replace the most recent commit with a new commit.

Redo After Undo

Scenario: You have done a git reset --hard for some unwanted changes, but then you realized that you actually needed them.

Solution: git reflog comes to your rescue! It is an amazing command for recovering project history and it can recover almost anything.

Hope this three tools will help you whenever you need to undo your recent changes.

This post is originally published on Kolosek Blog.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
Git 技巧:恢复到前一次提交 (图示分析)

对这篇文章的试验:http://www.oschina.net/translate/git-tips-revert-with-new-commit, FYI prerequisites: git init test cd test touch first.txt git add . git commit -m "intial......

cyper
2014/03/09
1K
8
详解Git工作区、暂存区、历史记录区以及git reset、git revert、git checkout等撤销命令的区别

一、可以将git简单的分为三个区域 1、工作区(working directory) 2、暂缓区(stage index) 3、历史记录区(history) 如图: 其中git add files 把当前工作目录中的文件放入暂存区域 这其...

天王盖地虎626
02/09
113
0
git 撤销操作 git reset与git revert的使用

先简单说一下,在提交到远程库之前我们使用git reset 命令完全可以满足我们 撤销操作的需求,如果操作已经提交到远程库,那只好使用 git revert 来提交一个新的撤销操作 撤销 需要撤销的那次...

悟_空
2017/12/15
0
0
idea git 高级操作(checkout、reset、revert)

---layout: blogistop: truetitle: "idea git 高级操作(checkout、reset、revert)"date: 2018-09-18category: 版本控制tags: 版本控制 --- git reset 切换到指定分支,这里以master为例,下......

开心的哈士奇
01/15
44
0
Git的基本概念和用法

在日常使用GIT过程中,经常会出错,比如无意间丢失了未提交的数据,回退版本时丢失了工作目录,等等。经过思考发现,所有这些错误都是因为对GIT中一些基本的概念模糊而导致,因为对一些基本概...

jims
2016/10/01
55
0

没有更多内容

加载失败,请刷新页面

加载更多

Mybatis Plus删除

/** @author beth @data 2019-10-17 00:30 */ @RunWith(SpringRunner.class) @SpringBootTest public class DeleteTest { @Autowired private UserInfoMapper userInfoMapper; /** 根据id删除......

一个yuanbeth
今天
5
0
总结

一、设计模式 简单工厂:一个简单而且比较杂的工厂,可以创建任何对象给你 复杂工厂:先创建一种基础类型的工厂接口,然后各自集成实现这个接口,但是每个工厂都是这个基础类的扩展分类,spr...

BobwithB
今天
5
0
java内存模型

前言 Java作为一种面向对象的,跨平台语言,其对象、内存等一直是比较难的知识点。而且很多概念的名称看起来又那么相似,很多人会傻傻分不清楚。比如本文我们要讨论的JVM内存结构、Java内存模...

ls_cherish
今天
4
0
友元函数强制转换

友元函数强制转换 p522

天王盖地虎626
昨天
5
0
js中实现页面跳转(返回前一页、后一页)

本文转载于:专业的前端网站➸js中实现页面跳转(返回前一页、后一页) 一:JS 重载页面,本地刷新,返回上一页 复制代码代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a h...

前端老手
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部