使用git add --patch <filename> </filename>手动编辑

时间:2013-07-24 03:20:11

标签: git version-control git-add git-patch

所以,我有一个我一直在分支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()

+

没关系。底部有一个迷你指南。我明白了。因此,我们希望将新函数放在此提交中,将其他函数的文档放入另一个提交中。根据迷你文档:# To remove '+' lines, delete them.

# 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"How to read the output from git diff?解释说我必须更新受影响的行号。为了做到这一点,现在,我可以手动计数并说“嗯,我已经删除了1,2,3 ...... 23行。之前我正在编辑74行,现在我正在编辑......嗯......祝我有一个计算器...... ...... 51行“('哇,我出汗')

这似乎是一个过于复杂的方法。我仍然认为补丁是正确的方法,但如果我需要手动更新目标文件中受影响的行数,我必须做错事。任何人都有关于如何更轻松有效地做到这一点的任何建议吗?

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

有Git GUI可以很容易地选择性地分割线条,您只需选择单独的线条并添加它们,无需手动编辑。“

两个这样的GUI是SourceTreeGit Cola