How I configure my git in a new computer

TL;DR: Add your user to your git config. My favourite aliases. I set up a gist to keep a .gitconfig file, and so should you.

Every now and then you change computers or you run you a new virtual machine/container. When you do so you lose most of your personal configurations for a lot of software. This forces you to work in suboptimal conditions or redo your configurations.

Recently I had to set up my git configuration and I thought I would share my experience with you.

First I set up my user credentials in the command line (github docs):

git config --global user.name "Firstname Lastname"
git config --global user.email "username@myEmail.com"

I did this because you need to have them in order to push your changes to github. I recommend you use the global flag to do it for your user in general and not just for one repository.

These commands create a .gitconfig file in your home directory and add a [user] section with the parameters name and email. Similarly, you can add more sections with more configurations. You can add [alias] to create shortcuts or just rename your favourite commands. Here is what I usually add:

The commands co, b, ci, st are self explanatory.

  • pu pushes your code to remote, and creates the branch if it doesn’t exist.
  • ap is the way you should always add your code to a commit. The patch option shows you your changes one by one, and asks you if you want to add them. This forces you to code review yourself in each commit, and makes it easier to create independent commits (sometimes I find myself implementing a whole feature at once, but splitting it in several commits to make it easier for others to understand my code, to make it easier to cherry-pick and even for me to understand what I just did).
  • rn allows you to rename branches, just like using the mv command in the shell to rename folders.

For example, if I am in a branch called feature/adds-fireball but I decide to implement a freeze ray, I rename it with the command:

git rn feature/adds-fireball feature/adds-freeze-ray

or if I am in the feature/adds-fireball branch, I just use:

git rn feature/adds-freeze-ray

Like the aliases, you can set your default editor for git commit messages, templates for your git messages, and more. You can read more about other .gitconfig options here.

After you set up your .gitconfig file, you can store it in a Github gist, for later reuse. A Gist is just some piece of code in the github cloud that you can keep private or share.

Let me know in the comments what aliases and configurations you use and why.

Edit 1: Thanks to Patrick Schlüter for pointing out the existance of git mv. Changed the alias to rn.