0

Hope you're doing well. Thank you for checking out this issue of mine.

I'd like to git diff two commits of two different branches like the following:

git diff temp-branch/working_directory..main/specific_commit

Testing git repo example:

git init

# first write
echo "this is a line" >> file1.txt
git add file1.txt && git commit -m "write a line to file1"

# second write
echo "this is the second line" >> file1.txt
git add file1.txt && git commit -m "write second line to file1"

# third write
echo "this is the third line" >> file1.txt
git add file1.txt && git commit -m "write third line to file1"

# fourth write
echo "this is the fourth line" >> file1.txt
git add file1.txt && git commit -m "write fourth line to file1"

main's git log:

commit d68dfcb1537154d0d2c0de1ab6f432032a642fd4 (HEAD -> main)

    write fourth line to file1

commit f2538275051c139db38afda1b05d24b3128ef601

    write third line to file1

commit 09d3644bf509b58251123033d565564b0a30bcfa

    write second line to file1

commit 3801f8850f9480a2ac9644ba65705386d9319e61

    write a line to file1

Now I'll do the following: creating a branch with a specific commit ("write second line to file1"), such that the HEAD is 09d3644bf509b58251123033d565564b0a30bcfa:

git checkout -b temp-branch 09d3644bf509b58251123033d565564b0a30bcfa

Then I'll write the fifth line to file1.txt:

echo "this is the fifth line" >> file1.txt

temp-branch's git status:

Changes not staged for commit:
        modified:   file1.txt

temp-branch's git log:

commit 09d3644bf509b58251123033d565564b0a30bcfa (HEAD -> temp-branch)

    write second line to file1

commit 3801f8850f9480a2ac9644ba65705386d9319e61

    write a line to file1

Given the current checkout branch is temp-branch. How do I compare temp-branch working directory with main's specific commit that's not HEAD (e.g., f2538275051c139db38afda1b05d24b3128ef601)? I'd like to compare temp-branch's "this is the fifth line" with main's "this is the third line".

In other words, I'd like to git diff temp-branch's working directory with main's f2538275051c139db38afda1b05d24b3128ef601.

Any input is much appreciated. Thank you.

phd
  • 82,685
  • 13
  • 120
  • 165
kohane15
  • 809
  • 12
  • 16
  • 2
    You can use `git diff commit1 commit2` to compare 2 commits as long as both commits are available in the repository. – ElpieKay Mar 22 '23 at 08:13
  • @ElpieKay That's the issue: `temp-branch` HEAD points to an older commit of `main` and so the commit I want to compare are non-existent in `temp-branch` – kohane15 Mar 22 '23 at 08:25
  • 2
    Why do you think compared commits somehow have to "exist in a branch". Commits exist regardless of branches. Like ElpieKay said, it only needs to exist *in the repository*. – Romain Valeri Mar 22 '23 at 08:26
  • 1
    @ElpieKay @Romain Valeri Thank you guys so much! You are right: I just need to find the commit hash regardless whether it exists in the branch (i.e., even though `HEAD` points to an older commit, I can still compare two commits). Thank you! Please create an answer and I'll mark it. Thanks again! – kohane15 Mar 22 '23 at 09:43
  • https://stackoverflow.com/search?q=%5Bgit-diff%5D+two+commits esp. the answer https://stackoverflow.com/a/10219941/7976758 – phd Mar 22 '23 at 12:52

1 Answers1

1

in my case, when i want to compare two different branches, i merge request one of the branch to another branch. it's not perfect but gitlab will compare those two branches, you can start from there

monterico
  • 340
  • 1
  • 3
  • 13