如何暂存新文件的部分内容?

时间:2015-05-25 23:38:53

标签: git git-add

我在这里尝试了这个建议:

  

How to stage only part of a new file with git?

git add -N new_file
git add -i

但是我无法让它工作,因为交互模式在没有s选项的情况下显示整个文件,这使我能够将文件拆分成更小的部分,从而暂存文件的一部分:

~/git_practice$ git init my_project
Initialized empty Git repository in /Users/7stud/git_practice/.git/

~/git_practice$ cd my_project

~/git_practice/my_project$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

~/git_practice/my_project$ echo This is the README file. > README.txt
~/git_practice/my_project$ ls
README.txt

~/git_practice/my_project$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    README.txt

nothing added to commit but untracked files present (use "git add" to track)

~/git_practice/my_project$ git add README.txt 
~/git_practice/my_project$ git commit -m "Add README file."
[master (root-commit) e815ed7] Add README file.
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

~/git_practice/my_project$ git status
On branch master
nothing to commit, working directory clean

~/git_practice/my_project$ git checkout -b new_feature
Switched to a new branch 'new_feature'

~/git_practice/my_project$ m new_feature.rb

(m是我为mvim命令设置的别名,它启动了macvim文本编辑器。)

这是我在new_feature.rb中输入的代码:

def addition(x, y)
  x+y
end

def substraction(x,y)
  x-y
end

#Uh oh!  I got carried away and created two new features.  
#I want to split addition/subtraction into two commits.

返回命令行:

~/git_practice/my_project$ git add -p new_feature.rb 
No changes.  

那不起作用。相反,我必须这样做:

 ~/git_practice/my_project$ git add -N new_feature.rb

据我所知,这实际上是将一个空白版本的new_feature.rb添加到临时区域;那么你可以用new_feature.rb中的部分代码修补那个空白文件:

~/git_practice/my_project$ git add -i new_feature.rb 

           staged     unstaged path
  1:        +0/-0       +10/-0 new_feature.rb

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

-i代表互动。您要求git以交互方式提示您有关如何将文件添加到临时区域的问题。作为回应,git显示一个包含各种选项的菜单。您可以输入选择前面的数字或选择的第一个字母(例如,p补丁):

What now> p
           staged     unstaged path
  1:        +0/-0       +10/-0 new_feature.rb

可能会列出多个文件,因此您必须选择要修补的文件名前面的数字(staged下的数字表示该文件的暂存版本为空,unstaged下的数字表示未分阶段的文件与分阶段版本相比有10个新行:

Patch update>> 1
           staged     unstaged path
* 1:        +0/-0       +10/-0 new_feature.rb

星号表示您选择要修补的文件。要实际开始修补,您必须在下一个提示符下点击Return:

Patch update>> <Hit Return>

diff --git a/new_feature.rb b/new_feature.rb
index e69de29..b44829e 100644
--- a/new_feature.rb
+++ b/new_feature.rb
@@ -0,0 +1,10 @@
+def addition(x, y)
+  x+y
+end
+
+def substraction(x,y)
+  x-y
+end
+
+#Uh oh!  I got carried away and created two new features.  
+#I want to separate addition/subtraction into two commits.

Stage this hunk [y,n,q,a,d,/,e,?]? 

根据答案:

我需要选择e选项,其中包含以下文字:

# Manual hunk edit mode -- see bottom for a quick guide                       
@@ -0,0 +1,10 @@                                                              
+def addition(x, y)                                                           
+  x+y                                                                        
+end                                                                          
+                                                                             
+def substraction(x,y)                                                        
+  x-y                                                                        
+end                                                                          
+                                                                             
+#Uh oh!  I got carried away and created two new features.                    
+#I want to separate addition/subtraction into two commits.                   
# ---                                                                         
# To remove '-' lines, make them ' ' lines (context).                         
# To remove '+' lines, delete them.                                           
# Lines starting with # will be removed.                                      
#                                                                             
# If the patch applies cleanly, the edited hunk will immediately be           
# marked for staging. If it does not apply cleanly, you will be given         
# an opportunity to edit again. If all lines of the hunk are removed,         
# then the edit is aborted and the hunk is left unchanged.                    
~                                                                               
~                                                                               
"~/git_practice/my_project/.git/addp-hunk-edit.diff" 21L, 671C

编辑该文件不会更改原始文件(new_feature.rb)。编辑该文件并保存该文件的结果将是获得暂存的文件部分,例如这是我的编辑:

# Manual hunk edit mode -- see bottom for a quick guide                       
@@ -0,0 +1,10 @@                                                              
+def addition(x, y)                                                           
+  x+y                                                                        
+end                                                                          


# ---                                                                         
# To remove '-' lines, make them ' ' lines (context).                         
# To remove '+' lines, delete them.                                           
# Lines starting with # will be removed.                                      
#                                                                             
# If the patch applies cleanly, the edited hunk will immediately be           
# marked for staging. If it does not apply cleanly, you will be given         
# an opportunity to edit again. If all lines of the hunk are removed,         
# then the edit is aborted and the hunk is left unchanged.                    
~                                                                               
~                                                                               
"~/git_practice/my_project/.git/addp-hunk-edit.diff" 21L, 671C

然后保存并退出文本编辑器后:

git commit -m "Add addition() method."

此时,您可以执行diff来比较git提交的内容v。原始文件的样子:

~/git_practice/my_project$ git diff new_feature.rb 

diff --git a/new_feature.rb b/new_feature.rb
index 6579fef..b44829e 100644
--- a/new_feature.rb
+++ b/new_feature.rb
@@ -1,3 +1,10 @@
 def addition(x, y)
   x+y
 end
+
+def substraction(x,y)
+  x-y
+end
+
+#Uh oh!  I got carried away and created two new features.  
+#I want to separate addition/subtraction into two commits.

行开头的blank表示该行与提交的文件和原始文件相同。该行开头的+表示该行未出现在已提交的文件中,但该行显示在原始文件中。 (并且行的开头有-符号表示该行显示在已提交的文件中,但不显示在原始文件中。)有关读取差异的更多详细信息,包括@@ -1,3 +1,10 @@的含义,请参阅{{ 3}}。

然后我重复了第二种方法的过程:

git add -p new_feature.rb

(该命令相当于git add -i new_feature.rb,然后选择 p atch菜单项。)

选择e后,我只需删除文件末尾的注释:

+#Uh oh!  I got carried away and created two new features.                    
+#I want to separate addition/subtraction into two commits.

然后另一个提交:

git commit -m "Add subtraction() method."

然后我留下了包含底部注释的原始文件,我在文件中并不想要。此外,该评论导致new_feature.rb在git status中显示为已修改的文件,因为已提交的版本未包含该评论。所以,我将原始文件重置为git知道的内容:

git checkout new_feature.rb

删除了已提交文件与未暂存原始文件之间的任何差异 - 并且无法恢复。

这给了我一个干净的git status

$ git status
On branch new_feature
nothing to commit, working directory clean

这是提交历史:

$ git log

commit 70c566157a0f41052c6091ce9025d8b95722015f
Author: 7stud <me@me.com>
Date:   Tue May 26 13:06:21 2015 0000

    Add subtraction() method.

commit 2ca5952c53bae7bc407d21cb3601395886d2cd4c
Author: 7stud <me@me.com>
Date:   Tue May 26 13:05:41 2015 0000

    Add addition() method.

commit 72ae28cbd1d7cf998eca5862b18e2af45b58f752
Author: 7stud <me@me.com>
Date:   Tue May 26 13:00:55 2015 0000

    Add README file.

1 个答案:

答案 0 :(得分:3)

您的大块中没有未更改的行,因此您无法将其拆分。使用e命令并手动编辑差异。使用vim对于它的高级线编辑功能特别有帮助。

看起来像这样:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -0,0 +1,10 @@
+def addition(x, y)
+  x+y
+end
+
+def substraction(x,y)
+  x-y
+end
+
+#Uh oh!  I got carried away and created two new features.  
+#I want to separate addition/subtraction into two commits.
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.