Great Git alias

Git is a very powerful tool. It can track all the code you write, let you organize your work into different branches, help you work seamlessly with other developers, and even let you time travel and make changes.

But if Git can do it, wouldn’t it be great? more? What if you can customize it with your own commands and let it do anything you can imagine?

In this three-part series, I will show you how to do this. You will learn how to create a custom Git alias that can be used to run any command you like. I will also show you many useful commands that can be added to Git to make it more useful.

This article is part of the No Bullshit Git free introduction course.If you are interested in Git and want to take other courses, be sure Sign up Free courses.

How to alias commands

Before we go to any future, as long as What is an alias? Aliases are a shortcut to run another Git command. You can set the alias like any other configuration value.

For example, let’s say you often make typos git status As git stats,you want git stats Run the same command.You can set alias.stats value status like this.

git config --global alias.stats status

prosperity! That’s it, you have created an alias.

Just like other configuration values, you can also open your ~/.gitconfig File and add aliases there. This does the same thing as the command above.

  stats = status

A shortcut

You may be using Git every day.Command like git status, git add with git commit Is awesome.

But uh. All typing. Who has time to do this?

Some of the most useful aliases you can set up for Git are one or two character shortcuts for commonly used commands.

  a = add
  b = branch
  c = commit
  cl = clone
  co = checkout
  cp = cherry-pick
  m = merge
  p = push --follow-tags
  pu = pull
  r = reset
  s = status 

These aliases can save a lot of time.Instead of typing git status, You only need to enter git sInstead of git add ., You can enter git a .This may sound trivial, but give it a try and I promise you won’t go back and type the complete word.

Forced push

Sometimes, you need to push changes to a remote repository and overwrite files.You may be accustomed to doing this git push --force.

But what happens if someone else has already pushed changes to the same branch? Your order will clear their submission. That’s not good.

Git has a safer way to push changes and overwrite your commits.Instead of using --force Logo you can use --force-with-lease. This flag will prevent you from accidentally overwriting other people’s submissions. This way of working is a bit magical, but you can believe it does.

You can write one force-push Alias:

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

Now you can run git force-push, Which is easier to remember. Of course, in order to save a few keystrokes, you can also add a short version of this alias.

git config --global alias.fp force-push

Please note that you can only set aliases for aliases in Git 2.20+.

Shell alias

You can use another powerful addition to Git aliases: shell aliases.If you add a ! You can run any shell command before the value of the alias.For example, you can add the following alias to print it out hello Every time you type git hello.

git config --global alias.hello "!echo hello"


You know them, you love them-Dad’s joke.When you accidentally type a typo git add As git dad, Git responds with a dad’s joke is perfect.

Note: I did not come up with this idea.The earliest reference to this idea I can find is Post on Reddit.

git config --global '!curl && echo'

Now why you accidentally type git dad, You will get such gems.

Why is the robot angry? Because someone keeps pressing his button!


That’s it so far

That is to keep this article to the end! I hope you have learned how to set up your own aliases and found that some of the aliases I have included are useful.

In the next part of this series, we will delve deeper into building custom Git commands that speed up the workflow. Until then, have fun!

If you like this article and want to learn more about Git, be sure to check No nonsense Git free introduction course.

Leave a Reply

Your email address will not be published. Required fields are marked *