DEV Community

Jeremy Friesen
Jeremy Friesen

Posted on • Originally published at takeonrules.com on

Everyday Custom Git Commands

Paying Attention to What I Repeat...And Documenting It

In this article I want to talk about git and writing my own git commands. As implemented git will recognize any executable file in your $PATH that starts with git- as a git command.

On my machines I have a ~/bin folder that is part of my $PATH and has many such git commands. Let’s explore a few of them.

A Walk Through of Git “Slog”

There is one git command I use almost every day: git slog; a simple custom function that I wrote.

Here’s the body of that command:

git log \
    --graph \
    --pretty=format:'%C(auto)%h — %s %C(blue)%an, %C(green)(%cs)%Creset%C(auto)%d%Creset' \
    $@

Enter fullscreen mode Exit fullscreen mode

The above command generates one line per commit (with colors to help my visual scanning) and renders the “graph” of those commits.

The trailing $@ allows me to pass additional arguments to git slog that are passed forward to git log. Useful if I want to use --since, --until, --author or many of the other git log options.

Below is the first few lines of output from Hyrax’s main branch as of :

* 4f8b5ded3 — provide default PermissionBadge presenter behavior when `nil` tamsin johnson, (2022-08-23) (HEAD -> main, origin/main, origin/HEAD)
* 3e08ab376 — don't load_and_authorize a model when indexing Embargoes tamsin johnson, (2022-08-23)
* 1c40a73cd — support newer versions of FactoryBot tamsin johnson, (2022-08-23)
* bf5df6f9d — Move copy of template below injection of mixin it references Chris Colvard, (2022-08-23)
* 3bf4b276a — soften dependendy on search_state_class for app generation Chris Colvard, (2022-08-23)
* c97f078c1 — Remove TAGS rodyoukai, (2022-08-18)
* b04b1eb7d — update batch_upload_form test rodyoukai, (2022-08-18)
* a4b5e0d40 — add bibliographic_citation to work form rodyoukai, (2022-08-18)
* 27ef645f0 — Merge pull request #5790 from samvera/admin_set_fallback_main Daniel Pierce, (2022-08-05)
|\
| * a7afa4639 — AdminSet needs to fall back to an id if noid is false Rob Kaufman, (2022-08-03)
|/
* 346cef1ed — Merge pull request #5784 from samvera/cbeer-patch-1 Daniel Pierce, (2022-07-27)
|\
| * 63c3cecf4 — SPARQL gem monkey-patches Hash#deep_dup and breaks Blacklight's expected contract with Rails Chris Beer, (2022-07-23)
| * 35cc0a054 — Implement search_state_class used by upstream blacklight Chris Beer, (2022-07-22)
| * 6cd2ca8ba — Unpin Blacklight Chris Beer, (2022-07-22)
|/
* 0cdb45928 — comet: decouple Derivatives from Characterization tamsin johnson, (2022-07-14)

Enter fullscreen mode Exit fullscreen mode

Additional Custom Git Functions I’ve Written

Most of these originally started out as git aliases; but I opted to “promote” them to commands. In part as a matter of practice, but also as I found the commands easier to document.

There are several others that I wrote:

git-edit
Edit (via my $EDITOR) all files, that according to git, have changed.
git-byegone
Delete all local tracking branches whose remote branches are gone (e.g. deleted).
git-prune-branches
As git-byegone but also prune remote branches and delete branches that were merged into the given branch.
git-squash
Perform an interactive rebase on the current branch from the point where it diverged from the given branch.
git-stats-for
A command line Ruby script to look at an author’s stats for a given repository.
git-unmerged-edit
Edit (via my $EDITOR) all unmerged files in the current repository.

You can see them in the ./bin directory of my dotzshrc repository.

Conclusion

Throughout my workday, I find myself often consulting my command history.
I use CTRL + r for history-incremental-search-backward which provides a prompt to search my commands, and help me remember what I previously did.
Most of the above commands I found myself using a few times before I “properly” captured them. And to help me remember them, I wrote them to their own file.

Top comments (0)