Git aliases are a surprisingly powerful way to shave seconds off your daily workflow, and those seconds add up to minutes, then hours.

Let’s see this in action. Imagine you’re constantly typing git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit. That’s a mouthful. We can turn this into a simple alias.

First, you’ll want to set up your Git configuration. This is usually done in ~/.gitconfig for global settings or .git/config for repository-specific settings. For a global alias, you’d add a section like this:

[alias]
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Now, instead of typing that monster command, you can simply run git lg. This is the core idea: replacing verbose, repetitive commands with short, memorable aliases.

But aliases aren’t just for git log. They can be used for any Git command, including complex sequences. Think about common tasks you perform. For instance, stashing changes, pulling, and then reapplying stashed changes is a frequent operation for many developers. You can create a single alias for this:

[alias]
    psa = "!git stash && git pull && git stash pop"

This alias, psa (pull, stash, apply), executes three commands in sequence. The ! prefix tells Git to run the following string as a shell command. This is where the real power comes in – you can chain commands, pipe output, and even use shell logic.

The mental model is simple: Git aliases are shortcuts defined in your configuration files that map a short string to a longer Git command or a shell command. They don’t change how Git fundamentally works; they just change how you interact with it. This means you can tailor Git to your specific needs and preferences, making your daily development tasks more efficient.

Consider the problem of frequently switching between branches and then immediately wanting to see the commit history of that new branch. A common pattern might be git checkout <branch-name> followed by git log. You can alias this:

[alias]
    co = checkout
    cob = "!f() { git checkout $1 && git log --oneline --graph; }; f"

Here, co is a simple alias for checkout. The cob alias is more advanced. It defines a shell function f that takes an argument $1 (the branch name), checks it out, and then displays a concise log. The f() and ; f part is a common shell idiom for defining and immediately executing a function. Now, git cob main will switch to the main branch and show you its history.

Many developers struggle with remembering the exact flags for common operations. For example, creating a new branch and immediately switching to it is git checkout -b <new-branch-name>. An alias can simplify this:

[alias]
    cb = "!f() { git checkout -b $1; }; f"

Now, git cb feature/new-thing does the same job. This might seem trivial, but when you’re doing this dozens of times a day across different projects, the cumulative time saved is significant.

The real magic of aliases is that they can abstract away complexity. Imagine you have a complex workflow for deploying to a staging environment: fetching the latest changes, running tests, building the application, and then deploying. You can encapsulate this entire process into a single alias.

[alias]
    deploy-staging = "!echo 'Deploying to staging...' && git fetch origin && git checkout main && git pull origin main && echo 'Running tests...' && npm run test && echo 'Building application...' && npm run build && echo 'Deploying...' && scp -r ./dist/* user@staging.server:/var/www/html/ && echo 'Deployment complete!'"

This alias is long, but it represents a significant reduction in manual steps. It uses echo for feedback, && to chain commands only if the previous one succeeds, and even includes scp for file transfer.

One thing many people overlook is that aliases can also be used to override Git commands with safer versions. For example, if you’re prone to accidentally force-pushing, you could alias push to something that requires an extra confirmation or prevents force-pushes by default.

[alias]
    push = "!git push --force-with-lease"

This alias replaces git push with git push --force-with-lease. While still a force push, --force-with-lease is significantly safer than a bare --force because it checks if the remote branch has been updated by someone else since your last fetch, preventing you from overwriting their work unintentionally.

As you become more comfortable, you might even start creating aliases that dynamically generate commands based on context, though that quickly ventures into shell scripting territory within your Git config. The key takeaway is that aliases are a highly personalizable aspect of Git, allowing you to mold the tool to your exact workflow.

The next step after mastering aliases is likely exploring Git hooks, which allow you to automate actions at specific points in the Git lifecycle.

Want structured learning?

Take the full Git course →