gpt4 book ai didi

git - 您如何暂存新文件的各个部分?

转载 作者:太空狗 更新时间:2023-10-29 13:40:32 26 4
gpt4 key购买 nike

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

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

星号表示您选择修补的文件。要真正开始打补丁,您必须在下一个提示符下按回车键:

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.

行首的空白 表示该行对于提交的文件和原始文件是通用的。行首的 + 表示该行未出现在提交的文件中,但该行出现在原始文件中。 (行首的 - 符号表示该行出现在提交的文件中,而不是原始文件中。)有关阅读差异的更多详细信息,包括 @@ -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.

关于git - 您如何暂存新文件的各个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com