小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

撤銷修改

 灬木木的花灬 2015-09-16

自然,你是不會犯錯的。不過現(xiàn)在是凌晨兩點,你正在趕一份工作報告,你在readme.txt中添加了一行:

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.

在你準(zhǔn)備提交前,一杯咖啡起了作用,你猛然發(fā)現(xiàn)了“stupid boss”可能會讓你丟掉這個月的獎金!

既然錯誤發(fā)現(xiàn)得很及時,就可以很容易地糾正它。你可以刪掉最后一行,手動把文件恢復(fù)到上一個版本的狀態(tài)。如果用git status查看一下:

$ 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:   readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

你可以發(fā)現(xiàn),Git會告訴你,git checkout -- file可以丟棄工作區(qū)的修改:

$ git checkout -- readme.txt

命令git checkout -- readme.txt意思就是,把readme.txt文件在工作區(qū)的修改全部撤銷,這里有兩種情況:

一種是readme.txt自修改后還沒有被放到暫存區(qū),現(xiàn)在,撤銷修改就回到和版本庫一模一樣的狀態(tài);

一種是readme.txt已經(jīng)添加到暫存區(qū)后,又作了修改,現(xiàn)在,撤銷修改就回到添加到暫存區(qū)后的狀態(tài)。

總之,就是讓這個文件回到最近一次git commitgit add時的狀態(tài)。

現(xiàn)在,看看readme.txt的文件內(nèi)容:

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

文件內(nèi)容果然復(fù)原了。

git checkout -- file命令中的--很重要,沒有--,就變成了“切換到另一個分支”的命令,我們在后面的分支管理中會再次遇到git checkout命令。

現(xiàn)在假定是凌晨3點,你不但寫了一些胡話,還git add到暫存區(qū)了:

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.

$ git add readme.txt

慶幸的是,在commit之前,你發(fā)現(xiàn)了這個問題。用git status查看一下,修改只是添加到了暫存區(qū),還沒有提交:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   readme.txt
#

Git同樣告訴我們,用命令git reset HEAD file可以把暫存區(qū)的修改撤銷掉(unstage),重新放回工作區(qū):

$ git reset HEAD readme.txt
Unstaged changes after reset:
M       readme.txt

git reset命令既可以回退版本,也可以把暫存區(qū)的修改回退到工作區(qū)。當(dāng)我們用HEAD時,表示最新的版本。

再用git status查看一下,現(xiàn)在暫存區(qū)是干凈的,工作區(qū)有修改:

$ 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:   readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

還記得如何丟棄工作區(qū)的修改嗎?

$ git checkout -- readme.txt

$ git status
# On branch master
nothing to commit (working directory clean)

整個世界終于清靜了!

現(xiàn)在,假設(shè)你不但改錯了東西,還從暫存區(qū)提交到了版本庫,怎么辦呢?還記得版本回退一節(jié)嗎?可以回退到上一個版本。不過,這是有條件的,就是你還沒有把自己的本地版本庫推送到遠(yuǎn)程。還記得Git是分布式版本控制系統(tǒng)嗎?我們后面會講到遠(yuǎn)程版本庫,一旦你把“stupid boss”提交推送到遠(yuǎn)程版本庫,你就真的慘了……

小結(jié)

又到了小結(jié)時間。

場景1:當(dāng)你改亂了工作區(qū)某個文件的內(nèi)容,想直接丟棄工作區(qū)的修改時,用命令git checkout -- file。

場景2:當(dāng)你不但改亂了工作區(qū)某個文件的內(nèi)容,還添加到了暫存區(qū)時,想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了場景1,第二步按場景1操作。

場景3:已經(jīng)提交了不合適的修改到版本庫時,想要撤銷本次提交,參考版本回退一節(jié),不過前提是沒有推送到遠(yuǎn)程庫。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多