Forem

Cover image for 95% Of Software Engineers Aren't Using This Simple Git Trick
Suraj
Suraj

Posted on

95% Of Software Engineers Aren't Using This Simple Git Trick

If you work with Git regularly, you know that git log can be overwhelming, displaying a long list of commit hashes, authors, dates, and messages in a cluttered format.

To make your logs more readable and visually appealing, you can create a custom Git alias:

alias glp='git log --pretty=format:"%C(yellow)%h%Creset - %C(green)%an%Creset, %ar : %s"'
Enter fullscreen mode Exit fullscreen mode

What This Alias Does:

Instead of the default git log output, this alias presents commit history in a concise, color-coded format:

  • %h → Shows the short commit hash in yellow.
  • %an → Displays the author’s name in green.
  • %ar → Indicates the commit time relative to now (e.g., "2 hours ago" or "3 days ago").
  • %s → Displays the commit message.

Example Output:

Image

Making the Alias Permanent

By default, the alias will only work in the current terminal session. Once you close the terminal, it will be lost. Follow these steps to make it permanent:

Step 1: Check Your Current Shell

Before adding the alias, check which shell you are using:

echo $SHELL
Enter fullscreen mode Exit fullscreen mode

If the output is:

  • /bin/bash → You are using Bash.
  • /bin/zsh → You are using Zsh.

This is important because the alias should be added to the correct configuration file.

Step 2: Add the Alias to the Correct File

For Bash Users:

Edit the ~/.bashrc file:

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add the following line at the end of the file:

alias glp='git log --pretty=format:"%C(yellow)%h%Creset - %C(green)%an%Creset, %ar : %s"'

Enter fullscreen mode Exit fullscreen mode

Save the file (CTRL + X, then Y, then Enter).

For Zsh Users:

Edit the ~/.zshrc file:

nano ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Add the alias at the end of the file:

alias glp='git log --pretty=format:"%C(yellow)%h%Creset - %C(green)%an%Creset, %ar : %s"'
Enter fullscreen mode Exit fullscreen mode

Save the file (CTRL + X, then Y, then Enter).

Step 3: Apply the Changes

For Bash, run:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

For Zsh, run:

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Step 4: Test the Alias

Now, run:

glp
Enter fullscreen mode Exit fullscreen mode

If the alias works, you’ll see a clean and structured Git log output.

What If the Alias Still Doesn't Work?

If you are using Bash and see errors like command not found: shopt, your ~/.bashrc file might be corrupted.

To fix it, reset your Bash configuration:

mv ~/.bashrc ~/.bashrc.bak  # Backup old file
cp /etc/skel/.bashrc ~/.bashrc  # Restore default bashrc
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Then, re-add the alias and try again.

Why Use This?

  • Improved readability – No more overwhelming logs.
  • Quick insights – Easily see who made a commit and when.
  • Better debugging – Quickly locate specific changes.

By customizing your Git workflow with aliases like this, you can make working with Git much more efficient. 🚀

Try it out and streamline your Git experience!

Top comments (0)