Git 如何处理新文件的部分内容?

Git 如何处理新文件的部分内容?,git,git-add,Git,Git Add,我在这里尝试了这个建议: 但我无法让它工作,因为交互模式显示整个文件,而没有选择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

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

但我无法让它工作,因为交互模式显示整个文件,而没有选择
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
+#Uh oh!  I got carried away and created two new features.                    
+#I want to separate 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中的部分代码修补该空白文件:

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 -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
):

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

星号表示您选择修补的文件。要实际开始修补,您必须在下一个提示下单击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,?]? 
编辑该文件不会更改原始文件(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提交的内容和原始文件的外观:

~/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.
行首处的
空白
表示该行与提交的文件和原始文件共用。行首处的
+
表示该行未显示在提交的文件中,但该行显示在原始文件中。(行首的
-
符号表示该行出现在提交的文件中,而不是原始文件中。)有关读取差异的详细信息,包括
@-1,3+1,10@
的含义,请参阅

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

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

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

~/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
+#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状态

$ 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.
$git日志
提交70c566157a0f41052c6091ce9025d8b95722015f
作者:7stud
日期:2015年5月26日星期二13:06:21 0000
添加减法()方法。
提交2ca5952c53bae7bc407d21cb3601395886d2cd4c
作者:7stud
日期:2015年5月26日星期二13:05:41 0000
Add addition()方法。
提交72ae28cbd1d7cf998eca5862b18e2af45b58f752
作者:7stud
日期:2015年5月26日星期二13:00:55 0000
添加自述文件。

您的臀部没有未更改的线条,因此您无法将其拆分。使用
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.

谢谢你的回复。你的身体里没有不变的线条——我不知道这意味着什么?这是一个新文件,所以从定义上来说,这不意味着什么都没有改变吗?使用e命令并手动编辑你的差异——我也不知道这意味着什么,在任何情况下,我读到拥有一个临时区域的全部意义是为了让你可以ilor您的提交--我想对一个方法使用一个提交,对另一个方法使用另一个提交。git不能这样做吗?@7stud 1.要使拆分生效,更改必须具有最小距离。它在新文件上不起作用。2.使用
e
而不是
s
。它会将您弹出到编辑器中,手动编辑差异。您的文件r工作树将保持不变,只有剩余的差异将应用于您的索引。啊,我明白了。我认为编辑该文件将更改我的原始文件。一个问题是:我不想在文件末尾提交我的注释。因此,在我分别提交每个方法后,我将保留一个修改后的文件,并在末尾添加注释。我该怎么做清除文件?git rm?@7stud将文件重置为您将使用的git版本:
git checkout---filename
第一个破折号是什么意思?
$ 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.
# 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.