Skip to content

Latest commit

 

History

History
306 lines (269 loc) · 9.82 KB

SSH Setup for Business GitHub vs Personal Account.md

File metadata and controls

306 lines (269 loc) · 9.82 KB

Setting Up Git for Multiple GitHub Accounts (Personal & Business)

Table of Contents

Commands

git remote set-url

 git remote set-url origin [email protected]:company/your-repo-name.git

Step 1: Check SSH Keys on Your Machine

  1. Open your Terminal.
  2. Run the following command to check if there are any SSH keys already on your Mac:
    ls -al ~/.ssh

Step 2: Generate an SSH Key for Your Business Account

  1. Run the following command in Terminal to create a new SSH key:
    ssh-keygen -t ed25519 -C "[email protected]"
  2. When prompted, save the key as:
    /Users/amr.elghadban/.ssh/id_ed25519_business_amr_elghadban_adkatech
  3. Leave the passphrase empty (or add one for security).
  4. Result:
    Your identification has been saved in /Users/amr.elghadban/.ssh/id_ed25519_business_amr_elghadban_adkatech
    Your public key has been saved in /Users/amr.elghadban/.ssh/id_ed25519_business_amr_elghadban_adkatech.pub
    The key fingerprint is: 
    SHA256:4AAAAAAhXXXXXXXXXXDyK+M/L2bbbbSs [email protected]
    

Your identification has been saved in /Users/amr.elghadban/.ssh/id_ed25519_business_amr_elghadban_seddiqiholding Your public key has been saved in /Users/amr.elghadban/.ssh/id_ed25519_business_amr_elghadban_seddiqiholding.pub The key fingerprint is: SHA256:C/ngW1ybCnc7LLGMJDgPobbZ1u26a/60t4762wFgNac [email protected]

Step 3: Add the SSH Key to Your Business GitHub Account

  1. Copy the new public key to your clipboard:
    pbcopy < ~/.ssh/id_ed25519_business_amr_elghadban_adkatech.pub
  2. Go to your GitHub Business account:
    • Click your profile picture → Settings.
    • Navigate to SSH and GPG keys under Access.
    • Click New SSH key.
  3. Paste the public key into the "Key" field.
    • Give it a meaningful title (e.g., MacBook Pro Business Key).
    • Click Add SSH key.

Step 4: Configure the SSH Config File for Multiple Accounts

  1. Open the SSH config file:
    nano ~/.ssh/config
  2. If the file doesn't exist, create it:
    nano ~/.ssh/config
  3. Add the following configuration:
    # --- Personal GitHub Account ---
      Host github.aaakk.us.kg-personal
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519
      # UseKeychain yes
      # AddKeysToAgent yes
    # ----------------------------
    
    # --- Business GitHub Account ---
       Host github.aaakk.us.kg-business
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_ed25519_business_amr_elghadban_adkatech
    # ----------------------------
    
    
  4. Save the file:
    • Press Ctrl + O, then Enter.
    • Press Ctrl + X to exit.
  5. Set correct permissions:
    chmod 600 ~/.ssh/config

Step 5: Test the SSH Configuration

Run the following command to test the SSH connection:

Expected Output:

Hi AmrAhmedElghadban! You've successfully authenticated, but GitHub does not provide shell access.

Step 6: Clone the Repository Using the Business SSH Configuration

Clone Repositories Using Custom Host Aliases

  • (for new clone repo) Use the business SSH alias:
git clone [email protected]:<company>/<reponame>.git

Do not use:

git clone [email protected]:<company>/<reponame>.git

  • (for existing clone repo) Navigate to your cloned repository: Open your terminal and go to the root directory of your cloned repository:
cd /path/to/your/repo

Check the current remote URL: Run the following command to see the current remote URL:

git remote -v

This will show something like:

origin  [email protected]:<company>/<reponame>.git (fetch)
origin  [email protected]:<company>/<reponame>.git (push)

Update the remote URL to use your business SSH alias: Use the git remote set-url command to change the remote URL:

git remote set-url origin [email protected]:<company>/<reponame>.git

Verify the change: Run git remote -v again to confirm the URL has been updated:

git remote -v
You should now see:
origin  [email protected]:<company>/<reponame>.git (fetch)
origin  [email protected]:<company>/<reponame>.git (push)

Test the connection: To ensure everything is working, you can run:

git fetch

Step 7: Move to the Repository

  1. Decide where to move the repository:
    mv ~/POSRetail_Php /Users/amr.elghadban/Desktop/workspace/project_folder
  2. Verify the move:
    ls /Users/amr.elghadban/Desktop/workspace/project_folder
    You should see the project_folder folder.
  3. Navigate to the new location:
    cd /Users/amr.elghadban/Desktop/workspace/project_folder/repo

Step 8: Confirm Everything Works

  1. Check the Git status:
    git status
  2. Ensure the remote origin is set correctly:
    git remote -v
    Expected Output:
    origin  [email protected]:<company>/<reponame>.git (fetch)
    origin  [email protected]:<company>/<reponame>.git (push)
    

Step 9: Verify the Setup

Ensure everything is working as expected.


Step 10:-Optional-Configure Global Identity and Repository-specific Identity

Global Identity

  • Set the identity for global Git identity:
git config --global user.name "Your Personal Name"
git config --global user.email "[email protected]"
  • Confirm the settings:
git config --global --list

Repository-specific Identity (Business Repo)

  1. Navigate to the repository directory:
    cd /Users/amr.elghadban/Desktop/workspace/project_folder/repo
  2. Set the identity for this repository:
    git config user.name "Amr Ahmed Elghadban"
    git config user.email "[email protected]"
  3. Check the repository-specific configuration:
    git config --list --local

GitHub Contribution Graph can be combined by setting the Noreply Email to be the same in both Your Repository or Globally

Globally

   git config --global user.email "[email protected]"
   git config --global user.name "Amr Angry"  

Repository

  git config user.email "[email protected]"
  git config user.name "Amr Elghadban"

Rewrite Past Commits (Optional)

If previous commits were made with the wrong email, you can rewrite them with the correct noreply email:

Then, force-push the changes:

git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "[email protected]" ]; then
    export GIT_COMMITTER_EMAIL="[email protected]"
    export GIT_AUTHOR_EMAIL="[email protected]"
fi
' -- --all
 git push --force

(Optional) Use .gitconfig Includes for Easier Setup

Use .gitconfig includes to manage multiple identities more efficiently.

#Repo clone from GitHup Stackoverflow

Just simply follow these steps:

  • Create Personal Access Token on GitHub
    • Go to this link or From your GitHub account, go to Settings → Developer Settings → Personal Access Token → Tokens (classic) → Generate New Token (Give your password) → click Generate token → Copy the generated Token, it will be something like [ghp_sFhFsSHhTzMDreGRLjmks4Tzuzgthdvfsrta] [(don't go to repository setting; it's your profile setting)]
    • Generate a new token and copy-paste it somewhere safely.
    • Now search for an app in your Mac, named Keychain Access.
    • Search for github.com (if there are multiple GitHub logins then choose Kind: Internet password), double-click it.
    • Click on show password, then enter your Mac's password and hit Enter.
    • Past the token you generated in step 2 and click Save changes Or
    • Set Global config
$ git config --global user.name "your_github_username"
$ git config --global user.email "your_github_email"
$ git config -l
  • Now cache the given record in your computer to remembers the token:
$ git config --global credential.helper cache
  • If needed, anytime you can delete the cache record by:
$ git config --global --unset credential.helper
$ git config --system --unset credential.helper