As software developers, GIT is a useful tool in the course of developments. In most occasions, we do need to configure GIT in our local machines, either changing the existing configuration or setting up a new configuration. In this short article, I want to highlight some config commands that can come in handy in the two scenarios above.
Existing Configuration
If you have ever used someone else's machine or inherited one from a developer that already has a global configuration, then you would have experienced the nasty case of a strange username appearing in your commits when you push your code to the repository. It's not a funny experience right? Trust me it can be embarrassing.
To escape this ugly experience, open your terminal and follow the steps below:
git config --list
This lists the available configurations. You can also see the credentials of the existing user.
Next,
git config --global --replace-all user.name "New User Name e.g Jon Doe" \
git config --global --replace-all user.username "New username e.g jonDoe" \
git config --global --replace-all user.email "New User Email"
The commands above will replace the existing config user information with your own information.
Notice that the commands above use the --global option which sets the configuration globally on your machine, if you want to limit the configuration to a given repository, you can ignore the --global and the config will only apply to the repository where the command was invoked.
New Configuration
If your machine is new, you would want to configure GIT after installing it so that your contributions will reflect with your GIT information. The steps below are all you need to hit the ground running.
git config --global user.name "New User Name e.g Jon Doe" \
git config --global user.username "New username e.g jonDoe" \
git config --global user.email "New User Email"
Note: The commands above are using --global which sets the config globally. If you want the setting to take effect on a given repository, then, then replace the --global with --local.
credential.helper
Most times, it could be annoying when GIT requests for your authentication each time you want to push to the remote repository. One of the ways to stop that is through the credential.helper. All you need to do is to invoke the command below before pushing to the repository.
git config credential.helper store
After invoking the command above, go ahead and push. You might be required to authenticate but the subsequent pushes will not require authentication as GIT will fetch the credentials from the store.
More tips
Changing remote url
Sometimes, we want to change the remote url of a cloned repository or just any remote repository. The command below is all you need.
git remote set-url origin <new url>
To verify that the remote url is exactly the desired one, the command below solves it.
git remote -v
Conclusion
Topics about GIT are almost inexhaustible. As a result, we need to focus on the area that we need at some point in time. What I have written in this article is what I find myself surfing the internet each time I need them, hence decided to write about it so that others who find it useful may benefit from its handiness.
Feel free to add your own helpful tips in the comment section below.
Top comments (4)
Thanks Orie for sharing your git config tips! Didn't know about the
--replace-all
flag yet 👏Concerning safety it is important to mention, that
git config credential.helper store
stores your credentials unencrypted in plain text on your disk (under~/.git-credentials
). It is recommended using either git-credential-cache or a helper that handles encryption.Just saying for those with setups where
store
is an unacceptable security tradeoff 😇Great point Andreas, I didn't know about git-credential-cache before now. Thanks for sharing!
Thank you for sharing your tips with us!
I also use these two more commands when I install Git on a new machine:
git config --global core.editor vi
To be sure vi is my default editor for editing commit messages (instead of emacs).
git config --global help.autocorrect 1
To execute the right command when my fingers do a typo
Thanks for sharing. Those are quite useful tips.