下面以现实场景做为情境。html
基础知识,理解git中的几个区域
git
修改本地工做目录中的readme.md,添加文字"第一次修改"缓存
而后查看下状态安全
➜ experimentation git:(master) ✗ git status On branch master Your branch is up-to-date with 'origin/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: README.md no changes added to commit (use "git add" and/or "git commit -a")
进行Add操做,并查看状态bash
➜ experimentation git:(master) ✗ git add README.md ➜ experimentation git:(master) ✗ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README.md ➜ experimentation git:(master) ✗
这时候,变更进入了缓存区(Index)spa
可是咱们忽然发现咱们改动错了,其实我是想改动experimentation.txt
文件。code
方法一:咱们固然能够再次修改experimentation和readme文件(删除readme中的修改,给experimentation中添加“第一次修改”),而后再Add
htm
方法二:咱们想的多是撤回以前的add操做,而后给experimentation中添加“第一次修改”,而后再次Add
,那这样的话,咱们如何操做呢?blog
撤销某个add文件:git reset HEAD <filename>
ip
撤销所有add文件:git reset HEAD .
➜ experimentation git:(master) ✗ git reset HEAD README.md Unstaged changes after reset: M README.md ➜ experimentation git:(master) ✗ git status On branch master Your branch is up-to-date with 'origin/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: README.md no changes added to commit (use "git add" and/or "git commit -a")
将本地代码多commit几回,再来看下咱们的提交log
git revert <commitId>
撤回到指定的版本,包括文件和状态。
git reset <commitId>
➜ experimentation git:(master) git reset 2a6c701 Unstaged changes after reset: M experimentation.txt ➜ experimentation git:(master) ✗ git status On branch master Your branch and 'origin/master' have diverged, and have 4 and 6 different commits each, respectively. (use "git pull" to merge the remote branch into yours) 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: experimentation.txt no changes added to commit (use "git add" and/or "git commit -a")
回退至指定的版本,区别以下