日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

撤銷修改

 灬木木的花灬 2015-09-16

自然,你是不會(huì)犯錯(cuò)的。不過現(xiàn)在是凌晨?jī)牲c(diǎn),你正在趕一份工作報(bào)告,你在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”可能會(huì)讓你丟掉這個(gè)月的獎(jiǎng)金!

既然錯(cuò)誤發(fā)現(xiàn)得很及時(shí),就可以很容易地糾正它。你可以刪掉最后一行,手動(dòng)把文件恢復(fù)到上一個(gè)版本的狀態(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會(huì)告訴你,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)。

總之,就是讓這個(gè)文件回到最近一次git commitgit add時(shí)的狀態(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命令中的--很重要,沒有--,就變成了“切換到另一個(gè)分支”的命令,我們?cè)诤竺娴姆种Ч芾碇袝?huì)再次遇到git checkout命令。

現(xiàn)在假定是凌晨3點(diǎn),你不但寫了一些胡話,還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)了這個(gè)問題。用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時(shí),表示最新的版本。

再用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)

整個(gè)世界終于清靜了!

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

小結(jié)

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

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

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

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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多