Two git aliases

Git logo
Git

I have tried using GUI based Git clients, but I always return to the command line. Not sure what the problem is. I think it may be either a habit, or a combination of that and the fact most GUI clients try to hide away Git details. That’s not necessarily bad. And it may work if you are new to Git. However, if you are like be who started using Git a long time ago, you may find it difficult to learn the abstraction each GUI client tries to serve you.

The only thing that I like, and I do use a client like that, is when I want to stage parts of a file line by line. I usually do it using GitHub Desktop. If I don’t care about the individual lines, and only care about the hunks, I will still probably use git add -p.

Lately, I found myself checking the list of commits I have done in my topic branch. And I also like seeing the list of files those commits include.

Here are two git aliases I am using quite a lot in the terminal:

[alias]
  branch-name = "!git rev-parse --abbrev-ref HEAD"
  commits = "!sh -c \"git log --left-right --graph --cherry-pick --oneline ${1:-master}...${2:-$(git branch-name)}\" -"
  files = "!sh -c \"git diff --name-only ${1:-master}...${2:-$(git branch-name)}\" -"

That is in my .gitconfig.

branch-name returns the current branch you have checked out.

Here is how to use it:

$ git commits
> 1480424 (HEAD -> petros/wizard-wars) Create a new wizard for additional organizations
> 3eec67f Add a path to the add payment method wizard step

$ git files
db/seeds.rb

So both commits in the example above contain changes in just one file: db/seeds.rb.

The current branch is petros/wizard-wars and the base branch is master. I didn’t have to type them explicitly.

Sometimes, you branch off of a branch other than your default one. For example, I am working on a branch called payments currently. Its base branch is master. I then created a new branch off of payments to work on a smaller part of the code. The branch is called petros/subscriptions-form. Here is how I had created those:

$ git checkout master
$ git checkout -b payments
$ git checkout -b petros/subscriptions-form payments

Here is how to see the list of commits:

$ git commits payments petros/subscription-form
> 293d8bf (HEAD -> petros/subscription-form, origin/petros/subscription-form) Add path to Add payment method pending task
> 19f25a6 Activate a Supportress subscription
> 08d39af Replace old plan stripe IDs in sample data
> e60250c Add formatted amount helper
> 99bdd89 Add pry-rails

And here are the files I have touched:

$ git files payments petros/subscription-form
Gemfile
Gemfile.lock
app/controllers/organization_scope/payment_methods_controller.rb
app/helpers/application_helper.rb
app/javascript/packs/payments_stripe.js
app/models/organization.rb
app/models/subscription.rb
app/views/layouts/application.html.erb
app/views/organization_scope/payment_methods/show.html.erb
config/initializers/stripe.rb
config/routes.rb
db/migrate/20200522194400_update_onboarding_step.rb
db/seeds.rb
db/structure.sql
lib/tasks/sample_data.rake

I tried to automate finding the correct parent branch, but I couldn’t find a reliable way. So I am fine just typing the base branch whenever that’s not the standard master one.

I hope you find those useful.

What are you up to Petros?

After 9 years at GitHub Support, I am now building a simple and fast customer support tool at an unbeatable price, to help teams stay calm and productive. I have founded a calm company called HeavyMelon. You can always check what I am doing now and where to find me online.

Leave a Reply