Git is a powerful tool, and you cannot call yourself a modern software engineer if you are not using Git or any other subversion system unless you are an old school guy who writes code alone and shares it in a zip file. Trust me!! Those guys still exist in my local village.
If you are reading this, it means you have the internet, and you use git at work or for personal projects, and sometimes you need to make customizations to make your life easier.
In this blog post, I would like to share with you four custom Git settings I found, and that may be useful for you.
The problem
You are working on a project, and the team has a standard commit message template you have to use for your commits, or like me, you don't like writing your commit messages in the default git editor, Vim. I had headaches while trying to quit Vim; once I have finished editing my commit message.
The solution
If you have ever faced one of the problems above, Git comes to your rescue with git config
where you can customize your default settings.
Configuration levels
You can customize your config on three levels :
- System-wide level: It means you apply your customs settings for all users on your laptop. Settings on system level are not essential for us unless you are sharing your computer with friends and family or working on a school computer where every student has his account on one laptop.
Any settings on system-level will have --system
argument.
git config --system
- Global Level or User level: this is specific to one user, and the settings are for you, and you share them across all your repositories.
On this level, the settings are followed with
--global
argument.
git config --global
- Repository Level or Local level: this only applied to the git repository you are working on. Setting global configuration is useful for settings on the project level when you want to follow a certain standard for the project.
For this, you use the --local
argument.
git config --local
Customs useful configurations
Once we know all levels for git configuration, let see which settings you can set up.
Tell Git who you are
Like in real-world relationships, once you meet a new person, you have to introduce yourself. The same applies to Git; before the first commit, you have to set up your username and email.
There comes user.name, and user.email config that needs to be made globally here are the commands:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Side note:
As said here If you use Github only in your laptop, you can set up your email like this :
git config --global user.email ":USERNAME@users.noreply.github.com"
It will still match your contributions correctly based on the no-reply address, but it won't include your real email address to in public commits, meaning that all those spams scripts won't pick you up and bother yours on your primary email address.
But if you use bitBucket, Gitlab and any other subversion system you can set up the email local per project.
Enjoy the Joy of writing your commit messages in your favorite editor
By default, Git uses Vim as the text editor for commits messages, rebase and merge messages.
If like me you have never used Vim because you find it challenging to use, the next core.editor
the command comes to your rescue.
git config --global core.editor "code -w"
With this command, you can tell Git to use a text editor installed on your laptop which is available via command line.
To make this working your editor should be accessible via the command line, you may want to check before by running which editor name
.
For example:
-
which code
, for everyone who started to code after 2016, which I guess VSCode is the default code editor -
which nano
orwhich subl
for old school guys -
which vim
for our grandparents in code
Note the -w argument after the editor name :
That force Git to wait for your commit message that is typing type on your custom editor otherwise Git will close and assuming there is an empty message. Thanks to this StackOverflow answer
All commits should wear the same uniform:
Source : national-education-DRC
If you work on a structured project that has the same template for commit message the commit.template
a command can help.
git config --local commit.template ~/.commit-message.md
The argument for this command is the path to your commit-message template, it can be any text file, I prefer using markdown for formatting purposes but you can also use a text file.
I prefer setting this locally on projects and having one globally for my personal projects.
They don't deserve to be on Git
You may be familiar with the .gitignore file on projects.
Sometimes some files don't deserve to be on Git, instead of ignoring them for a project you ignore them globally using a global template for Git ignore with the core.excludesfile
command
git config --global core.excludesfile ~/.gitignore_global
Conclusion
Live is already complicated; we should not let Git add another level of complication in it.
I hope those settings will helps you to make life more comfortable when using Git.
If you have other customs configurations you would like to share with us, please drop them in comments.
Top comments (2)
Very interesting post-Espoir.
i did not know that i could do more appealing stuff with git config.
Thank you.
Good post @espoir , this really inspires me to customize git as I want, and to avoid those repetitive commands.... good stuff