在Ubuntu中,git diff输出到文件的结果奇怪

3

我想在Ubuntu 13.04上创建一个补丁,在使用Netbeans或其他IDE时没有问题,但是当我在命令行中运行时:

git diff > 1.patch

然后

cat 1.patch

输出如下:

diff --git a/sites/all/modules/pet/pet.module b/sites/all/modules/pet/pet.module
index adfd273..1091090 100755
--- a/sites/all/modules/pet/pet.module
+++ b/sites/all/modules/pet/pet.module
@@ -71,7 +71,7 @@ function pet_access($op, $type = NULL, $account = NULL) {
 /**
  * Implements hook_permission().
  */
-function pet_permission() {  
+function pet_permission() {
   $permissions = array(
     'administer previewable email templates' =>  array(
       'title' => t('Administer previewable email templates'),
@@ -183,26 +183,26 @@ function pet_lookup_uid($mail) {
 /**
  * Send tokenized email to a list of recipients.
  *
- * Given a list of recipients, and an optional node id, perform token 
+ * Given a list of recipients, and an optional node id, perform token
  * substitution and send an email to each. The node substitutions, if any,
  * are the same in each email sent.  The user tokens, if any are custom based
- * on the account (if any) associated with each email.  
+ * on the account (if any) associated with each email.
  *
  * @param $name
  *   The unique name of the PET template.
  * @param $recipients
  *   An array of at least one recipient in one of two formats:
  *      1. a simple email address, in which case the uid is looked up
- *      2. an array('mail' => <email address>, 'uid' => <uid>) in which case 
+ *      2. an array('mail' => <email address>, 'uid' => <uid>) in which case
  *         the uid is already available (more efficient)
  * @param $options
  *   An array of options as follows:
  *      nid - An optional node id for token substitutions.
- *      subject - An optional subject which if provided will override the 
+ *      subject - An optional subject which if provided will override the
  *        subject in the PET.
  *      body - An optional body which if provided which will override the body
  *        in the PET.
- *      body_plain - An optional plain text body which if provided which will 
+ *      body_plain - An optional plain text body which if provided which will
  *        override the plain text body in the PET.
  *      from - An optional from email which if provided which will override the
  *        from in the PET (which in turn overrides the site default).
@@ -221,12 +221,12 @@ function pet_send_mail($name, $recipients, $options) {
     watchdog('pet', 'At least one recipient must be provided for PET %name.', array('%name' => $name), WATCHDOG_NOTICE);
     return;
   }

但是gedit显示如下:
[1mdiff --git a/sites/all/modules/pet/pet.module b/sites/all/modules/pet/pet.module[m
[1mindex adfd273..1091090 100755[m
[1m--- a/sites/all/modules/pet/pet.module[m
[1m+++ b/sites/all/modules/pet/pet.module[m
[36m@@ -71,7 +71,7 @@[m [mfunction pet_access($op, $type = NULL, $account = NULL) {[m
 /**[m
  * Implements hook_permission().[m
  */[m
[31m-function pet_permission() {  [m
[32m+[m[32mfunction pet_permission() {[m
   $permissions = array([m
     'administer previewable email templates' =>  array([m
       'title' => t('Administer previewable email templates'),[m
[36m@@ -183,26 +183,26 @@[m [mfunction pet_lookup_uid($mail) {[m
 /**[m
  * Send tokenized email to a list of recipients.[m
  *[m
[31m- * Given a list of recipients, and an optional node id, perform token [m
[32m+[m[32m * Given a list of recipients, and an optional node id, perform token[m
  * substitution and send an email to each. The node substitutions, if any,[m
  * are the same in each email sent.  The user tokens, if any are custom based[m
[31m- * on the account (if any) associated with each email.  [m
[32m+[m[32m * on the account (if any) associated with each email.[m
  *[m
  * @param $name[m
  *   The unique name of the PET template.[m
  * @param $recipients[m
  *   An array of at least one recipient in one of two formats:[m
  *      1. a simple email address, in which case the uid is looked up[m
[31m- *      2. an array('mail' => <email address>, 'uid' => <uid>) in which case [m
[32m+[m[32m *      2. an array('mail' => <email address>, 'uid' => <uid>) in which case[m
  *         the uid is already available (more efficient)[m
  * @param $options[m
  *   An array of options as follows:[m
  *      nid - An optional node id for token substitutions.[m

.

3个回答

7

这段文字是有颜色的。<kbd>Esc</kbd>[36m 是一种用于改变前景色的 ANSI 转义序列。

1. 禁用颜色

git diff --color=never

2. 或者更改配置文件

git config color.ui auto

这会导致.git/config文件包含以下内容:
[color]
    ui = auto

auto 表示当输出到终端时颜色自动启用,但如果输出到文件/管道,则不会启用。

3. 修复现有补丁

查看 AnsiFilter,以从现有的补丁文件中删除 ANSI 转义序列。


太好了!谢谢你!我使用了第一种选项并得到了正确的补丁。 - Vlad Savitsky
1
然后我检查了我的配置,发现[color] diff = always和ui = true。我删除了'diff = always'并设置了'ui = auto',正如你建议的那样,得到了正确的补丁和彩色的git diff输出!再次感谢你,你救了我的一天! - Vlad Savitsky

2
我点击了这个链接 enter link description here,查看颜色: $ export LESS="-eirMX"
你可能需要编辑.profile.bashrc文件以永久设置环境变量。为了方便,您只需将上述行追加到以下3个文件中的一个即可:
~/.profile
~/.bash_profile
/etc/profile 

我发现在 macOS 上必须使用“-R”,这与“-r”不同。 - dz902

1
你可以试一下这个,它对我有效:

git config --global color.diff auto

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