7.2 Git Tools - Interactive Staging 交互式暫存

Interactive Staging 交互式暫存

Git 自帶一些腳本,方便你在命令行下進(jìn)行工作碉怔。本節(jié)會(huì)介紹幾個(gè)腳本命令烘贴,可以讓你一次只暫存文件的一部分(正常的暫存只能存整個(gè)文件)。比如有時(shí)你文件進(jìn)行了一些改動(dòng)撮胧,但希望將這些改動(dòng)做成多個(gè)提交桨踪,以便讓一次提交只完成一個(gè)任務(wù),這樣其他開(kāi)發(fā)者審核起來(lái)也比較容易芹啥。運(yùn)行 git add 帶選項(xiàng) -i 或者 --interactive锻离,Git 會(huì)進(jìn)入一個(gè)交互式終端模式:
Git comes with a couple of scripts that make some command-line tasks easier. Here, you’ll look at a few interactive commands that can help you easily craft(制作) your commits to include only certain combinations and parts of files. These tools are very helpful if you modify a bunch of files and then decide that you want those changes to be in several focused commits rather than one big messy commit. This way, you can make sure your commits are logically separate changesets and can be easily reviewed by the developers working with you. If you run git add with the -i or --interactive option, Git goes into an interactive shell mode, displaying something like this:

$ git add -i
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now>

Staging and Unstaging Files 暫存與撤銷(xiāo)暫存

如果在 What now> 提示符后鍵入 2 或 u铺峭,腳本就會(huì)問(wèn)你想要暫存哪個(gè)文件:

What now> 2
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Update>>

輸入相應(yīng)的數(shù)字就可以了:

Update>> 1,2
           staged     unstaged path
* 1:    unchanged        +0/-1 TODO
* 2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Update>>

前面的 * 意味文件被選中,再次按回車(chē)汽纠,選中的文件就會(huì)被暫存:
The * next to each file means the file is selected to be staged. If you press Enter after typing nothing at the Update>> prompt, Git takes anything selected and stages it for you:

Update>>
updated 2 paths

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now> 1
           staged     unstaged path
  1:        +0/-1      nothing TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb

如果這時(shí)想要撤銷(xiāo)對(duì) TODO 文件的暫存卫键,使用 3 或 r(撤消)選項(xiàng):

What now> 3
           staged     unstaged path
  1:        +0/-1      nothing TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Revert>> 1
           staged     unstaged path
* 1:        +0/-1      nothing TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Revert>> [enter]
reverted one path

驗(yàn)證一下,再次查看 Git 狀態(tài)虱朵,可以看到已經(jīng)取消暫存 TODO 文件:

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now> 1
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb

使用 6 或 d(區(qū)別)命令莉炉,類(lèi)似于 git diff --cached

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now> 6
           staged     unstaged path
  1:        +1/-1      nothing index.html
Review diff>> 1
diff --git a/index.html b/index.html
index 4d07108..4335f49 100644
--- a/index.html
+++ b/index.html
@@ -16,7 +16,7 @@ Date Finder

 <p id="out">...</p>

-<div id="footer">contact : support@github.com</div>
+<div id="footer">contact : email.support@github.com</div>

 <script type="text/javascript">

如上所述,用腳本后就不用每次都輸入完整的命令了卧秘。

Staging Patches 暫存補(bǔ)丁

你也可以只暫存文件的特定部分呢袱。例如對(duì) simplegit.rb 文件作了兩處修改,但只想暫存其中的一個(gè)翅敌。這就是 5 或 p(補(bǔ)缎吒!)的功能:(在Git的提示下,一個(gè)一個(gè)的處理)
It’s also possible for Git to stage certain parts of files and not the rest. For example, if you make two changes to your simplegit.rb file and want to stage one of them and not the other, doing so is very easy in Git. From the interactive prompt, type 5 or p (for patch). Git will ask you which files you would like to partially stage; then, for each section of the selected files, it will display hunks of the file diff and ask if you would like to stage them, one by one:

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index dd5ecc4..57399e0 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -22,7 +22,7 @@ class SimpleGit
   end

   def log(treeish = 'master')
-    command("git log -n 25 #{treeish}")
+    command("git log -n 30 #{treeish}")
   end

   def blame(path)
Stage this hunk [y,n,a,d,/,j,J,g,e,?]?

這時(shí)有很多選項(xiàng)蚯涮。 輸入 ? 顯示所有可以使用的命令列表:

Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk (塊)
n - do not stage this hunk
a - stage this and all the remaining hunks in the file 全部
d - do not stage this hunk nor any of the remaining hunks in the file 全部不
g - select a hunk to go to 前往某個(gè)塊
/ - search for a hunk matching the given regex (正則)
j - leave this hunk undecided, see next undecided hunk 不處理治专,到下一個(gè)未處理的塊
J - leave this hunk undecided, see next hunk 不處理,到下一個(gè)塊
k - leave this hunk undecided, see previous undecided hunk 不處理遭顶,到前一個(gè)未處理的塊
K - leave this hunk undecided, see previous hunk 不處理张峰,到前一個(gè)塊 
s - split the current hunk into smaller hunks 分割成更小的塊
e - manually edit the current hunk 手工編輯當(dāng)前塊
? - print help

如果你只暫存文件的一部分,狀態(tài)可能會(huì)是下面這樣:
Generally, you’ll type y or n if you want to stage each hunk, but staging all of them in certain files or skipping a hunk decision until later can be helpful too. If you stage one part of the file and leave another part unstaged, your status output will look like this:

What now> 1
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:        +1/-1      nothing index.html
  3:        +1/-1        +4/-0 lib/simplegit.rb

注意第三項(xiàng)棒旗,simplegit.rb 文件的狀態(tài):有些行被暫存喘批,有些行沒(méi)有被暫存。此時(shí)退出腳本并運(yùn)行 git commit 就可以提交已經(jīng)暫存的內(nèi)容了铣揉。
The status of the simplegit.rb file is interesting. It shows you that a couple of lines are staged and a couple are unstaged. You’ve partially staged this file. At this point, you can exit the interactive adding script and run git commit to commit the partially staged files.

在命令行中使用 git add-p--patch 選項(xiàng)饶深,也會(huì)啟動(dòng)同樣的腳本。
You also don’t need to be in interactive add mode to do the partial-file staging – you can start the same script by using git add -p or git add --patch on the command line.

Furthermore, you can use patch mode for partially resetting files with the reset --patch command, for checking out parts of files with the checkout --patch command and for stashing parts of files with the stash save --patch command. We’ll go into more details on each of these as we get to more advanced usages of these commands.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末逛拱,一起剝皮案震驚了整個(gè)濱河市敌厘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌朽合,老刑警劉巖俱两,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異曹步,居然都是意外死亡宪彩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)讲婚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)毯焕,“玉大人,你說(shuō)我怎么就攤上這事∧擅ǎ” “怎么了婆咸?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)芜辕。 經(jīng)常有香客問(wèn)我尚骄,道長(zhǎng),這世上最難降的妖魔是什么侵续? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任倔丈,我火速辦了婚禮,結(jié)果婚禮上状蜗,老公的妹妹穿的比我還像新娘需五。我一直安慰自己,他們只是感情好轧坎,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布宏邮。 她就那樣靜靜地躺著,像睡著了一般缸血。 火紅的嫁衣襯著肌膚如雪蜜氨。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,736評(píng)論 1 312
  • 那天捎泻,我揣著相機(jī)與錄音飒炎,去河邊找鬼。 笑死笆豁,一個(gè)胖子當(dāng)著我的面吹牛郎汪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播闯狱,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼煞赢,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了扩氢?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤爷辱,失蹤者是張志新(化名)和其女友劉穎录豺,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體饭弓,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡双饥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了弟断。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咏花。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出昏翰,到底是詐尸還是另有隱情苍匆,我是刑警寧澤,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布棚菊,位于F島的核電站浸踩,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏统求。R本人自食惡果不足惜检碗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望码邻。 院中可真熱鬧折剃,春花似錦、人聲如沸像屋。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)开睡。三九已至因苹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間篇恒,已是汗流浹背扶檐。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留胁艰,地道東北人款筑。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像腾么,于是被迫代替她去往敵國(guó)和親奈梳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容