Git回退

综合技术 2018-12-06 阅读原文

一、checkout&&reset

git checkout 指令是用来还原一个代码仓库中的文件的。

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git status
On branch master
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:   test

no changes added to commit (use "git add" and/or "git commit -a")

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git checkout test

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean

在修改文件之后,执行git add指令之前,如果执行checkout指令,则会抛弃当前本地的所有修改,恢复到上次最后的提交版本。

如果修改文件并执行git add指令后继续修改文件,此时再执行checkout指令,查看代码仓库状态。

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git status
On branch master
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:   test

no changes added to commit (use "git add" and/or "git commit -a")

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git add test

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git checkout test

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test

git checkout <file> 指令其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除。

git reset HEAD <file> 指令将一个文件移出暂存区。

二、回退版本

在Git中,用HEAD表示当前版本,那么上一个版本就是 HEAD^ ,上上一个版本就是 HEAD^^ ,如果往上100个版本可以写成HEAD~100。

回退到上个版本

$ git reset --hard HEAD^
HEAD is now at 4269b25 添加README

也可以写commit id来表示对应的版本。

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ touch test2

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git add
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git add test2

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git commit -m "add test2"
[master c896e52] add test2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test2

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git lg
* c896e52 (HEAD -> master)add test2(3 seconds ago)<huangxiaolei>--abbrev-commit
* 4269b25添加README(76 minutes ago)<huangxiaolei>--abbrev-commit

huangxiaolei@huangxiaolei MINGW64 /f/GitTest (master)
$ git reset --hard 4269b25
HEAD is now at 4269b25 添加README

学海无涯苦作舟

Android成长录.jpg

简书

责编内容by:简书阅读原文】。感谢您的支持!

您可能感兴趣的

应该如何做好持续集成和部署? 应该如何做好持续集成和部署? 前言 持续集成和部署是每一个互联网开发团队都必须要面对的问题,由于业务和技术团队快速增长,技术积累较弱,所以一个高效的,可持续的运维规范尤为重要。 高效可持续的运维环境需要合理的规范作为支撑:...
【手把手撸一个脚手架】第三步, 获取 github 项目信息... 脚手架, 作为一个工具, 主要是用来搬运我们已经准备好的项目模板(webpack 配置, koa 项目雏形等等), 这一步我们就来创建用于搬运的项目模板. 并通过接口获取相关信息 github Api 文档 ps: 这一步基于 gi...
Push changes to github without pulling I have a stupid situation. I have developed a project that was been developed by another developers. I have sent my sour...
书单丨成为全栈工程师的5种硬实力 全栈工程师的作用和价值越来越被重视 但软件开发所涉及的技术体系过于庞大 怎样才能成为一名全栈工程呢? 本文列出了5个全栈工程师成长路上的关键开发技能 掌握这些技能,成为全栈工程师不再遥远 NO.1 掌...
Learning Git With GitKraken: How to Squash Commits We created a series of Git tutorial videos that were really well received by developers wanting to learn Git. Howeve...