设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

Git实现部分提交

2014-10-21 10:50| 发布者: joejoe0332| 查看: 9400| 评论: 0|原作者: wulathink, daxiang, warrior_by, Garfielt|来自: oschina

摘要: 每次当我正为一个特性努力时,总会发现我需要先对代码的另外一部分进行扩展。如果我不能这样做,我会在那个点创建一个分支。但是我没有。我会以两个特性类和真实特性的附加改变作为结尾。使用git分别提交两个版本的 ...

  每次当我正为一个特性努力时,总会发现我需要先对代码的另外一部分进行扩展。如果我不能这样做,我会在那个点创建一个分支。但是我没有。我会以两个特性类和真实特性的附加改变作为结尾。使用git分别提交两个版本的代码,同时保证每个代码都被编译很容易。


  我正在做我的新的大项目;命令行计算器。我已经完成了加法而且我对我已经完成的部分感到很高兴,我将要加入减法部分。在完成减法的途中我发现我需要对控制台输出格式类做一些修改。之前的部分我将“+”符号硬编码了,但是现在需要将它作为一个符号。我将它改为了符号而且得到了一个解决方案。


  

  我实现了一个git 状态显示功能,然而效果看起来很不好。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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上如何实现提交部分文件,请留言。


阶段希望改变

  第一步是阶段更改,我希望在第一次提交的的时候。 

1
2
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  文件现在已经准备好被提交。 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 的其他变化。

1
2
3
4
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文件。 

1
2
3
4
5
6
7
8
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]>


两次提交

  我的测试表明,未更新的阶段更改不影响编译编译和运行,第一部的更改已经可以提交了。

1
2
3
4
C:\git\spikes\gitpartial [master +0 ~1 -0]> git commit -m "Improved ConsoleFormatter."
[master d69baee] Improved ConsoleFormatter.
 file changed, 0 insertions(+), 0 deletions(-)
C:\git\spikes\gitpartial [master]>


  是时候将其他的变更改回来了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 !]>


  然后提交它们。

1
2
3
4
5
6
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]>


结果

  结果是一个漂亮的提交历史,独立的提交独立的更改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
 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]>

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


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部