手动使用git add --patch <文件名> 进行编辑

9

所以,我正在分支A中处理一个文件,并且即将准备提交它。然而,在查看差异时,我认为最好将其分成两个单独的提交(在这种情况下,也许是两个单独的分支)。我之前已经使用git add --patch来分别暂存不同的块,因此我认为我可以使用它。问题是,我需要拆分其中的一个块。运行git add --patch SdA.py并使用e来编辑问题块...

# Manual hunk edit mode -- see bottom for a quick guide
@@ -50,13 +50,74 @@ import PIL.Image as im

 import constant

+
+def exp_range(min=None, max=None, step=None):
+    """
+    Generate an exponentially increasing value scaled and offset such
+    that it covers the range (min, max].  Behaviour is similar to
+    exp(x), scaled such that the final value generated is equal to
+    'max'.  'step' defines the granularity of the exponential
+    function.  The default value is 5, corresponding to a step-size
+    of tau.
+
+    :type min: float
+    :param min: minimum value of range (offset)
+
+    :type max: float
+    :param max: Maximum (final) value of range
+
+    :type step: int
+    :param step: Number of incremental steps within the range
+                 (min, max]
+    
+    """
+    if min is None:
+        raise StopIteration
+
+    # One input argument (same as range(10))
+    if min is not None and max is None and step is None:
+        step = min
+        min = 0.
+        max = 1.
+    elif step is None:
+        step = 5
+
+    for i in xrange(step):
+        exp_rate = np.exp(i - (step-1))
+        yield min + (max - min) * exp_rate
+    raise StopIteration
+
+
 def norm(input):
+    """
+    Return the norm of a vector or row-wise norm of a matrix
+
+    :type input: theano.tensor.TensorType
+    :param input: Theano array or matrix to take the norm of.
+    
+    """
     return T.sqrt((input * input).sum(axis=0))


 def normalize(vector, scale=1.0, offset=0.5):
+    """
+    Normalize (Zero and scale) a vector such that it's peak to peak
+    value is equal to 'scale', and it is centered about 'offset'.
+
+    :type vector: numpy.ndarray
+    :param vector: Vector to normalize to the given parameters.
+
+    :type scale: float
+    :param scale: Peak-to-peak range to stretch or shrink the vector's
+                  current peak-to-peak range to.
+
+    :type offset: float
+    :param offset: Value at which to center the peak-to-peak range at.
+    
+    """
     return (vector - vector.min()) * scale / vector.ptp()

+

没问题。底部有一个迷你指南,我明白了。所以,我们想把新的功能放在这个提交中,而其他功能的文档放在另一个提交中。根据迷你文档:# 要删除“+”行,请将它们删除。

# Manual hunk edit mode -- see bottom for a quick guide
@@ -50,13 +50,74 @@ import PIL.Image as im

 import constant

+
+def exp_range(min=None, max=None, step=None):
+    """
+    Generate an exponentially increasing value scaled and offset such
+    that it covers the range (min, max].  Behaviour is similar to
+    exp(x), scaled such that the final value generated is equal to
+    'max'.  'step' defines the granularity of the exponential
+    function.  The default value is 5, corresponding to a step-size
+    of tau.
+
+    :type min: float
+    :param min: minimum value of range (offset)
+
+    :type max: float
+    :param max: Maximum (final) value of range
+
+    :type step: int
+    :param step: Number of incremental steps within the range
+                 (min, max]
+    
+    """
+    if min is None:
+        raise StopIteration
+
+    # One input argument (same as range(10))
+    if min is not None and max is None and step is None:
+        step = min
+        min = 0.
+        max = 1.
+    elif step is None:
+        step = 5
+
+    for i in xrange(step):
+        exp_rate = np.exp(i - (step-1))
+        yield min + (max - min) * exp_rate
+    raise StopIteration
+
+
 def norm(input):
     return T.sqrt((input * input).sum(axis=0))


 def normalize(vector, scale=1.0, offset=0.5):
     return (vector - vector.min()) * scale / vector.ptp()

那看起来不错。让我们添加它...

error: patch failed: SdA.py:50
error: SdA.py: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

Mmkay... git add --interactive "Your edited hunk does not apply"如何阅读git diff的输出?解释了我必须更新受影响的行号。现在,我可以手动计算并说:“嗯,我已经删除了1、2、3……23行。我之前正在编辑74行,现在我正在编辑……嗯……希望我有一个计算器......51行”(“哇,我出汗了”)。
这似乎是一种过于复杂的方法。我仍然认为补丁是正确的方法,但如果我需要手动更新文件中受影响的行数,那么我肯定做错了什么。有没有人有更轻松、更高效的建议?
2个回答

8

将要删除的行用#注释掉,而不是删除它们,可以解决这个问题。我不确定这种行为是否是emacs的一部分,但注释一行实际上会将补丁消息顶部的计数器递减。另一个我发现有用的功能是使用s先分割块,然后独立地添加每一个块。在这个特定的例子中,这将是更好的解决方案。


这在我的Sublime中也可以工作(但没有自动递减计数器)。 - ricab

1

有一些Git图形界面工具可以轻松地选择性地暂存代码行,你只需要选中单个代码行并添加即可,无需手动编辑补丁。

其中两个这样的GUI工具是SourceTreeGit Cola


3
我有点反感图形用户界面,因为我觉得它们增加了大多数事物的不必要抽象。这既有好处也有坏处。对于复杂操作,GUI成为一种工具,可能比我做得更好。然而,对于简单操作(我认为这个补丁是一个简单操作),我认为应该优先选择命令行。我会等待几天,看看是否还有其他答案。谢谢你的建议。 - Jason_L_Bens
@Phox,就我个人而言,我几乎完全使用命令行来使用Git,但对于选择性暂存,我的经验是GUI使操作更快捷和容易。 - user456814
+1 for git-cola,我已经在Linux上寻找这个功能一段时间了。我经常使用git add -p,但由于某种原因,“s”键无法正常工作。 - nemesisdesign
这完全没有回答问题。这个问题涉及CLI git(这是一个合适的客户端),而且有一种方法可以使用它来完成,无论其他工具如何比较。 - ricab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接