Git撤销操作
git commit --amend
撤销上一次提交并将暂存区的文件重新提交git checkout --filename
拉取暂存区的文件并将其替换工作区的文件- 注意与
git checkout branchname
区别,这个命令将用来切换分支
- 注意与
git reset HEAD --filename
拉取最近一次提交的版本库中的这个文件到暂存区,该操作不影响工作区
提交到版本库:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim index.htmlhello world onefine
this is a html file, version 1.0
:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -am 'version 1.0'[master 3058e7a] version 1.0 1 file changed, 1 insertion(+)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
假设还有细微的bug需要修改:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git log --oneline3058e7a (HEAD -> master) version 1.01d6647a modify style.csse922fd7 modify index.html13d63f2 add style.css039d4a6 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim index.htmlhello world onefine
version 1.0
:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
此时,如果要重新提交就不能直接使用原来的版本。
这时使用git commit -amend
撤销上一次提交并将暂存区的文件重新提交。 git commit -amend
git commit -amend
:撤销上一次提交并将暂存区的文件重新提交,形成一个新的版本。
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit --amendversion 1.0# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date: Sun Feb 10 08:28:37 2019 +0800## On branch master# Changes to be committed:# modified: index.html#:q[master f6af45a] version 1.0 Date: Sun Feb 10 08:28:37 2019 +0800 1 file changed, 1 insertion(+)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
再次使用git log
命令来查看一下:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git log --onelinef6af45a (HEAD -> master) version 1.01d6647a modify style.csse922fd7 modify index.html13d63f2 add style.css039d4a6 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
如果想仅修改版本的描述而不对内容做任何的改变,也可以使用此命令:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit --amendversion 1.0 , this is new!# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date: Sun Feb 10 08:28:37 2019 +0800## On branch master# Changes to be committed:# modified: index.html#:wq[master db28546] version 1.0 , this is new! Date: Sun Feb 10 08:28:37 2019 +0800 1 file changed, 1 insertion(+) ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git log --onelinedb28546 (HEAD -> master) version 1.0 , this is new!1d6647a modify style.csse922fd7 modify index.html13d63f2 add style.css039d4a6 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
接下来对误操作的撤销做介绍。
假设有小屁孩乱动了你的index.html
文档,就像这样: ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.htmldafa d daf hello wodafrldda onefdfadfine
vdersion 1.0
adfdfdasffdafONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
如果想回到之前的样子,可以怎么做呢?
git checkout
git checkout --filename
拉取暂存区的文件并将其替换工作区的文件。
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git checkout -- index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.htmlhello world onefine
version 1.0
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
或者使用git checkout -- .
来取消全部的错误操作。
git reset HEAD <file>
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim index.htmlhello world onefine
version 2.0
:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add .ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD ..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
如果想撤销现在暂存区的信息,恢复到以前暂存区的内容,可以使用git reset HEAD <file>
命令:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git reset HEAD index.htmlUnstaged changes after reset:M index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html hello world onefine
version 2.0
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
注意此时工作区的文件还是被修改之后的,只是已经回到了被修改状态,想要恢复直接使用git checkout -- .
就可以了。
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git checkout -- index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.htmlhello world onefine
version 1.0
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
HEAD
介绍:
头指针,一直指向最新一次的提交(Committed
状态),所有可以使用版本号来替代HEAD
,如:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git logcommit db28546bf7287af75f26f5367440b2f56bd2c4f0 (HEAD -> master)Author: onefine <188302531@qq.com>Date: Sun Feb 10 08:28:37 2019 +0800 version 1.0 , this is new!commit 1d6647a03772dc0f155040585117437b9714d1e4Author: onefine <188302531@qq.com>Date: Fri Feb 8 10:17:14 2019 +0800 modify style.css...省略...内容太多了ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
注意版本号可以不写全。
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git reset 1d6647a03772dc0f15 index.htmlUnstaged changes after reset:M index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD..." to unstage) modified: index.htmlChanges not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
这里出现两条提示
1、Changes to be committed,这是因为git reset
只是拉回到暂存区,git在比较暂存区跟版本库的时候发现两个文件是不一样的。 2、Changes not staged for commit,这是因为暂存区和工作区的文件不一样。 这时使用git checkout
将原暂存区文件拉回工作区:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git checkout -- index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html hello world onefine
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$
重新提交到版本库
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -m 'reset rewirte'[master f1e248e] reset rewirte 1 file changed, 1 deletion(-)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$