Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BolderLearnerTechSchool/0086026d177af7320a8fa3e707fb3f30 to your computer and use it in GitHub Desktop.
Save BolderLearnerTechSchool/0086026d177af7320a8fa3e707fb3f30 to your computer and use it in GitHub Desktop.
Configure Your Computer To Use More Than One GitHub Account

Configure Your Computer To Use More Than One GitHub Account

thinking

Photo by Dziana Hasanbekava

Table of Contents

You have a laptop, and you have been using git and GitHub (you have an account there). Somehow, from your own exploits or from school/work projects, you realized that you need to work with more than one GitHub account. Your school, or your workplace, expects you to participate in the development of a project they own. You do not want to mix your contributions and push your local changes to the wrong account. As you probably know, you cannot push your work to an account you have no access to. This is because GitHub will use your SSH key, and only one SSH key can be used per account. What can you do?

The assumption here is that you already know how to configure your local machine to work with git and GitHub. If not, consult the official GitHub Documentation, else, ask your teacher for a broken-down step-by-step guide. In this guide, we shall maintain the use of SSH to access our GitHub accounts.

If your school/workplace has an existing repository, they probably will issue you with their own email address. Alternatively, if you are trying this on your own, you would want to create a new GitHub account using a different email address (other than your existing one). How do you configure your computer to push and pull from different accounts?

Step 1: Create A New SSH Key

In your terminal, run the following command (you can use the built-in terminal interface or your text editor's. The examples below will be shown using VS Code's terminal):

ssh-keygen -t rsa -b 4096 -C "your-email-address"

You need to replace the text "your-email-address" with the email address associated with the new GitHub account. If the account belongs to your school or your workplace, you need to have access to the email you were issued. If it is your new/different email address, then use it here.

new_ssh_key2

A new private/public key pair will be generated for you. During the creation of the new key pair, you will be asked to enter a file in which to save it. See the image below.

question_to_save_key_pair2

The default location/file (as you can see in the image above) is /home/harry/.ssh/id_rsa (the name harry will change in your case to reflect your computer).

Before pressing Enter on your keyboard, be sure to clearly name your new file. Simply copy the file path in brackets and replace it by appending the text "my_school" (give your school name) or "my_work" (give the name of your workplace)

update_file_name

You will be asked to enter a passphrase. The passphrase should be something simple that you can easily remember and one that a bot or a computer cannot easily recognize. Alternatively, you can choose to leave it blank and press Enter on your keyboard.

passphrase

For security purposes, you will be asked to re-enter the passphrase again.

passphrase2

The key fingerprint will be as follows:

key_generated

Run the command ls -al ~/.ssh/ to see the listed files. You should see two files bearing the name you gave above (in my case, it was "my_school_test").

listed_key_files2

It is important to note that the files id_rsa and id_rsa.pub host the keys to your original GitHub account. That is why it was crucial for us to create a new file specifically associated with the new/different account we want to configure.

Finally, we need to copy the key we have just generated. To do so, we begin by running the following command (the cat command is used to display a file's content on the terminal):

ssh_key

Copy the key seen from ssh-rsa AAAA... until where you can see your email address ...qw== [email protected]. Be careful not to include any empty spaces while copying.

Step 2: Attach The New Key To Your Account

The next step is to attach the contents in our clipboard (what we have copied) to the new GitHub account. Visit the link https://github.com/settings/ssh/new. This resource will display a form to add your new SSH key. The form has the fields Title and Key.

  • In the Title input box, feel free to give it the name of your computer (since it is the one with the SSH Key).
  • In the Key input box, paste the contents of your clipboard.

ssh_form

Once done, press the green "Add SSH Key" button.

Step 3: Add Your New SSH Key To The Agent

We need to tell SSH about our new key. This key exists in a new file called ~/.ssh/id_rsa_my_school_test. In your terminal, type:

ssh-add id_rsa_my_school_test # Change this to the name of your file

add_identity

If all goes well, you should see Identity Added: ....

Step 4: Create A Config File

With the bulk of the workload out of the way now, it is time to specify to what account we should push or pull. The first step would be to create a new file called config within the ~/.ssh/ subfolder.

create_config_file

The terminal command touch has been used to create an empty file. As it is right now, the config file has nothing in it. If you are curious to know if indeed this file has been created, you can run this command to see all the existing files inside ~/.ssh/:

config_listed

Step 5: Update The Config File

We shall use the terminal editor nano to make changes to the config file.

open_config_using_nano

Should you not have nano, consider installing it as follows:

# Update your package lists:
sudo apt update

# Install Nano
sudo apt install nano

# Enter your password and press Y to confirm

# Verify the installation: 
nano --version

Once you run sudo nano ~/.ssh/config, an empty config file will open in your terminal. Update it by typing the following (nano uses the keyboard arrows for navigation):

# Default (personal)
Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa

# School (or work or other)
Host github.com-my-school-test
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_my_school_test

Press Ctrl + X and then press Y (for 'yes') on your keyboard to save your changes and finally press enter to complete the process.

Understanding The Config File

The first part of the config file points to our default GitHub account. In the example above, it is a personal account. The host is github.com. The second part can be the account associated with your school or workplace or other. Note that the host has changed to github.com-my-school-test.

While everything else remains the same, the IdentifyFile for my personal account is ~/.ssh/id_rsa while that of my secondary account also changes to ~/.ssh/id_rsa_my_school_test.

These differences will help the SSH agent to properly identify which accounts you are to work with.

Step 6: Try Out The New Account

Now, we can try to push a new project to the secondary account. Create a new project (it can even be a basic README.md file). Once you are ready to push to GitHub, head over to your favorite browser and access your default/original account.

default_profile

From the top-right, you can switch between your accounts. Since we want to push to our new account, be sure that you have logged into it.

Create a new repository as usual. Make sure that you have selected the SSH option during setup.

new_repo

As usual, run the command seen to get your brand new project to GitHub up until git branch .... These are the commands you need to run:

git init
git add README.md
git commit -m "first commit"
git branch -M main

# Don't add the remaining two just yet

To complete the pushing, you need to take caution to use the second Host instead of the default Host. Instead of:

git remote add origin git@github.com:BolderLearnerTechSchool/test.git

You need this:

git remote add origin git@github.com-my-school-test:BolderLearnerTechSchool/test.git

If you cannot tell the difference, see the highlighted section of the image below:

update_host_before_pushing

Use github.com-my-school-test instead of github.com. This applies during pushing, pulling, cloning, etc.

Finally, you can run:

git push -u origin main

Return to GitHub on your secondary account, refresh your page and you should see the repo updated. Subsequent changes on the repo do not require you to explicityly specify the the path to the secondary account. Make changes, commit and push normally.

Author And Committer

Before diving into the procedures, it’s important to distinguish between an "author" and a "committer":

  • Author: The person who originally wrote the code.
  • Committer: The person who last applied the code to the repository, which could be different in cases of patches or collaborative projects.

Git Configuration Levels

There are three levels to git configurations:

  • local (--local): specific to a particular repository and overrides system and global settings. These configurations are stored in .git/config within the repository itself.
    • Use local configurations for settings that should only apply to a specific project, such as project-specific user names, email addresses, merge strategies, or Git hooks.
    • For example:
      git config --local core.editor nano
      # sets nano as the repo's main text editor
  • global (--global): applies to every repository the current user works with on the machine, with settings usually found in the user’s home directory, for example, ~/.gitconfig or ~/.config/git/config.
    • It is ideal for settings like your user name and email, which are likely to be consistent across all your Git activities. Also used for defining global ignore patterns and setting up aliases you want available in every project.
    • For example:
      git config --global user.email "[email protected]"
      # This sets your email address for all your repositories
  • system (--system): affects all users on a machine and all their repositories; settings are typically stored in a configuration file accessible system-wide, such as /etc/gitconfig.
    • System-level configurations might include settings related to system-wide Git behavior, security configurations, or network settings. This is particularly useful in organizational contexts where many users operate on the same system.
    • For example:
      sudo git config --system core.autocrlf false
      # This disables automatic conversion of line endings for every user on the system

The reason why harry is the author of the commits above (see this link for the log of git commits) is because my machine has him configured on a global level, and by default, is applied in this repository too. But, we want to change that.

Prioritization Of Configurations

Git prioritizes configurations at the local level over the global level, and global over system, meaning that if the same configuration is specified at multiple levels, the more specific level takes precedence. For example, a user.email setting in the local configuration of a repository will override the global configuration.

Update Author

After making multiple changes to the repo, run this command:

git log

This command is used to track a branch's commit history. After running, it will display project-related information, such as a detailed commit history, which can be used as a reference to go back in time to a previous version of the project. In my case, this is what I saw:

git_log

The author above is harry instead of BolderLearnerTechSchool. harry is the default user configured for my computer and my original account, but now, if I want to author a commit as BolderLearnerTechSchool, I have to re-configure my git.

Amend The Author Of The Most Recent Commit

To change the author information of the most recent commit, run:

git commit --amend --author="New Author Name <[email protected]>" 
# remember to add the angle brackets (<>) around your email

most_recent_commit_edit_author

This command will open your default text editor to edit the commit message, and it replaces the author details with the new ones specified. Press Ctrl + X to save the change. Now, if we try to run git log again, you will notice that the new author of the most recent commit is BolderLearnerTechSchool.

updated_recent_commit

Amend The Author Of A Specific Commit

  • To change the author of a specific commit that isn't the most recent, you'll need to use an interactive rebase:

    git rebase -i <commit-id>

    Replace <commit-id> with the hash of the commit just before the one you want to amend.

  • In the text editor that opens, change the word pick to edit next to the commit you want to amend, then save and close the editor.

  • Amend the commit:

    git commit --amend --author="New Author Name <[email protected]>"
  • Continue the rebase:

    git rebase --continue

Configure All Commits To Be Authored By Someone Else

As wonderful as the solution above is, it is cumbersome to constantly be updating each commit to reflect a particular author. As we have learnt above, the ideal level of our configuration in this particular repo/project is local often denoted by the flag --local.

Up until now, the author and committer of the changes in the test repository has been harry. It is time to make BolderLearnerTechSchool both the author and committer of subsequent changes to this directory/repository. Let us set up our user name and user email. Run:

git config --local user.name "BolderLearnerTechSchool"
git config --local user.email "[email protected]"

These will set our new user as the default author of everything happening in this repo/directory. Our global configurations will be overriden. Running git log will show that the changes made above have been effected.

local_config_applied

Push the local changes in the directory to your GitHub account and refresh the repo to see the change in user and authorship.

seen_on_github

Any further updates to your project will show the new user as the author of all commits made in this repository.


If you found this content useful, please leave us a star.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment