See your PR from the git cli


How to see your PR from the terminal using git.

With git, we can clone. We have a local copy. Then we push changes upstream. Often we're doing a pull request to the upstream source. Often this is somewhere like Github. And often pull request reviews are done via a github.com web ui. Have you ever, instead, wanted to be able to see the pull request (the contents of the branch that you plan to merge) via the git cli tool? If so, here are some common operations.

See PR commit log

See all the commits on your branch since it diverged:

git log main..HEAD

Note that HEAD is the current commit on the check-out branch.

Aside: For a prettier log, try this .gitconfig alias:

[alias]
    l = log --pretty='format:%C(yellow)%h %Creset- %C(white)%s %C(bold blue)[%an]%Creset %Cgreen%cr' -n 20 --graph

See a commit

This works on your current branch or any other known commit hash.

git show 58686a2

Use a shortened 7-digit version of the commit sha for easy viewing and typing.

See PR diff

This will output all the changes you've made since diverging from main.

git diff main..HEAD

See a PR file diff

This is the same as above, showing all changes since main divergence, but confined to a single file.

git diff HEAD..main -- ./src/saucy.clj

Those are some of the good ones to see where you stand, straight from the terminal. This is where the magic happens.

Revert a single file

You can checkout main to point your current branch back there, but if you want to update only a single file to what it is on main, before your branch's changes, do this:

git checkout main -- ./src/saucy.clj

This will modify the file and stage it. You'll then commit it. This will add to your log, then, of course.

If you're trying to avoid that, you can fall back to an interactive rebase, then drop or edit the commit, depending on the nature of the commit:

git rebase -i main

What are some of the useful commands of a similar nature that you use?