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]>

(而且更改的字节数是对的。我之前被骗了,它们不是实际文件中的代码。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值