Git
How to Change Your Git Account in the Terminal?
Working with Git from the terminal is often essential for efficient development, but sometimes you may need to switch between multiple Git accounts. For instance, you might want to use separate accounts for personal and work projects or switch to a different GitHub account for a specific repository.
This post will walk you through the steps to change your Git account in the terminal, allowing you to seamlessly manage repositories under multiple profiles.
Why Would You Need to Switch Git Accounts?
Switching Git accounts can be helpful in several scenarios:
- Work vs. Personal Projects: When using different Git accounts for work and personal projects, switching lets you keep repositories organized.
- Contributing to Multiple Organizations: If you’re contributing to repositories owned by different organizations, you may need to switch accounts frequently.
- Managing Multiple GitHub or GitLab Accounts: GitHub and GitLab require separate SSH keys for each account to prevent conflicts.
Prerequisites
Before we get started, ensure:
- Git is installed: Check with
git --version
to confirm. - You have multiple accounts: You’ll need credentials (username, email, and SSH keys) for each account you want to use.
Method 1: Switching Accounts Using Global Configuration
If you only need to use one account at a time across all repositories, you can use Git’s global configuration to set a new account.
1.1 Update Git Config with New Account Details
Run the following commands to set your new account’s details as the global Git configuration. Replace [email protected]
and Your Name
with the appropriate email and username.
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
This will apply your new account details to all repositories on your local machine.
1.2 Verify the Changes
To verify the updated global configuration, run:
git config --global --list
You should see the updated user name and email.
Note: This will apply to all Git repositories on your local machine. If you only need to change the account for a specific repository, use Method 2 below.
Method 2: Setting a Specific Account per Repository
If you work on multiple projects and want each project to use a different Git account, you can configure Git at the repository level.
2.1 Navigate to the Repository Directory
First, navigate to the repository you want to change the Git account for:
cd /path/to/your-repository
2.2 Configure Local Git Account Settings
Run the following commands to set the account for this specific repository only:
git config user.email "[email protected]"
git config user.name "Your Name"
Git will now use this account for all commits made within this repository. This local configuration overrides the global configuration for this repository only.
2.3 Verify the Repository-Specific Configuration
To confirm the changes, run:
git config --list
Look for the user.name
and user.email
values associated with this repository.
Method 3: Switching Between Accounts Using SSH Keys
When working with multiple accounts on platforms like GitHub or GitLab, it’s often necessary to use different SSH keys for each account to authenticate your pushes and pulls securely.
3.1 Generate an SSH Key for Each Account
If you haven’t already created an SSH key for each account, run the following command. Replace [email protected]
with the email associated with the account:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
When prompted, save the key with a unique filename, such as id_rsa_work
or id_rsa_personal
.
3.2 Add the SSH Keys to the SSH Agent
Start the SSH agent and add both keys to it:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_work
ssh-add ~/.ssh/id_rsa_personal
3.3 Configure SSH Config File
Edit or create an SSH configuration file at ~/.ssh/config
and add entries for each Git account. Replace Host
, IdentityFile
, and User
as appropriate:
# Personal GitHub account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# Work GitHub account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
3.4 Setting the Remote URL for Each Repository
For each repository, set the remote URL to use the correct SSH key by specifying the host configured in the SSH config file (github-personal
or github-work
).
# For personal GitHub account
git remote set-url origin git@github-personal:username/repository.git
# For work GitHub account
git remote set-url origin git@github-work:username/repository.git
This configuration allows you to switch seamlessly between multiple accounts by associating each repository with its own SSH key.
Method 4: Using Environment Variables for Quick Switching
Environment variables can provide a quick way to switch accounts if you find yourself switching often.
4.1 Create Aliases in Your Shell Profile
Add aliases to your shell profile (e.g., .bashrc
or .zshrc
) for each account. For example:
alias git-work="git config user.email '[email protected]' && git config user.name 'Work Name'"
alias git-personal="git config user.email '[email protected]' && git config user.name 'Personal Name'"
4.2 Activate an Account with a Single Command
Run git-work
or git-personal
before committing to quickly switch accounts without affecting your global configuration.
Best Practices for Managing Multiple Git Accounts
- Use SSH Configurations: SSH configurations allow for seamless switching and are especially helpful for accessing multiple GitHub or GitLab accounts.
- Keep Commits Organized: Keep track of which account you’re using for each repository to avoid misattributing commits.
- Regularly Check Configurations: Run
git config --list
to verify account details before committing to ensure you’re using the right account. - Document Your Setup: Document your SSH configurations and aliases in a reference file to avoid confusion when switching accounts.
Troubleshooting Tips
- Permission Denied Errors: If you encounter
Permission denied (publickey)
, ensure the correct SSH key is added to the SSH agent and associated with your account on GitHub. - Remote Repository Errors: Double-check the remote URL in each repository with
git remote -v
to confirm it matches the expected SSH host. - Commit Attribution Errors: If commits are attributed to the wrong account, verify the
user.email
configuration in the repository and update if needed.
Conclusion
Switching between Git accounts in the terminal allows you to manage multiple profiles seamlessly, whether for work, personal projects, or organization contributions. With the methods outlined above, you can easily change accounts using global or repository-specific settings, SSH configurations, or environment variables. By following these steps and best practices, you’ll be able to efficiently switch Git accounts and keep your projects well-organized across multiple profiles.