您如何暂存新文件的各个部分?
How do you stage parts of a new file?
我尝试了这里的建议:
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 提交的内容与原始文件的内容:
~/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.
A blank
在行的开头意味着该行对于提交的文件和原始文件是公共的。行首的 +
表示该行未出现在提交的文件中,但该行出现在原始文件中。 (行首的 -
符号表示该行出现在提交的文件中而不是原始文件中。)有关读取差异的更多详细信息,包括 @@ -1,3 +1,10 @@
的含义,请参阅 here.
然后我重复了第二种方法的过程:
git add -p new_feature.rb
(该命令等同于 git add -i new_feature.rb
然后选择 patch 菜单项。)
选择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.
你的hunk中没有不变的行,所以你不能拆分它。使用 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.
我尝试了这里的建议:
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 提交的内容与原始文件的内容:
~/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.
A blank
在行的开头意味着该行对于提交的文件和原始文件是公共的。行首的 +
表示该行未出现在提交的文件中,但该行出现在原始文件中。 (行首的 -
符号表示该行出现在提交的文件中而不是原始文件中。)有关读取差异的更多详细信息,包括 @@ -1,3 +1,10 @@
的含义,请参阅 here.
然后我重复了第二种方法的过程:
git add -p new_feature.rb
(该命令等同于 git add -i new_feature.rb
然后选择 patch 菜单项。)
选择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.
你的hunk中没有不变的行,所以你不能拆分它。使用 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.