Senger CodeLab πŸš€

How to save username and password with Mercurial

September 29, 2026

πŸ“‚ Categories: Programming
🏷 Tags: Mercurial Hgrc
How to save username and password with Mercurial

Remembering usernames and passwords for various online services can be a real hassle, especially for developers juggling multiple repositories. If you’re using Mercurial, a distributed version control system, repeatedly entering your credentials can disrupt your workflow. Thankfully, Mercurial offers several ways to securely store your authentication details, allowing for seamless interaction with your repositories. This article dives into the most effective methods for saving your username and password with Mercurial, streamlining your development process and boosting your productivity.

Storing Credentials in the hgrc File

The most common method for saving your Mercurial credentials involves configuring the .hgrc file. This file, located in your home directory, stores your global Mercurial settings. You can specify your username and password for specific repositories or even set default credentials for all your Mercurial interactions. This centralized approach simplifies authentication and reduces the need for constant login prompts.

To implement this, open your .hgrc file with a text editor and add the following lines under the [auth] section, replacing <REPO_URL> with the URL of your repository and <USERNAME> and <PASSWORD> with your actual credentials:

[auth] <REPO_URL>.username = <USERNAME> <REPO_URL>.password = <PASSWORD> 

This method provides a persistent solution, but remember to protect your .hgrc file’s permissions to ensure the security of your stored credentials.

Using the keyring Extension

For enhanced security, consider using the keyring extension. This extension integrates with your operating system’s secure credential storage, providing a more robust way to manage your Mercurial authentication details. By leveraging the keyring, your passwords are encrypted and stored outside of the .hgrc file, minimizing the risk of unauthorized access. This method is generally recommended for better security practices.

Installing and Configuring keyring

To use the keyring extension, you’ll first need to install it. You can typically do this using pip install mercurial-keyring. Once installed, enable the extension by adding the following line to your .hgrc file:

[extensions] keyring = 

After enabling the extension, Mercurial will automatically prompt you to store your credentials in the keyring when you first interact with a repository. Your credentials will then be securely stored and automatically retrieved whenever you access the repository.

Leveraging SSH Keys for Authentication

If you’re working with repositories hosted on servers that support SSH, using SSH keys offers a highly secure and convenient alternative to password-based authentication. With SSH keys, you generate a key pair – a private key that remains on your machine and a public key that you add to your server’s authorized keys list. This method eliminates the need to store passwords altogether.

This approach enhances security as your private key never leaves your system. Moreover, it streamlines workflows as you won’t need to enter a password each time you interact with the repository. For detailed instructions on generating and configuring SSH keys, consult your server’s documentation.

Using Environment Variables

You can also leverage environment variables to temporarily store your Mercurial credentials. This method is particularly useful in scripting or automated environments where storing credentials in configuration files might not be ideal. By setting the HGUSERNAME and HGPASSWORD environment variables, you can instruct Mercurial to use these values for authentication.

While convenient for scripting, be cautious about storing sensitive credentials directly in environment variables as they can be exposed in certain scenarios. For example:

export HGUSERNAME=<USERNAME> export HGPASSWORD=<PASSWORD> 

Storing your Mercurial username and password securely is crucial for protecting your code and maintaining efficient workflows. The methods outlined above provide various approaches, catering to different security preferences and use cases. Explore these options to find the best fit for your needs. Consider factors like the sensitivity of your repositories and your development environment when making your choice. Properly securing your credentials is a key step in responsible development practices.

  • Prioritize security when choosing a credential storage method.
  • Regularly review and update your security practices.
  1. Identify the most appropriate method for your needs.
  2. Implement the chosen method carefully.
  3. Verify the functionality and security of your setup.

Looking for more ways to optimize your Mercurial workflow? Check out our guide on advanced Mercurial techniques.

Featured Snippet: The most secure way to store Mercurial credentials is by using the keyring extension or SSH keys. These methods encrypt your passwords or eliminate the need for them altogether, providing robust security for your repositories.

For further reading on Mercurial and security best practices, explore these resources:

[Infographic Placeholder]
Frequently Asked Questions --------------------------

Q: Can I store credentials for multiple repositories?

A: Yes, you can store credentials for multiple repositories in your .hgrc file or using the keyring extension.

Q: What if I forget my password?

A: If using the .hgrc file, you can directly edit it. If using the keyring, you’ll need to consult your operating system’s keyring manager to retrieve or reset your password.

By implementing these strategies, you can simplify your Mercurial workflow, enhance security, and improve your overall development experience. Remember to choose the method that best suits your needs and security requirements. Now, dive back into your coding and experience the benefits of secure and streamlined Mercurial authentication! Explore further and learn about managing multiple Mercurial accounts and integrating with various hosting platforms.

Question & Answer :
I used Mercurial in a personal project, and I have been typing my username and password every time I want to push something to the server.

I tried adding the following to the .hgrc file in my home directory, but it seems to be completely ignored.

[ui] username = MY_USER_NAME password = MY_PASSWORD 

How to do this the right way?

You can make an auth section in your .hgrc or Mercurial.ini file, like so:

[auth] bb.prefix = https://bitbucket.org/repo/path bb.username = foo bb.password = foo_passwd 

The β€˜bb’ part is an arbitrary identifier and is used to match prefix with username and password - handy for managing different username/password combos with different sites (prefix)

You can also only specify the user name, then you will just have to type your password when you push.

I would also recommend to take a look at the keyring extension. Because it stores the password in your system’s key ring instead of a plain text file, it is more secure. It is bundled with TortoiseHg on Windows, and there is currently a discussion about distributing it as a bundled extension on all platforms.