2019独角兽企业重金招聘Python工程师标准>>>

git ls-files | xargs cat | wc -l

2.4 Git Basics - Undoing Things

Undoing Things

At any stage, you may want to undo something. Here, we’ll review a few basic tools for undoing changes that you’ve made. Be careful, because you can’t always revert some of these undos. This is one of the few areas in Git where you may lose some work if you do it wrong.

Changing Your Last Commit

One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to try that commit again, you can run commit with the--amend option:

$ git commit --amend

This command takes your staging area and uses it for the commit. If you’ve made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same and all you’ll change is your commit message.

The same commit-message editor fires up, but it already contains the message of your previous commit. You can edit the message the same as always, but it overwrites your previous commit.

As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

After these three commands, you end up with a single commit — the second commit replaces the results of the first.

Unstaging a Staged File

The next two sections demonstrate how to wrangle your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both. How can you unstage one of the two? The git status command reminds you:

$ git add .
$ git status
On branch master
Changes to be committed:(use "git reset HEAD <file>..." to unstage)modified:   README.txtmodified:   benchmarks.rb

Right below the “Changes to be committed” text, it says "use git reset HEAD <file>... to unstage". So, let’s use that advice to unstage the benchmarks.rb file:

$ git reset HEAD benchmarks.rb
Unstaged changes after reset:
M       benchmarks.rb
$ git status
On branch master
Changes to be committed:(use "git reset HEAD <file>..." to unstage)modified:   README.txtChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified:   benchmarks.rb

The command is a bit strange, but it works. The benchmarks.rb file is modified but once again unstaged.

Unmodifying a Modified File

What if you realize that you don’t want to keep your changes to the benchmarks.rb file? How can you easily unmodify it — revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, git status tells you how to do that, too. In the last example output, the unstaged area looks like this:

Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified:   benchmarks.rb

It tells you pretty explicitly how to discard the changes you’ve made (at least, the newer versions of Git, 1.6.1 and later, do this — if you have an older version, we highly recommend upgrading it to get some of these nicer usability features). Let’s do what it says:

$ git checkout -- benchmarks.rb
$ git status
On branch master
Changes to be committed:(use "git reset HEAD <file>..." to unstage)modified:   README.txt

You can see that the changes have been reverted. You should also realize that this is a dangerous command: any changes you made to that file are gone — you just copied another file over it. Don’t ever use this command unless you absolutely know that you don’t want the file. If you just need to get it out of the way, we’ll go over stashing and branching in the next chapter; these are generally better ways to go.

Remember, anything that is committed in Git can almost always be recovered. Even commits that were on branches that were deleted or commits that were overwritten with an --amend commit can be recovered (see Chapter 9 for data recovery). However, anything you lose that was never committed is likely never to be seen again.

Reference: http://git-scm.com/book/en/Git-Basics-Undoing-Things

转载于:https://my.oschina.net/u/579199/blog/286522

Git Cheat sheet相关推荐

  1. Git Cheat Sheet 中文版

    Git Cheat Sheet 中文版 本文内容转载GitHub repo Git-Cheat-Sheet 索引 配置 配置文件 创建 本地修改 搜索 提交历史 分支与标签 更新与发布 合并与重置 撤 ...

  2. Git Cheat Sheet——Git的常用命令和最佳做法

    国外网友制作的Git Cheat Sheet,已经翻译为中文,描述了常用的Git命令和使用git的最佳做法 我对翻译后的文案加上序号和格式的调整 建议记下它们,如果你使用git 一.常见命令 1. 创 ...

  3. 一份 Git cheat sheet 送给您

    0. 前言 大家好,我是多选参数的程序锅,一个正在 neng 操作系统.学数据结构和算法以及 Java 的硬核菜鸡.到今天为止,关于 Git 的大坑算是给填上了.但是 Git 这个系列并不会结束,程序 ...

  4. 139.00.007 Git学习-Cheat Sheet

    @(139 - Environment Settings | 环境配置) Git虽然极其强大,命令繁多,但常用的就那么十来个,掌握好这十几个常用命令,你已经可以得心应手地使用Git了. 友情附赠国外网 ...

  5. 容器编排技术 -- kubectl Cheat Sheet

    容器编排技术 -- kubectl Cheat Sheet 1 Kubectl 自动补全 2 Kubectl 上下文和配置 3 创建对象 4 显示和查找资源 5 更新资源 6 修补资源 7 编辑资源 ...

  6. mysql 递归_「MySQL」 - SQL Cheat Sheet - 未完成

    近几个月的心情真是安排的妥妥的,呈现W状.多的不说了,这里对SQL的测试进行简单梳理,制作一份SQL Cheat Sheet. 0x01.数据库基本架构 Clinet层 Server层 连接器 网络连 ...

  7. ubuntu cheat sheet 目录结构

     Ubuntu Cheat Sheet Ubuntu系统目录结构 以下为Ubuntu目录的主要目录结构,您稍微了解它们都包含了哪些文件就可以了,不需要记忆. / 根目录 │ ├boot/ 启动文件.所 ...

  8. Emmet Cheat Sheet(Sublime编辑)

    快捷创建html标签 官网的Emmet Cheat Sheet :http://docs.emmet.io/cheat-sheet/ https://files.cnblogs.com/files/t ...

  9. Nmap Cheat Sheet Part 1

    译者:未知 原文:Nmap Cheat Sheet: From Discovery to Exploits – Part 1: Introduction to Nmap 在侦查期间,扫描一直是信息收集 ...

  10. XSS Cheat Sheet

    XSS Cheat Sheet XSS 101 <h1>Hello,<script>alert(1)</script>!</h1> 1. With &l ...

最新文章

  1. 两条线段相切弧_两条直线间的圆弧连接
  2. 近一半企业曾遭遇云计算安全问题
  3. python是什么公司开发的软件-软件开发|什么是行为驱动的 Python?
  4. Error response from daemon: manifest not found.
  5. break与continue的区别【图解】(简洁明了)
  6. 转-聚合查询变慢-详解Elasticsearch的Global Ordinals与High Cardinality
  7. linux主机基本情况,查看linux主机系统基本信息.pdf
  8. 计算机二级考试答题无法启动ppt,计算机二级考试中操作题常见问题之[演示文稿]...
  9. 链接聚合是将一组物理接口_如何增加带宽,提升网络可靠性?
  10. Android Studio 使用教程(5)---打包apk
  11. SSO单点登录学习总结(3)—— 基于CAS实现单点登录实例
  12. NET分页实现及代码
  13. In 2018, the release of Huawei‘s p20 pro
  14. 【leetcode刷题】[简单]427. 建立四叉树(construct quad tree)-java
  15. Ruby语言入门之Hello world
  16. 怎么写化学反应方程式?
  17. 高德地图驾车导航使用
  18. iOS 环信移动客服接入
  19. 小红书笔记下沉的方法和技巧
  20. 【数据可视化应用】绘制QQ图(附Python和R语言代码)

热门文章

  1. Scikit-learn:聚类clustering
  2. 从bagging到dropout(deep learning笔记Ian)
  3. 怎样调整input框背景颜色_还在用百度搜索PPT背景图?7个高大上的图片网站,个个都是高清免费无版权!...
  4. Kotlin — 协程简介与使用
  5. Amlogic_Android7.1 HDMI显示流程源码分析
  6. python3 自动识图
  7. P2900 [USACO08MAR]土地征用Land Acquisition
  8. git如何查看某个人提交的日志。
  9. bzoj3531: [Sdoi2014]旅行 (树链剖分 动态开点线段树)
  10. 《大道至简》第一章读后感(java伪代码)