DEV Community

Cover image for Boost Your Git Productivity with Aliases: Start Saving Time Today!
Ione Souza Junior
Ione Souza Junior

Posted on • Originally published at ionixjunior.dev on

Boost Your Git Productivity with Aliases: Start Saving Time Today!

Tired of spending precious time typing out those long, repetitive Git commands? You're not alone. Git aliases offer a powerful solution to streamline your workflow and boost your productivity. Imagine effortlessly navigating your Git repository with shortcuts for common commands, saving time and reducing the risk of errors. This blog post will introduce you to the world of Git aliases, demystifying their functionality and demonstrating their immense power. We'll guide you through creating your own custom shortcuts, showcasing practical examples for common Git tasks like log, fetch, commit, and more. By the end, you'll be ready to embrace the efficiency and speed that Git aliases bring to your development process. Get ready to master Git and unleash your inner coding ninja!

What are Git Aliases?

Git aliases are essentially shortcuts for Git commands. They allow you to define custom names for frequently used Git commands, making your workflow faster and more efficient. Imagine you frequently use the command git status to check the state of your repository. With an alias, you could define git st to represent git status, saving you keystrokes every time you need to check your repository’s status.

Think of Git aliases as personalized macros for Git. You create them to map a custom name (your alias) to a specific Git command or combination of commands. This means you can essentially create custom commands tailored to your specific workflows and needs.

This is good, isn’t it? Let’s see in practice how to create it.

How to Create Git Aliases

Creating Git aliases is very simple. You can set them up globally, making them available across all your Git projects, or locally for a specific repository. Here’s a step-by-step guide to get you started.

The core command for creating Git aliases is:

git config --global alias.ALIAS_NAME COMMAND
Enter fullscreen mode Exit fullscreen mode

Replace ALIAS_NAME by the alias you want and the COMMAND by the command. Let’s create an alias named “st” that will represent git status:

git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

Now, whenever you type git st in your terminal, Git will execute git status behind the scenes.

This was very simple, but you can create more complex commands, with a lot of parameters. Let me show you an example about the “log” command. I like so much to see the log on graph and in one line, so I’ve created an alias for this:

git config --global alias.lg "log --oneline --graph"
Enter fullscreen mode Exit fullscreen mode

This alias is definitely a time saver! If you prefer, you can change your alias manually editing the ~/.gitconfig file. Also, you can type git config --list | grep alias to see all alias you already configure in your machine.

Aliases That I Use

Here’s a collection of my favorite Git aliases that help streamline my workflow. I’ve organized them by functionality to make it easier for you to see how they can be applied:

Status and Basic Navigation

  • alias.st=status: This is a classic, saving you from typing git status every time you want to check the current state of your repository.
  • alias.br=branch: A quick way to list your current branches or create new ones, replacing git branch.
  • alias.co=checkout: A simple way to switch to a different branch, replacing git checkout.
  • alias.sw=switch: Another alias for “checkout”, providing a more concise alternative.

Committing and Amending

  • alias.ci=commit: Short for “commit,” this alias streamlines the process of creating a new commit.
  • alias.cia=commit --amend: Use this for amending the last commit, adding changes or modifying the commit message.
  • alias.cian=commit --amend --no-edit: Similar to “cia” alias, but it skips the commit message editor, allowing you to quickly amend the commit without message changes.

Diffing & Comparing

  • alias.df=diff -w: This alias creates a diff with whitespace ignored, simplifying the comparison of code changes.
  • alias.dfword=diff -w --word-diff: This alias shows word-by-word differences in the diff, making it easier to pinpoint specific changes.
  • alias.dft=!f() { GIT_EXTERNAL_DIFF=difft git diff -w --ext-diff $@; }; f: This alias uses the “difft” program (a custom program) to generate the diff, making the output more concise and informative.
  • alias.sh=!f() { GIT_EXTERNAL_DIFF=difft git show -p --ext-diff $@; }; f: This alias uses a custom diff tool (defined by difft) to generate the diff output using git show command.

Cleaning Up the Workspace

  • alias.cl=clean -dfX: This alias removes untracked files and ignored files from your workspace, keeping things tidy.

Enhanced Log Viewing

  • alias.lg=log --oneline --graph: This alias displays a concise and graphical log, making it easier to visualize commit history.
  • alias.lga=log --oneline --graph --all: Similar to “lg”, but it shows all branches in the log, providing a more complete picture.
  • alias.lgd=log --pretty=format:'%h %ad | %s%d [%an]' --date=short: This alias provides a detailed commit log, including the commit hash, date, subject, and author.

Cherry-Picking and Fetching

  • alias.cp=cherry-pick: A shortcut for cherry-picking specific commits from other branches.
  • alias.ft=fetch origin -p: This fetches changes from the “origin” remote.

Finding the Parent Branch

  • alias.parent=!git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//' #: This complex alias finds the parent branch of the current branch, which can be helpful when working with feature branches.

Believe me: this simple alias help me a lot every day to working with Git. Here is the alias part of my .gitconfig file:

[alias]
    st = status
    ci = commit
    br = branch
    cia = commit --amend
    cian = commit --amend --no-edit
    df = diff -w
    dfword = diff -w --word-diff
    dft = "!f() { GIT_EXTERNAL_DIFF=difft git diff -w --ext-diff $@; }; f"
    co = checkout
    sw = switch
    cl = clean -dfX
    lg = log --oneline --graph
    lga = log --oneline --graph --all
    cp = cherry-pick
    ft = fetch origin -p
    parent = "!git show-branch | grep '*' | grep -v \"$(git rev-parse --abbrev-ref HEAD)\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//' #"
    lgd = log --pretty=format:'%h %ad | %s%d [%an]' --date=short
    sh = "!f() { GIT_EXTERNAL_DIFF=difft git show -p --ext-diff $@; }; f"
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering Git is essential for any developer, and Git aliases are your secret weapon for unlocking a more efficient and enjoyable workflow. By replacing lengthy commands with simple, personalized shortcuts, you can save countless hours, reduce errors, and gain a deeper understanding of your repository’s history.

We’ve explored the basics of Git aliases, and demonstrated practical examples. Now, it’s time to put your newfound knowledge into practice. Start by creating a few aliases for your most frequently used commands, and experiment with more complex combinations as you become more comfortable.

Remember, the power of Git aliases lies in their ability to adapt to your specific needs. Embrace the flexibility, experiment with different approaches, and personalize your Git experience to maximize your productivity.

So, don’t waste another minute on tedious commands! Start using Git aliases today and experience the joy of a streamlined, efficient workflow that empowers you to achieve more.

Top comments (0)