git添加比较和合并工具(meld)

  git 下的(difftool)和(mergetool)是专门提供给使用者用自己的工具进行diff和merge的命令:

# git config --global  diff.tool  meld

# git config --global  merge.tool meld

然后直接使用命令进行两次提交的比较和合并:

# git difftool HEAD HEAD^1

缺点:

虽然使用git difftool已经基本满足了我的需要,但还有个小问题:如果我要比较两次提交之间的差异时,difftool只能一个文件一个文件的比较,每次都要提示你是否打开这个文件,然后打开meld进行比较,当你关闭meld后,才会提示下一个差异文件。这样非常浪费效率,下面一个方法直接利用meld的目录比较能力(参考: https://github.com/thenigan/git-diffall):

1.新建diffall文本文件

2.在diffall文件中添加如下代码:

  1 #!/bin/sh
  2 # Copyright 2010 - 2012, Tim Henigan <[email protected]>
  3 #
  4 # Permission is hereby granted, free of charge, to any person obtaining
  5 # a copy of this software and associated documentation files (the
  6 # "Software"), to deal in the Software without restriction, including
  7 # without limitation the rights to use, copy, modify, merge, publish,
  8 # distribute, sublicense, and/or sell copies of the Software, and to
  9 # permit persons to whom the Software is furnished to do so, subject to
 10 # the following conditions:
 11 #
 12 # The above copyright notice and this permission notice shall be included
 13 # in all copies or substantial portions of the Software.
 14 #
 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 18 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 19 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 20 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 21 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 22
 23
 24 # Perform a directory diff between commits in the repository using
 25 # the external diff or merge tool specified in the user‘s config.
 26
 27 USAGE=‘[--cached] [--copy-back] [-x|--extcmd=<command>] <commit>{0,2} [-- <path>*]
 28     --cached     Compare to the index rather than the working tree.
 29     --copy-back  Copy files back to the working tree when the diff
 30                  tool exits (in case they were modified by the
 31                  user).  This option is only valid if the diff
 32                  compared with the working tree.
 33     -x=<command>
 34     --extcmd=<command>  Specify a custom command for viewing diffs.
 35                  git-diffall ignores the configured defaults and
 36                  runs $command $LOCAL $REMOTE when this option is
 37                  specified. Additionally, $BASE is set in the
 38                  environment.
 39 ‘
 40
 41 SUBDIRECTORY_OK=1
 42 . "$(git --exec-path)/git-sh-setup"
 43
 44 TOOL_MODE=diff
 45 . "$(git --exec-path)/git-mergetool--lib"
 46
 47 merge_tool="$(get_merge_tool)"
 48 if test -z "$merge_tool"
 49 then
 50     echo "Error: Either the ‘diff.tool‘ or ‘merge.tool‘ option must be set."
 51     usage
 52 fi
 53
 54 start_dir=$(pwd)
 55
 56 # All the file paths returned by the diff command are relative to the root
 57 # of the working copy. So if the script is called from a subdirectory, it
 58 # must switch to the root of working copy before trying to use those paths.
 59 cdup=$(git rev-parse --show-cdup) &&
 60 cd "$cdup" || {
 61     echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
 62     exit 1
 63 }
 64
 65 # set up temp dir
 66 tmp=$(perl -e ‘use File::Temp qw(tempdir);
 67     $t=tempdir("/tmp/git-diffall.XXXXX") or exit(1);
 68     print $t‘) || exit 1
 69 trap ‘rm -rf "$tmp"‘ EXIT
 70
 71 left=
 72 right=
 73 paths=
 74 dashdash_seen=
 75 compare_staged=
 76 merge_base=
 77 left_dir=
 78 right_dir=
 79 diff_tool=
 80 copy_back=
 81
 82 while test $# != 0
 83 do
 84     case "$1" in
 85     -h|--h|--he|--hel|--help)
 86         usage
 87         ;;
 88     --cached)
 89         compare_staged=1
 90         ;;
 91     --copy-back)
 92         copy_back=1
 93         ;;
 94     -x|--e|--ex|--ext|--extc|--extcm|--extcmd)
 95         if test $# = 1
 96         then
 97             echo You must specify the tool for use with --extcmd
 98             usage
 99         else
100             diff_tool=$2
101             shift
102         fi
103         ;;
104     --)
105         dashdash_seen=1
106         ;;
107     -*)
108         echo Invalid option: "$1"
109         usage
110         ;;
111     *)
112         # could be commit, commit range or path limiter
113         case "$1" in
114         *...*)
115             left=${1%...*}
116             right=${1#*...}
117             merge_base=1
118             ;;
119         *..*)
120             left=${1%..*}
121             right=${1#*..}
122             ;;
123         *)
124             if test -n "$dashdash_seen"
125             then
126                 paths="$paths$1 "
127             elif test -z "$left"
128             then
129                 left=$1
130             elif test -z "$right"
131             then
132                 right=$1
133             else
134                 paths="$paths$1 "
135             fi
136             ;;
137         esac
138         ;;
139     esac
140     shift
141 done
142
143 # Determine the set of files which changed
144 if test -n "$left" && test -n "$right"
145 then
146     left_dir="cmt-$(git rev-parse --short $left)"
147     right_dir="cmt-$(git rev-parse --short $right)"
148
149     if test -n "$compare_staged"
150     then
151         usage
152     elif test -n "$merge_base"
153     then
154         git diff --name-only "$left"..."$right" -- $paths >"$tmp/filelist"
155     else
156         git diff --name-only "$left" "$right" -- $paths >"$tmp/filelist"
157     fi
158 elif test -n "$left"
159 then
160     left_dir="cmt-$(git rev-parse --short $left)"
161
162     if test -n "$compare_staged"
163     then
164         right_dir="staged"
165         git diff --name-only --cached "$left" -- $paths >"$tmp/filelist"
166     else
167         right_dir="working_tree"
168         git diff --name-only "$left" -- $paths >"$tmp/filelist"
169     fi
170 else
171     left_dir="HEAD"
172
173     if test -n "$compare_staged"
174     then
175         right_dir="staged"
176         git diff --name-only --cached -- $paths >"$tmp/filelist"
177     else
178         right_dir="working_tree"
179         git diff --name-only -- $paths >"$tmp/filelist"
180     fi
181 fi
182
183 # Exit immediately if there are no diffs
184 if test ! -s "$tmp/filelist"
185 then
186     exit 0
187 fi
188
189 if test -n "$copy_back" && test "$right_dir" != "working_tree"
190 then
191     echo "--copy-back is only valid when diff includes the working tree."
192     exit 1
193 fi
194
195 # Create the named tmp directories that will hold the files to be compared
196 mkdir -p "$tmp/$left_dir" "$tmp/$right_dir"
197
198 # Populate the tmp/right_dir directory with the files to be compared
199 while read name
200 do
201     if test -n "$right"
202     then
203         ls_list=$(git ls-tree $right "$name")
204         if test -n "$ls_list"
205         then
206             mkdir -p "$tmp/$right_dir/$(dirname "$name")"
207             git show "$right":"$name" >"$tmp/$right_dir/$name" || true
208         fi
209     elif test -n "$compare_staged"
210     then
211         ls_list=$(git ls-files -- "$name")
212         if test -n "$ls_list"
213         then
214             mkdir -p "$tmp/$right_dir/$(dirname "$name")"
215             git show :"$name" >"$tmp/$right_dir/$name"
216         fi
217     else
218         if test -e "$name"
219         then
220             mkdir -p "$tmp/$right_dir/$(dirname "$name")"
221             cp "$name" "$tmp/$right_dir/$name"
222         fi
223     fi
224 done < "$tmp/filelist"
225
226 # Populate the tmp/left_dir directory with the files to be compared
227 while read name
228 do
229     if test -n "$left"
230     then
231         ls_list=$(git ls-tree $left "$name")
232         if test -n "$ls_list"
233         then
234             mkdir -p "$tmp/$left_dir/$(dirname "$name")"
235             git show "$left":"$name" >"$tmp/$left_dir/$name" || true
236         fi
237     else
238         if test -n "$compare_staged"
239         then
240             ls_list=$(git ls-tree HEAD "$name")
241             if test -n "$ls_list"
242             then
243                 mkdir -p "$tmp/$left_dir/$(dirname "$name")"
244                 git show HEAD:"$name" >"$tmp/$left_dir/$name"
245             fi
246         else
247             mkdir -p "$tmp/$left_dir/$(dirname "$name")"
248             git show :"$name" >"$tmp/$left_dir/$name"
249         fi
250     fi
251 done < "$tmp/filelist"
252
253 LOCAL="$tmp/$left_dir"
254 REMOTE="$tmp/$right_dir"
255
256 if test -n "$diff_tool"
257 then
258     export BASE
259     eval $diff_tool ‘"$LOCAL"‘ ‘"$REMOTE"‘
260 else
261     run_merge_tool "$merge_tool" false
262 fi
263
264 # Copy files back to the working dir, if requested
265 if test -n "$copy_back" && test "$right_dir" = "working_tree"
266 then
267     cd "$start_dir"
268     git_top_dir=$(git rev-parse --show-toplevel)
269     find "$tmp/$right_dir" -type f |
270     while read file
271     do
272         cp "$file" "$git_top_dir/${file#$tmp/$right_dir/}"
273     done
274 fi

3.保存,运行如下命令配置

# git config --global alias.diffall /PATH/diffall
				
时间: 2024-10-08 08:15:49

git添加比较和合并工具(meld)的相关文章

Ubuntu下Git安装图形化代码合并工具kdiff3

kdiff3是个优秀的图像化代码比较与合并工具,安装也十分容易: sudo apt-get install kdiff3 git config --global merge.tool kdiff3 安装完成. 遇到代码要手动合并的时候,输入 git mergetool 即可打开图形化合并工具. 原文地址:https://www.cnblogs.com/liuwei0773/p/9342992.html

Windows平台下使用Beyond Compare作为GIT默认的比对与合并工具

在Windows平台使用GUI习惯了,因此在CMD命令下反而感到不适 特别是在使用GIT时,尤其明显(这主要是GIT在工作中已经不可或缺) 使用GIT最常用的功能就是提交,添加,比较差异和合并分支,特别是在更改的文件比较多的情况下在CMD命令下查看差异下显得特别的不人性化(鼠标不停的向下翻) 终于实在是忍受不了,所以就想能不能通过自己最常用的方式来比较差异呢? 因为在工作中一直使用的就是Beyond Compare,所以决定将其作为默认的GIT比较工具 编辑GIT配置文件 C:\Users\Ad

Windows下使用Beyond Compare作为git的比对与合并工具

Windows下使用Beyond Compare作为git的比对与合并工具 介绍 其实各种git的GUI客户端都有自带比对工具,但是一开始学Git的时候,用的是Windows下的Git Bash,后来也渐渐熟悉各种命令,用图形客户端反而不习惯了. 这里介绍如何将Beyond Compare配置为git的difftool和mergetool.当需要比对或者合并冲突时,就可以通过difftool和mergetool调用Beyond Compare进行比对和合并冲突了. 操作 目前我电脑里安装的是Be

Ubuntu 下安装使用文件比较合并图形工具Meld

Meld是一款跨平台的文件比较合并工具使用Python开发,具体内容参照官网:http://meldmerge.org/ 注意以下环境要求: Requirements Python 2.7 (Python 3 not yet supported) GTK+ 3.6 (3.12 in development) GLib 2.34 (2.36 in development) PyGObject 3.8 GtkSourceView 3.6 (3.10 in development) 1:选择适合的版本

Git 分支创建与合并

3.2 Git 分支 - 分支的新建与合并 分支的新建与合并 现在让我们来看一个简单的分支与合并的例子,实际工作中大体也会用到这样的工作流程: 开发某个网站. 为实现某个新的需求,创建一个分支. 在这个分支上开展工作. 假设此时,你突然接到一个电话说有个很严重的问题需要紧急修补,那么可以按照下面的方式处理: 返回到原先已经发布到生产服务器上的分支. 为这次紧急修补建立一个新分支,并在其中修复问题. 通过测试后,回到生产服务器所在的分支,将修补分支合并进来,然后再推送到生产服务器上. 切换到之前实

ubuntu安装文件比较工具Meld

Meld是一款可视化的文件及目录对比(diff) / 合并 (merge) 工具,通过它你可以对两个或三个文件/目录进行对比,并以图形化的方式显示出它们的不同之处,同时还提供编辑及合并功能,另外还支持 CVS, Subversion, Bazaar-ng 和 Mercurial 等版本控制工具. 项目主页: http://meld.sourceforge.net/ # Ubuntu 用户安装: sudo apt-get install meld # 整合到gedit sudo apt-get i

git 添加外部项目地址

github 提交第三方模块流程 // git config --global user.name 'your name' 可以设置全局用户名,在commit记录里显示的是这个配置设置的名称. // git config --global user.email 'your email' 可以设置全局用户邮箱. // git config user.name 可以查看当前设置的用户名 一.模块的创建及更新 1.创建模块 --新建一个github仓库repository,比如:git_submodu

Git添加远程库和从远程库中获取(新手傻瓜式教学)

一.    Git添加远程库 1.在本地新建一个文件夹,在该文件夹使用Git工具,运行$ git init,将该文件夹变为本地Git仓库,同时会生成一个隐藏的.git文件夹. 2.在该文件夹中用NotePad++建立一个xx.txt文件(建立文件即可,不要求什么格式与内容):在Git工具中依次运行 $ git add xx.txt 和  $ git commit -m "描述信息". 3.创建SSH Key.在Git工具中运行 $ ssh-keygen -t rsa -C [email

git使用之rebase合并提交

git使用之rebase合并提交技术 maybe yes 发表于2015-03-15 22:43 原文链接 : http://blog.lmlphp.com/archives/88/The_use_tutorial_of_git_rebase_to_merge_multiple_commits_as_a_submit  来自 : LMLPHP后院 对于版本控制系统 GIT ,一直没有深入研究,只是从日常使用方面入手,能解决平常使用出现的问题就可以了.GIT 的版本控制,有三种后悔方式:reset