显示提交之间的差异

发布于 2024-09-12 06:10:25 字数 489 浏览 12 评论 0原文

我在 Ubuntu 10.04 (Lucid Lynx) 上使用 Git。

我已经向我的主人做出了一些承诺。

但是,我想了解这些提交之间的差异。它们都在我的主分支上。

例如:

commit dj374
made changes

commit y4746
made changes

commit k73ud
made changes

我想得到k73ud和dj374之间的区别。但是,当我执行以下操作时,我看不到我在 k73ud 中所做的更改。

git diff k73ud..dj374 > master.patch

I am using Git on Ubuntu 10.04 (Lucid Lynx).

I have made some commits to my master.

However, I want to get the difference between these commits. All of them are on my master branch.

For example:

commit dj374
made changes

commit y4746
made changes

commit k73ud
made changes

I want to get the difference between k73ud and dj374. However, when I did the following I couldn't see the changes I made in k73ud.

git diff k73ud..dj374 > master.patch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

水晶透心 2024-09-19 06:10:25

尝试

git diff k73ud^..dj374

确保在结果差异中包含 k73ud 的所有更改。

git diff 比较两个端点 (而不是提交范围)。
由于 OP 希望看到 k73ud 引入的更改,因此他们需要区分 第一个父提交 <代码>k73ud:k73ud^(或k73ud^1< /code> 或 k73ud~)。

这样,diff 结果将包含自 k73ud 父级以来的更改(意味着包括 k73ud 本身的更改),而不是 k73ud 以来引入的更改(直至 dj374)。

您也可以尝试:

git diff oldCommit..newCommit
git diff k73ud..dj374 

和(1 个空格,而不是更多):

git diff oldCommit newCommit
git diff k73ud dj374

如果您只需要获取文件名(例如手动复制修补程序):

git diff k73ud dj374 --name-only

并且您可以将更改应用于另一个分支:

git diff k73ud dj374 > my.patch
git apply my.patch

Try

git diff k73ud^..dj374

to make sure to include all changes of k73ud in the resulting diff.

git diff compares two endpoints (instead of a commit range).
Since the OP wants to see the changes introduced by k73ud, they need to differentiate between the first parent commit of k73ud: k73ud^ (or k73ud^1 or k73ud~).

That way, the diff results will include changes since k73ud parent (meaning including changes from k73ud itself), instead of changes introduced since k73ud (up to dj374).

Also you can try:

git diff oldCommit..newCommit
git diff k73ud..dj374 

and (1 space, not more):

git diff oldCommit newCommit
git diff k73ud dj374

And if you need to get only files names (e.g. to copy hotfix them manually):

git diff k73ud dj374 --name-only

And you can get changes applied to another branch:

git diff k73ud dj374 > my.patch
git apply my.patch
一片旧的回忆 2024-09-19 06:10:25

要查看以下之间的区别:

您的工作副本和暂存区域:

% git diff

暂存区域和最新提交:

% git diff --staged

您的工作副本和提交 4ac0a6733:

% git diff 4ac0a6733

提交 4ac0a6733 和最新提交:

% git diff 4ac0a6733 HEAD

提交 4ac0a6733 和提交 826793951

% git diff 4ac0a6733 826793951

有关更多说明,请参阅 官方文档

To see the difference between:

Your working copy and staging area:

% git diff

Staging area and the latest commit:

% git diff --staged

Your working copy and commit 4ac0a6733:

% git diff 4ac0a6733

Commit 4ac0a6733 and the latest commit:

% git diff 4ac0a6733 HEAD

Commit 4ac0a6733 and commit 826793951

% git diff 4ac0a6733 826793951

For more explanation see the official documentation.

若言繁花未落 2024-09-19 06:10:25

如果您想查看每次提交引入的更改,请尝试“git log -p”

If you want to see the changes introduced with each commit, try "git log -p"

能怎样 2024-09-19 06:10:25
  1. gitk --all
  2. 选择第一个提交
  3. 右键单击另一个提交,然后diff selected → this
  1. gitk --all
  2. Select the first commit
  3. Right click on the other, then diff selected → this
策马西风 2024-09-19 06:10:25

要查看两个不同提交之间的差异(我们将其称为 ab),请使用

git diff a..b
  • 注意 ab 之间的差异ba相反。

要查看上次提交和尚未提交的更改之间的差异,请使用

git diff

如果您希望稍后能够返回差异,则可以将其保存在文件中。

git diff a..b > ../project.diff

To see the difference between two different commits (let's call them a and b), use

git diff a..b
  • Note that the difference between a and b is opposite from b and a.

To see the difference between your last commit and not yet committed changes, use

git diff

If you want to be able to come back to the difference later, you can save it in a file.

git diff a..b > ../project.diff
倦话 2024-09-19 06:10:25

我使用 gitk 来查看差异:

gitk k73ud..dj374

它有 GUI 模式,因此审阅更容易。

I use gitk to see the difference:

gitk k73ud..dj374

It has a GUI mode so that reviewing is easier.

审判长 2024-09-19 06:10:25

最简单的检查拉取后最后 2 次提交的更改:

git diff HEAD~2 

Simplest for checking the changes in the last 2 commits after pull:

git diff HEAD~2 
痞味浪人 2024-09-19 06:10:25
 1. git diff <commit-id> <commit-id>
 2. git diff HEAD^ HEAD     -(HEAD = current branch’s tip),( HEAD^ = version before the last commit)
 3. git diff HEAD^ HEAD — ./file     (comparison to specified file)
 4. git diff HEAD~5 HEAD           - (HEAD~5 refers to the last 5 commits.)
 1. git diff <commit-id> <commit-id>
 2. git diff HEAD^ HEAD     -(HEAD = current branch’s tip),( HEAD^ = version before the last commit)
 3. git diff HEAD^ HEAD — ./file     (comparison to specified file)
 4. git diff HEAD~5 HEAD           - (HEAD~5 refers to the last 5 commits.)
や三分注定 2024-09-19 06:10:25

我编写了一个脚本,显示两次提交之间的差异,在 Ubuntu 上运行良好。

https://gist.github.com/jacobabrahamb4/a60624d6274ece7a0bd2d141b53407bc

#!/usr/bin/env python
import sys, subprocess, os

TOOLS = ['bcompare', 'meld']

def execute(command):
    return subprocess.check_output(command)

def getTool():
    for tool in TOOLS:
        try:
            out = execute(['which', tool]).strip()
            if tool in out:
                return tool
        except subprocess.CalledProcessError:
            pass
    return None

def printUsageAndExit():
    print 'Usage: python bdiff.py <project> <commit_one> <commit_two>'
    print 'Example: python bdiff.py <project> 0 1'
    print 'Example: python bdiff.py <project> fhejk7fe d78ewg9we'
    print 'Example: python bdiff.py <project> 0 d78ewg9we'
    sys.exit(0)

def getCommitIds(name, first, second):
    commit1 = None
    commit2 = None
    try:
        first_index = int(first) - 1
        second_index = int(second) - 1
        if int(first) < 0 or int(second) < 0:
            print "Cannot handle negative values: "
            sys.exit(0)
        logs = execute(['git', '-C', name, 'log', '--oneline', '--reverse']).splitlines()
        if first_index >= 0:
            commit1 = logs[first_index].split(' ')[0]
        if second_index >= 0:
            commit2 = logs[second_index].split(' ')[0]
    except ValueError:
        if first is not '0':
            commit1 = first
        if second is not '0':
            commit2 = second
    return commit1, commit2

def validateCommitIds(name, commit1, commit2):
    if not commit1 and not commit2:
        print "Nothing to do, exit!"
        return False
    try:
        if commit1:
            execute(['git', '-C', name, 'cat-file', '-t', commit1])
        if commit2:
            execute(['git', '-C', name, 'cat-file', '-t', commit2])
    except subprocess.CalledProcessError:
        return False
    return True

def cleanup(commit1, commit2):
        execute(['rm', '-rf', '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])

def checkoutCommit(name, commit):
    if commit:
        execute(['git', 'clone', name, '/tmp/'+commit])
        execute(['git', '-C', '/tmp/'+commit, 'checkout', commit])
    else:
        execute(['mkdir', '/tmp/0'])

def compare(tool, commit1, commit2):
        execute([tool, '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])

if __name__=='__main__':
    tool = getTool()
    if not tool:
        print "No GUI diff tools, install bcompare or meld"
        sys.exit(0)
    if len(sys.argv) is not 4:
        printUsageAndExit()

    name, first, second = None, 0, 0
    try:
        name, first, second = sys.argv[1], sys.argv[2], sys.argv[3]
    except IndexError:
        printUsageAndExit()

    commit1, commit2 = getCommitIds(name, first, second)

    if validateCommitIds(name, commit1, commit2) is False:
        sys.exit(0)

    cleanup(commit1, commit2)

    try:
        checkoutCommit(name, commit1)
        checkoutCommit(name, commit2)
        compare(tool, commit1, commit2)
    except KeyboardInterrupt:
        pass
    finally:
        cleanup(commit1, commit2)
    sys.exit(0)

I wrote a script which displays diff between two commits, works well on Ubuntu.

https://gist.github.com/jacobabrahamb4/a60624d6274ece7a0bd2d141b53407bc

#!/usr/bin/env python
import sys, subprocess, os

TOOLS = ['bcompare', 'meld']

def execute(command):
    return subprocess.check_output(command)

def getTool():
    for tool in TOOLS:
        try:
            out = execute(['which', tool]).strip()
            if tool in out:
                return tool
        except subprocess.CalledProcessError:
            pass
    return None

def printUsageAndExit():
    print 'Usage: python bdiff.py <project> <commit_one> <commit_two>'
    print 'Example: python bdiff.py <project> 0 1'
    print 'Example: python bdiff.py <project> fhejk7fe d78ewg9we'
    print 'Example: python bdiff.py <project> 0 d78ewg9we'
    sys.exit(0)

def getCommitIds(name, first, second):
    commit1 = None
    commit2 = None
    try:
        first_index = int(first) - 1
        second_index = int(second) - 1
        if int(first) < 0 or int(second) < 0:
            print "Cannot handle negative values: "
            sys.exit(0)
        logs = execute(['git', '-C', name, 'log', '--oneline', '--reverse']).splitlines()
        if first_index >= 0:
            commit1 = logs[first_index].split(' ')[0]
        if second_index >= 0:
            commit2 = logs[second_index].split(' ')[0]
    except ValueError:
        if first is not '0':
            commit1 = first
        if second is not '0':
            commit2 = second
    return commit1, commit2

def validateCommitIds(name, commit1, commit2):
    if not commit1 and not commit2:
        print "Nothing to do, exit!"
        return False
    try:
        if commit1:
            execute(['git', '-C', name, 'cat-file', '-t', commit1])
        if commit2:
            execute(['git', '-C', name, 'cat-file', '-t', commit2])
    except subprocess.CalledProcessError:
        return False
    return True

def cleanup(commit1, commit2):
        execute(['rm', '-rf', '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])

def checkoutCommit(name, commit):
    if commit:
        execute(['git', 'clone', name, '/tmp/'+commit])
        execute(['git', '-C', '/tmp/'+commit, 'checkout', commit])
    else:
        execute(['mkdir', '/tmp/0'])

def compare(tool, commit1, commit2):
        execute([tool, '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])

if __name__=='__main__':
    tool = getTool()
    if not tool:
        print "No GUI diff tools, install bcompare or meld"
        sys.exit(0)
    if len(sys.argv) is not 4:
        printUsageAndExit()

    name, first, second = None, 0, 0
    try:
        name, first, second = sys.argv[1], sys.argv[2], sys.argv[3]
    except IndexError:
        printUsageAndExit()

    commit1, commit2 = getCommitIds(name, first, second)

    if validateCommitIds(name, commit1, commit2) is False:
        sys.exit(0)

    cleanup(commit1, commit2)

    try:
        checkoutCommit(name, commit1)
        checkoutCommit(name, commit2)
        compare(tool, commit1, commit2)
    except KeyboardInterrupt:
        pass
    finally:
        cleanup(commit1, commit2)
    sys.exit(0)
乖乖哒 2024-09-19 06:10:25

我总是喜欢使用命令行,并且手边有用户友好的工具(带有 GUI)。两全其美。以下是我在 Git 中比较两次提交的方法。

您可以显示两次提交之间的差异,如下所示。

在文本编辑器中编辑 git 配置文件:

git config --global -e 

在 Windows 中的 Git 配置文件中设置一个适当的 diff 工具(用户友好的),例如 Meld:

[difftool "meld"]
cmd = "C:/Program Files (x86)/Meld/Meld.exe" "LOCAL\" \"REMOTE" --label "DIFF (ORIGINAL MY)"
prompt = false
path = C:\Program Files (x86)\Meld\Meld.exe

可以使用 Chocolatey 从命令行安装 Meld:

choco install meld

让我们定义一个 shell 函数帮助我们比较文本编辑器中 [alias] 下的两个 sha-s(提交):

[alias]
showchangesbetween = "!w() { git difftool \"$1\" \"$2\" --dir-diff --ignore-all-space; }; w"

要借助 Meld(或您最喜欢的其他比较工具)比较提交,只需在命令行中键入:

git showchangesbetween somesha123 somesha456

提交 sha-s 是 容易看到的打字。

 git log 

例如,

I always love using the command line and have user friendly tools (with GUI) at my hand. Best of both worlds. Here is how I do it to compare two commits in Git.

You can show the diff between two commits like the following.

Edit your git config file in a TEXT EDITOR:

git config --global -e 

Set up a proper diff tool (user friendly) like Meld like this in Windows in the Git config file:

[difftool "meld"]
cmd = "C:/Program Files (x86)/Meld/Meld.exe" "LOCAL\" \"REMOTE" --label "DIFF (ORIGINAL MY)"
prompt = false
path = C:\Program Files (x86)\Meld\Meld.exe

Meld can be installed using Chocolatey like this from the COMMAND LINE:

choco install meld

Let's define a shell function to help us compare two sha-s (commits) under [alias] in the TEXT EDITOR:

[alias]
showchangesbetween = "!w() { git difftool \"$1\" \"$2\" --dir-diff --ignore-all-space; }; w"

To compare the commits with the help of Meld (or your other favorite diff tool, just type at the COMMAND LINE:

git showchangesbetween somesha123 somesha456

The commit sha-s are easily visible typing

 git log 

for example.

长梦不多时 2024-09-19 06:10:25

接受的答案很好。

只是把它再次放在这里,这样就很容易理解和使用了。将来尝试一下,

git diff c1...c2 > mypatch_1.patch  
git diff c1..c2  > mypatch_2.patch  
git diff c1^..c2 > mypatch_3.patch  

我对上述所有命令都得到了相同的差异。

以上有帮助
1.查看commit c1和commit c1之间的区别另一个提交 c2
,可用于将更改应用到另一个分支

2.还制作一个显示差异的补丁文件,如果它没有正确显示差异
然后c1 & c2可能取错了
因此,将它们调整为提交前(如 c1 至 c0),或调整为提交后(如 c2 至 c3)

使用 gitk 查看提交 SHA,第 8 个字符足以将它们用作 c0、c1、c2 或c3.您还可以从 Gitlab > 查看提交 ID存储库>提交等。

希望有帮助。

Accepted answer is good.

Just putting it again here, so its easy to understand & try in future

git diff c1...c2 > mypatch_1.patch  
git diff c1..c2  > mypatch_2.patch  
git diff c1^..c2 > mypatch_3.patch  

I got the same diff for all the above commands.

Above helps in
1. seeing difference of between commit c1 & another commit c2
2. also making a patch file that shows diff and can be used to apply changes to another branch

If it not showing difference correctly
then c1 & c2 may be taken wrong
so adjust them to a before commit like c1 to c0, or to one after like c2 to c3

Use gitk to see the commits SHAs, 1st 8 characters are enough to use them as c0, c1, c2 or c3. You can also see the commits ids from Gitlab > Repository > Commits, etc.

Hope that helps.

野心澎湃 2024-09-19 06:10:25

下面的命令在 Ubuntu 20.04 和 git v2.25.1 上非常适合我:

git diff <base-commit-id> <target-commit-id>

Command below perfectly works for me on Ubuntu 20.04 and git v2.25.1:

git diff <base-commit-id> <target-commit-id>
冰之心 2024-09-19 06:10:25

对于最后两次提交,

git diff HEAD~1 HEAD

通过扩展来比较 2 个提交,例如

git diff HEAD~6 HEAD~3

For the last two commits

git diff HEAD~1 HEAD

by extension to compare 2 commits,that can be for example

git diff HEAD~6 HEAD~3
画骨成沙 2024-09-19 06:10:25

假设您在底部(最旧的)还有一个提交,那么这将变得非常简单:

commit dj374
made changes

commit y4746
made changes

commit k73ud
made changes

commit oldestCommit
made changes

现在,使用下面的内容将轻松达到目的。

git diff k73ud oldestCommit

Let's say you have one more commit at the bottom (oldest), then this becomes pretty easy:

commit dj374
made changes

commit y4746
made changes

commit k73ud
made changes

commit oldestCommit
made changes

Now, using below will easily server the purpose.

git diff k73ud oldestCommit
ゞ记忆︶ㄣ 2024-09-19 06:10:25

使用此命令来了解提交和未暂存之间的区别:

git difftool --dir-diff

Use this command for the difference between commit and unstaged:

git difftool --dir-diff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文