Git 實現部分提交
每次當我正為一個特性努力時,總會發現我需要先對代碼的另外一部分進行擴展。如果我不能這樣做,我會在那個點創建一個分支。但是我沒有。我會以兩個特性類和真實特性的附加改變作為結尾。使用git分別提交兩個版本的代碼,同時保證每個代碼都被編譯很容易。
我正在做我的新的大項目;命令行計算器。我已經完成了加法而且我對我已經完成的部分感到很高興,我將要加入減法部分。在完成減法的途中我發現我需要對控制臺輸出格式類做一些修改。之前的部分我將“+”符號硬編碼了,但是現在需要將它作為一個符號。我將它改為了符號而且得到了一個解決方案。
我實現了一個git 狀態顯示功能,然而效果看起來很不好。
C:\git\spikes\gitpartial [master +1 ~2 -0 !]> 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: ConsoleFormatter.cs modified: Program.cs Untracked files: (use "git add <file>..." to include in what will be committed) Subtraction.cs no changes added to commit (use "git add" and/or "git commit -a") C:\git\spikes\gitpartial [master +1 ~2 -0 !]>
我已經使用了更新后的ConsoleFormatter.cs、Program.cs 、 Subtraction.cs文件。 第一個文件包含了更新后的控制臺格式化特性,它獨立于新增功能。 我想單獨提交ConsoleFormmatter.cs文件。不只是提交。 通過隱藏視圖中的其它文件,我還想編譯和測試我要提交的代碼。 在 git中,使用幾行命令便可以完成這些工作。 但是在版本控制中,我從來沒有想出如何以一種足夠簡單的方式做到這些。 在 svn上,我通常是一次提交很多文件。 如果有人知道在 svn上如何實現提交部分文件,請留言。
階段希望改變
第一步是階段更改,我希望在第一次提交的的時候。
C:\git\spikes\gitpartial [master +1 ~2 -0 !]> git add ConsoleFormatter.cs C:\git\spikes\gitpartial [master +0 ~1 -0 | +1 ~1 -0 !]>
做一個 git status 來顯示 ConsoleFormatter.cs 文件現在已經準備好被提交。
C:\git\spikes\gitpartial [master +0 ~1 -0 | +1 ~1 -0 !]> git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: ConsoleFormatter.cs 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: Program.cs Untracked files: (use "git add <file>..." to include in what will be committed) Subtraction.cs C:\git\spikes\gitpartial [master +0 ~1 -0 | +1 ~1 -0 !]>
隱藏其他變化
現在這個時候去隱藏視圖與 git stash 的其他變化。
C:\git\spikes\gitpartial [master +0 ~1 -0 | +1 ~1 -0 !]> git stash -u -k Saved working directory and index state WIP on master: 0b093c1 Implemented addition. HEAD is now at 0b093c1 Implemented addition. C:\git\spikes\gitpartial [master +0 ~1 -0]>
-k 開關告訴倉庫保持文件的完整。 -u 開關告訴倉庫包括無路徑的文件(那些新的和未添加到git的)。
再次做一個 git status只顯示了 ConsoleFormatter.cs 文件我想提交的第一步。這不僅僅是一個git的狀態。這是實際的工作目錄的內容。在這一點上我可以編譯和測試我要檢查的代碼,發現它確實沒有更新Program.cs文件。
C:\git\spikes\gitpartial [master +0 ~1 -0]> git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: ConsoleFormatter.cs C:\git\spikes\gitpartial [master +0 ~1 -0]>
兩次提交
我的測試表明,未更新的階段更改不影響編譯編譯和運行,第一部的更改已經可以提交了。
C:\git\spikes\gitpartial [master +0 ~1 -0]> git commit -m "Improved ConsoleFormatter." [master d69baee] Improved ConsoleFormatter. 1 file changed, 0 insertions(+), 0 deletions(-) C:\git\spikes\gitpartial [master]>
是時候將其他的變更改回來了。
C:\git\spikes\gitpartial [master]> git stash pop 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: Program.cs Untracked files: (use "git add <file>..." to include in what will be committed) Subtraction.cs no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (1bd32daf6787280201ccdacb90bc76e1ca45e7ce) C:\git\spikes\gitpartial [master +1 ~1 -0 !]>
然后提交它們。
C:\git\spikes\gitpartial [master +1 ~1 -0 !]> git add . C:\git\spikes\gitpartial [master +1 ~1 -0]> git commit -m "Implemented Subtraction." [master 46f2e66] Implemented Subtraction. 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Subtraction.cs C:\git\spikes\gitpartial [master]>
結果
結果是一個漂亮的提交歷史,獨立的提交獨立的更改。
C:\git\spikes\gitpartial [master]> git log --stat commit 46f2e66f69dac06ce111f3ab13c6ec68d9b9fae2 (HEAD, master) Author: Anders Abel <anders@abel.nu> Date: Thu Oct 16 22:18:02 2014 +0200 Implemented Subtraction. Program.cs | Bin 12 -> 22 bytes Subtraction.cs | Bin 0 -> 12 bytes 2 files changed, 0 insertions(+), 0 deletions(-) commit d69baeeb4b753779bdc3706730e9162244b6e355 Author: Anders Abel <anders@abel.nu> Date: Thu Oct 16 22:17:47 2014 +0200 Improved ConsoleFormatter. ConsoleFormatter.cs | Bin 12 -> 22 bytes 1 file changed, 0 insertions(+), 0 deletions(-) commit 0b093c1dcb7a536a40648e2977fb73edef9200d5 Author: Anders Abel <anders@abel.nu> Date: Thu Oct 16 22:03:53 2014 +0200 Implemented addition. Addition.cs | Bin 0 -> 12 bytes ConsoleFormatter.cs | Bin 0 -> 12 bytes Program.cs | Bin 0 -> 12 bytes 3 files changed, 0 insertions(+), 0 deletions(-) C:\git\spikes\gitpartial [master]>
(而且更改的字節數是對的。我之前被騙了,它們不是實際文件中的代碼。)