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.
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.
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.
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.
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.
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
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.