Better Programming

Advice for programmers.

Follow publication

Super-Useful Git Aliases That Will Speed Up Your Coding

Zeev Katz
Better Programming
Published in
3 min readOct 1, 2020
One line of code.
Photo by the author.

The most effective way to understand the purpose of each alias is by testing them during the reading, so before we start, let’s go step by step and set them up on your machine:

  1. Copy the following content:

2. Open ~/.gitconfig for editing:

vi ~/.gitconfig

3. Paste the alias list under [alias]. In case you don’t see it, you can add it by yourself:

[alias]
st = status
s = switch
cp = cherry-pick
...

4. Save and exit.

You’ve now armed with aliases! Now let’s learn how we can use them and start firing up our terminal!

Shortened Aliases

This kind of group contains the simplest Git aliases that you can create. Their whole purpose is shortening long Git commands to something that’s easy to write and remember.

Let’s go over them and explain what they actually are under the hood.

st

Shortened alias for status.

amend

Amends the head commit without opening the message editor.

sw

Shortened alias for switch (added in Git v2.23).

cp(c/s/a)

Shortened alias for cherry-pick.

  • cpc — Used to continue after resolving conflicts during cherry-pick.
  • cps— Used to skip conflicts during cherry-pick.
  • cpa— Used to abort the current cherry-pick operation.

rb(c/s/a)

Shortened alias for rebase.

  • rbc — Used to continue after resolving conflicts during rebase.
  • rbs — Used to skip conflicts during rebase.
  • rba — Used to abort the current rebase operation.

ras

Restores all the current staged changes by using restore(added in Git v2.23).

puh

Pushes the current branch to the remote as a new branch.

wip

Commits the staged changes with a WIP message. Useful when switching between “in progress” branches.

Functional Aliases

This kind of group allows us to provide arguments to the aliases and benefit from the advantages of dynamic commands. For example, they could be really useful for aliases that need a specific commit or branch name to work with.

As you can see in the gist, the functional aliases start with !, which allows us to run shell commands. Then, we define a function that has our Git command and call this function immediately after its declaration (the last f). To use the provided arguments, you can access them as normal shell parameters ($1, $2, etc.).

Now let’s see how can we use each of them.

bm [branch_name]

Renames the current branch name with the provided new name.

fu [commit]

Takes the staged changes upon the provided commit without changing the commit message by using fixup.

fua [commit]

Same as fu, but it stages all the changes in the working directory automatically.

ir [number]

Starts interactive rebase from the last number of commits.

rs [file_path]

Restores the provided staged file by using restore (added in Git v2.23).

fr [branch_name]

Fetches from the remote the latest version of all branches and rebases the current branch onto the remote version of the provided branch name.

What’s Next?

As you can see, it's so easy to create new aliases in Git, so be creative and write your own aliases when you find yourself repeating long and hard-to-remember commands. Save your priceless time for the real tasks, such as design, coding, and reviews.

Always remember: The tools work for you, so don’t stop with Git aliases and try to improve your productivity with every tool you use in your daily work.

Thanks for reading!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Zeev Katz
Zeev Katz

Written by Zeev Katz

Frontend Leader at proteanTecs 👨‍💻 | ngze owner and leader 🚀

Responses (3)

Write a response