Top 10 Git Aliases to Supercharge Your Workflow

Discover the most essential Git aliases that can boost your productivity and streamline your workflow.

Top 10 Git Aliases to Supercharge Your Workflow

Git aliases are a powerful tool that can significantly enhance your productivity and streamline your workflow. By creating custom shortcuts for common Git commands, you can save time and reduce the cognitive load associated with remembering complex syntax. In this article, we'll explore some of the most important Git aliases that can help you work more efficiently and effectively.

Setting Up Git Aliases

To create Git aliases, you can use the git config command with the --global flag to define aliases that apply across all repositories on your system.

Here's an example of how to set up a simple alias which shows you all available aliases:

git config --global alias.alias "config --get-regexp ^alias"

This command creates a new alias named alias that lists all defined aliases when executed. You can run it by typing git alias in your terminal.

Top 10 Git Aliases

1. Git Checkout

One of the most commonly used Git commands is git checkout, which allows you to switch branches or restore files from the repository. To create a shortcut for this command, you can define an alias like this:

git config --global alias.co "checkout"

With this alias in place, you can use git co instead of git checkout to switch branches or restore files.

2. Git Checkout New Branch

Creating a new branch in Git typically involves two steps: first, you create the branch, and then you switch to it. You can combine these steps into a single command by defining an alias like this:

git config --global alias.cb "checkout -b"

Now you can use git cb <branch-name> to create a new branch and switch to it in one go.

3. Git Cherry-Pick

Isn't it annoying to type git cherry-pick every time? Git cherry-pick is a useful command for copying a single commit from one branch to another. You can create an alias like this to save time:

git config --global alias.cp "cherry-pick"

Now you can use git cp <commit-hash> to cherry-pick a commit without typing the full command.

4. What is the Current Branch?

You're in the middle of something and can't remember which branch you're on. No problem! You can create an alias to quickly check the current branch ("bn" stands for "branch name"):

git config --global alias.bn "branch --show-current"

Now you can use git bn to display the name of the current branch.

5. What's the Status?

Checking the status of your repository is a common task in Git. You can create an alias to simplify this command:

git config --global alias.st "status"

Now you can use git st instead of git status to see the current status of your repository.

6. Git Please

One of the most useful Git aliases is git please, which adds the --force-with-lease flag to the git push command. This flag ensures that you don't accidentally overwrite changes on the remote repository that you're not aware of. It's a safer alternative to the --force flag, which can lead to data loss if used incorrectly.

To set up the git please alias, run the following command:

git config --global alias.please "push --force-with-lease"

Now you can use git please instead of git push --force-with-lease to push your changes to the remote repository.

7. In case of Emergency

You did a lot of changes but suddenly the fire alarm goes off. You need to leave the office immediately but don't want to lose your work in a burned-down building. You can create an alias to save your changes in a remote branch quickly:

git config --global alias.fire '!git add . && git checkout -b emergency && git commit -m "fire" && git push -u origin emergency'

This alias creates a new branch named emergency, adds all changes, commits them with the message "fire", and pushes the branch to the remote repository. You can use it by typing git fire in your terminal. I've also seen people renaming this alias to "git friday" to save their work before leaving the office immediately on Friday to go into the weekend 🤫

8. Git graph

Visualizing the commit history of your repository can be helpful for understanding the project's development over time. You can create an alias to display a concise graph of the commit history:

git config --global alias.graph "log --oneline --all --graph --decorate"

Now you can use git graph to see a compact graph of the commit history with commit messages and branch names.

9. Git log

Sometimes "git log" is too verbose and you want to see a more concise version of the commit history. You can create an alias to display a simplified log with one line per commit:

git config --global alias.lg "log --oneline --decorate"

Now you can use git lg to see a condensed version of the commit history with commit hashes and decorations.

10. Git undo

Mistakes happen, and Git provides several ways to undo them. You can create an alias to simplify the process of undoing the last commit:

git config --global alias.undo "reset HEAD~1 --mixed"

Now you can use git undo to undo the last commit while keeping the changes in your working directory.

Conclusion

With these 10 Git aliases in your toolbox, you can supercharge your workflow and work more efficiently with Git. By creating custom shortcuts for common commands, you can save time and reduce the cognitive load associated with remembering complex syntax. Experiment with these aliases and customize them to fit your workflow. Happy coding!