Git is confusing af. There I said it. In my brief experience using developer tools so far it’s one of the most clunky (albeit powerful) tools out there for software development and something we all have to learn how to use. Throughout the course of our capstone project I’ve come across a few tricks/hacks that might make your life a little easier.
Switching back and forth between branches quickly
Oftentimes I find myself checking out some branch, some other branch, then switching back and having to type out all this stuff out. When your branch names are like utilities/audio-processor-improvements
it gets a bit tedious.
A trick is to use git checkout -
to switch back and forth between the two last branches quickly. Example:
Navigate to the repo home dir
This takes your shell to the repo’s home directory of the working tree which is especially useful in our project’s monorepo since our services are often deeply nested:
cd $(git rev-parse --show-toplevel)
Note: I did not come up with this myself. See this SO answer.
Using git for-each-ref
As the documentation states,
Iterate over all refs that match
https://git-scm.com/docs/git-for-each-ref<pattern>
and show them according to the given<format>
, after sorting them according to the given set of<key>
. If<count>
is given, stop after showing that many refs. The interpolated values in<format>
can optionally be quoted as string literals in the specified host language allowing their direct evaluation in that language.
Ok well that basically means nothing to me so let’s work through an example of using this. Say we want to get an overview of whatever remote branches exist currently. Use git for-each-ref refs/remotes
.
Cool that’s relatively useful, but it would be more useful to know WHEN were these commits that are listed made. We can do that by using custom --format
arguments for our command. Say we want to visualize some of the previous information with the date added in:
git for-each-ref refs/remotes --format='%(committerdate:relative) ---- %(authorname) ---- %(refname:short)'
Unfortunately the --format
flag requires us to specify all the information we want so adding additional information to this command would require us to add it individually. More options are available in the documentation. Finally, we can sort the information ascending or descending (I prefer descending) by adding the --sort
flag as so as well as viewing only the unmerged branches.
git for-each-ref refs/remotes --sort=-committerdate --format="%(committerdate:relative) ---- %(authorname) ---- %(refname:short)" --no-merged
There’s options to add color and styling but that’s all I have for now! You can add these commands as aliases to your gitconfig
or whatever dotfiles you’re using. Onto finishing up the project!