git diff HEAD vs staged vs local
When using tools like pre-commit (as I’ve discussed in my post on simple CI pipelines) you often end up in the state where it would be handy to review the differences between:
- the code you staged for commit (which pre-commit ran its checks against)
- the local version of the code after various automations like
black
have run on the code and created modified versions of the code locally (unstaged)
It’s not particularly obvious how to see the familiar differences between these versions
of the code, but you want to see what’s going on if you’re not happy to blindly accept
the changes made to your code. If you are, you just git add .
to accept all the
changes
Using git
If the differences are fairly small and simple, viewing the differences in the command line with git is possible, however this becomes impractical with larger amounts of lines changed
git status -vv
Using pycharm
If you’ve used pycharm for any significant length of time, you’ve probably used the nice functionality to compare differences between commits, or between the HEAD and the local state of the code. It’s not very clear though how to do the same thing between the three existing states HEAD, Staged changes, Unstaged/local changes…
First you need to “Enable staging area” in the git settings of Pycharm.
Then you have two options:
Commit tab
Go to the “Commit” tab (on the left-hand side where “Project” is) and right-click a file > “Compare HEAD, Staged and Local versions”
From a file with changes
Right-click in the editor > “Compare HEAD, Staged and Local versions”