1

I just created a local commit, and I would like to see all the changes that were made in that local commit, so I would like to diff it against what it was prior to that commit. Do I just git log to find the IDs of the last commits, and then git diff last_commit_id prior_to_last_commit_id, or is there a better approach?

In addition, when I tried this command, it brought up some kind of terminal interactive tool where I have to press the down arrow to see the changes in a file. Is there a way to show this in an editor instead of in the terminal?

user5965026
  • 465
  • 5
  • 16
  • `git diff HEAD~1 > file.patch` then open the file? But I'd recommend knowing what's going into the commit _before_ making it, using e.g. `git add -p` to review piece by piece and choosing what to stage. – jonrsharpe Dec 09 '21 at 17:55
  • Does this answer your question? [How to see the changes between two commits without commits in-between?](https://stackoverflow.com/questions/1191282/how-to-see-the-changes-between-two-commits-without-commits-in-between) – SwissCodeMen Dec 09 '21 at 22:09
  • Consider also `git log -p`, perhaps with `--no-walk` or `-n 1` or `-1`; and `git show`. The `git show` command is roughly equivalent to `git log -p --no-walk` (with the key difference being their behavior on merge commits). – torek Dec 10 '21 at 07:23

2 Answers2

1

The short answer is: Yes! You did it correctly.
To compare those commits you need to do the git diff command:
git diff commit_id_1 commit_id_2

Then what you are facing is the default diff tool which is used to compare the commits.
If you'd like it to be opened in a IDE like Visual Studio Code, just use this solution:
How to use Visual Studio Code as the default editor for Git MergeTool

Or for any other IDE see:
Configuring diff tool with .gitconfig

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • I think I configured git to use vscode but `git diff commit_id_1 commit_id_2` still seems to default to the terminal. I followed the instructions in the TLDR section in https://www.roboleary.net/vscode/2020/09/15/vscode-git.html. Is there a particular flag that needs to be included with `git diff` to use vscode? – user5965026 Dec 09 '21 at 18:36
  • I've noticed that I had pasted a wrong link for VS code in the answer. Now it's fixed, please check the link. That was the one that I've used on my end. – Just Shadow Dec 10 '21 at 08:25
  • Yeah it looks like I have the same settings as the one in your link. What happens when you do `git diff commit_id_1 commit_id_2`, does it open it up in VSC for you? Mine still uses terminal even while using those settings – user5965026 Dec 10 '21 at 15:07
  • 1
    You can configure the git for both local repo and globally. And the settings of the local repo override the global one. I guess you just have such situation. Just check your local gitconfig file to see if it has some changes. Or just apply the same settings for the local repo – Just Shadow Dec 13 '21 at 08:19
1

Use git show to see the last commit

Since you're talking about seeing the contents of just one commit, I would use git show instead of git diff.

git show

will display the contents of HEAD, which is going to be the last commit since you just made it. It display the log message and the changes introduced by that commit.

To see the contents of any other commit, just specify it:

git show <sha1 or other commitish>

Use git log -p to see the last few commits

To extend your use case just a little bit, if you want to see the last few commits, the quickest way there is the -p switch to git log, which shows the log one commit after the other, but with the same contents that git show

git log -p
joanis
  • 10,635
  • 14
  • 30
  • 40