Let’s say at a previous commit some feature was working and I altered the code related to that feature after the commit. I want to compare the code from the working commit with the current code. What is the best way to do this?
`
I can run git checkout commithash
but it means I have to switch back and forth between the current branch and old commit.
Update
It turns out there is a git command designed for this exact use case:
- Get the of the working code.
- Checkout the branch where your bug is.
- Run
git diff <commit-hash>
If it’s hard to pinpoint the code differences due e.g. the volume of code, try running git diff <commit-hash> -- file-path
. This will show the differences in a VIM editor.
If the Vim editor isn’t helping try:
git checkout <commit-hash>
- Copy the file
git checkout og_branch
- Create a new file e.g. settings_old.py and paste the old working code in it.
- Open up each file side by side to compare.
2
Answers
Git does not work like that.
You can either use VS Code external Plugins (such as GitLens), or use git tooling (gitk for example) to inspect old changes.
gitk <COMMIT-HASH>
I would go with the first option and install GitLens – it’s a great plugin that allows you to do exactly what you’re after.
If you want to compare a lot of files or even directories with an external tool, another solution is to use
git worktree
feature.You could checkout code at a given commit inside another ‘wortree’ directory with the command:
Then you have your current branch and the checked out commit folders side by side and you could open 2 instances of Vscode or use an external tool able to compare directories.
See git worktree doc for more details: https://git-scm.com/docs/git-worktree