avatarRaouf Makhlouf

Summary

The web content discusses the use of Git aliases to streamline and simplify common Git commands, including creating a more readable log, fixing typos in previous commits, and listing aliases.

Abstract

The article titled "Git: useful alias" emphasizes the importance of optimizing repetitive tasks in software development by using Git aliases. It provides solutions for improving the readability of Git logs through custom aliases, such as the 'lg' alias for a colorized and simplified log view. Additionally, it addresses the common issue of fixing typos in past commits by utilizing the '--fixup' and '--autosquash' options during an interactive rebase to maintain a clean Git history. The article also introduces a bonus alias for listing all Git aliases, reinforcing the theme of efficiency and convenience in version control workflows. The author encourages the use of these aliases to automate tasks and enhance the command-line interface experience.

Opinions

  • The author believes that developers prefer to create shortcuts and simplify commands to increase efficiency.
  • It is suggested that a customized Git log, such as the 'lg' alias, significantly improves readability.
  • The author values a "clean" Git history and recommends using '--fixup' and '--autosquash' to integrate fixes into existing commits seamlessly.
  • The creation of a Git alias named 'alias' to list all aliases is presented as a handy tool for developers who utilize aliasing.
  • The author expresses enthusiasm about the benefits of Git aliases, stating they are "super useful" for automating repetitive tasks and improving console output.

Git: useful alias

It’s well known That developers want to optimize and simplify repetitive tasks. We like to make shortcuts, type fewer characters to run this or that command. With this aim in mind, I suggest some git aliases.

Photo by Héctor J. Rivas on Unsplash

1. Pretty logging

Problem: The default display of the log is complicated, difficult to use. $ git log

git log

Solution: We create an alias called lg, which is a customized and simplified log with colors.

$ git config --global alias.lg " --no-pager log --color --graph --pretty = format: '% Cred% h% Creset -% C (yellow)% d% Creset% s% Cgreen (% cr)% C ( bold blue) <% an>% Creset '--abbrev-commit "

Now you can run: $ git lg

custom git logging

It is still much more readable! :)

2. Quick fix

Problem: Sometimes we have to fix a typo in a previous commit after a few new commits. But the goal is to keep a “clean” git history with consistent commits adding features to facilitate the code reviews.

Solution: With two useful options in git that work together, we can solve the problem: 1. to mark your commit as a fix of a previous commit $ git commit --fixup <commit to fix> 2. to organize merging of these fixup commits and associated regular commits $ git rebase -i --autosquash <commit to fix>~

Example: Suppose that we have the following git history:

We found a typo in the README file, so we can fix and want to modify the ‘chore() add README’ commit instead of creating a new commit for a typo:

${fix the typo}
$git add .
$git commit --fixup cab2a2c

Note that Git has created a commit with a message prefixed with ‘!fixup’ :

To clean-up the history, we interactively rebase our changes with $ git rebase --autosquash --interactive cab2a2c~. This will produce a clean history again:

The option--autosquash, has automatically reordered and applied the fixup commit. Note that this option only works with an interactive rebase.

With these, we can easily merge little fixes with the original feature and keep our branch clean.

In one shot with alias: Open the config file, $ git config --global -e then at the alias level, add:

[alias]
       fixup =! sh -c 'SHA =$git rev-parse $1) \
       && git commit --fixup $SHA \
       && git rebase -i --autosquash $SHA~ '-

Once the alias has been added, we can also fix bugs then fixup the modification.

We will obviously use the alias we just created. The only thing to do is copy the SHA of the commit to which we want to apply the fixup.

Then:

git fixup <commit to fix>

The rebase window opens, and we need to verify that everything is correct. Save, quit, and look at history.

That’s great, isn’t it! 👍

3. Bonus: List Git aliases

Alias is a great trick, but how do I print a list of my git aliases?

I created a git alias called (strangely enough) alias for precisely this purpose. It handy from time to time if you use useful aliasing.

Once you run :

git config --global alias.alias "! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /"

This will create a permanent git alias named alias Which gets stored in your ~/.gitconfig file. Using it will list all of your git aliases in nearly the same format as they are in the ~/.gitconfig file. To use it, type: git alias

List Git aliases

Wrapping Up

In this blog, we mentioned why git alias is essential, and we showed how easy using them. Overall, the alias is super useful to automatize some repetitive tasks or to improve console output.

Let’s keep in touch! Sign up for my weekly newsletter

❤ If you liked this post, you might also love:

Git
Alias
Best Practices
Coding
Programming
Recommended from ReadMedium