提交前 Git 比较文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10988757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:53:51  来源:igfitidea点击:

Git Compare files before commit

gitgithubgithub-for-windows

提问by mallows98

Is there a functionality in git where I could compare my local files to a git source control prior to committing changes?

git 中是否有一个功能可以在提交更改之前将我的本地文件与 git 源代码控制进行比较?

回答by Karthik Bose

Of-course you can do.

你当然可以。

  1. Using git diffcommand without any arguments: will compare each modified files in your file system against the files in the current checked-out branch (or) tag.

  2. Using git diff <tag(or)branch name>: will compare each modified files in your file system against the files in the specified branch (or) tag.

  3. Using git diff <path/to/file_name (or) path/to/folder>: will compare the specified file or files in the folder in your file system against the current checked-out branch (or) tag.

  4. Using git diff <tag1(or)branch1 name> <tag2(or)branch2 name>: will compare all modified files between two branches / tags.

  1. 使用git diff不带任何参数的命令:将文件系统中每个修改过的文件与当前检出分支(或)标签中的文件进行比较。

  2. 使用git diff <tag(or)branch name>: 会将文件系统中每个修改过的文件与指定分支(或)标签中的文件进行比较。

  3. 使用git diff <path/to/file_name (or) path/to/folder>: 将文件系统中文件夹中的指定文件或文件与当前检出的分支(或)标记进行比较。

  4. 使用git diff <tag1(or)branch1 name> <tag2(or)branch2 name>: 将比较两个分支/标签之间的所有修改过的文件。

There are many options, you can pass to 'git diff' command to format your output. Here I've listed a few:

有很多选项,您可以传递给 'git diff' 命令来格式化输出。我在这里列出了一些:

  • git diff --name-only: Show only names of changed files, not the contents.
  • git diff --name-status: Show only names and status of changed files.
  • git diff --cached (or --staged): compares only the files which are staged/indexed.
  • git diff --name-only: 仅显示更改文件的名称,不显示内容。
  • git diff --name-status:仅显示更改文件的名称和状态。
  • git diff --cached (or --staged): 只比较暂存/索引的文件。

for more information: execute git diff --helpin your git bash.

更多信息:git diff --help在你的 git bash 中执行。

FYI: git diffwill generate output in command line. If you want to see the output in some visual tools, use git difftool.

仅供参考:git diff将在命令行中生成输出。如果要在某些可视化工具中查看输出,请使用git difftool.

Using git difftool: you can configure the git to use diff/merge tool to compare files. Checkout this link: use Winmerge inside of Git to file diff

使用git difftool:您可以将 git 配置为使用 diff/merge 工具来比较文件。查看此链接:在 Git 中使用 Winmerge 来归档差异

You can pass all git diffarguments and options to git difftoolas well.

您也可以将所有git diff参数和选项传递给git difftool

回答by Frank Sposaro

I like to use

我喜欢用

git status

It will show you what files have changed and what you are tracking. You can then use

它将显示哪些文件已更改以及您正在跟踪哪些文件。然后你可以使用

git diff

or the more GUI friendly

或者更友好的 GUI

gitk

To see the diff.

要查看差异。

回答by positron

I prefer to use this method.

我更喜欢使用这种方法。

A script for comparing

比较的脚本

    #!/bin/sh
(                              # execute in a subshell so you can continue
                               #   working in the current shell
    set -o xtrace              # bash setting that echos each command before it's executed
    > /tmp/auto_bcompare_log   # truncate existing log file
    BCOMP_PATH=/usr/bin/bcompare
    BRANCH=""                # get branch argument from command line
    TEMPDIR=`mktemp -d`        # get a temp directory
    CWD=`pwd`                  # remember the current directory
    git clone $CWD $TEMPDIR
    cd $TEMPDIR
    git checkout $BRANCH
    cd $CWD
    $BCOMP_PATH $CWD $TEMPDIR
    rm -rf $TEMPDIR
) >> /tmp/auto_bcompare_log 2>&1 < /dev/null & # background and redirect
                                               # stdout/stderr/stdin

The above script is definitely_not_ written by me. I took it from the net, but not sure of the sources. Save this in your repo as compare.sh. Make sure you give the correct path for the bcompare file.

上面的脚本绝对_不是_我写的。我是从网上拿的,但不确定来源。将它保存在你的仓库中作为 compare.sh。确保为 bcompare 文件提供正确的路径。

For comparing before commit.

用于在提交前进行比较。

./compare.sh <branch_you_want_to_compare>

What this script basically does is that, it checkouts the branch that you give as a parameter to a temp directory and opens up for a comparison with your pwd. With this you can review your changes before committing. I hope this is what you want.

这个脚本的主要作用是,它检出您作为参数提供给临时目录的分支,并打开与您的密码进行比较。有了这个,您可以在提交之前查看您的更改。我希望这就是你想要的。

回答by ptyx

Since commits are easy to amend/change before they're pushed, I find it easier to commit, and then at the log to see what I changed.

由于提交在推送之前很容易修改/更改,我发现提交更容易,然后在日志中查看我更改的内容。

In case it's not what I'm expecting, amend, reset, rebase -i, etc...

如果这不是我所期望的,请修改、重置、rebase -i 等...