Git-Memo0-Config on New Server and Stash

First Post:

Last Update:

Word Count:
972

Read Time:
6 min

Page View: loading...


0. Preface

When developing on a shared Team server, we often face two core challenges:

  1. Multi-Account & Privacy Management: How to configure your own SSH Key without overwriting the server’s default Key, ensuring you can clone your Private projects, and that Commit records accurately show your account (instead of root or others).
  2. Temporary Code Backup: When you have a large number of unfinished changes (Stash) locally that need to be saved, or need to transfer these temporary changes to another machine, what is the safest way to do so?

This article will use the IsaacGHX account as an example to provide a set of Git best practice configuration guides in a shared environment.


1. Configure SSH Key (IsaacGHX Example)

Before configuring the SSH Key, be sure to check if a Key already exists to prevent overwriting someone else’s configuration (especially on a shared server).

Step 1: Check Existing Keys

Run in the terminal:

1
ls -al ~/.ssh

If you see files like id_rsa or id_ed25519, it means a Key already exists.
Note: If there is already someone else’s Key on the server, DO NOT run ssh-keygen directly to overwrite the default file. Instead, you should generate a new filename.

Step 2: Generate a New SSH Key

Generate a new Key using your email. To avoid overwriting, we can specify a filename.

1
2
# Replace with your email
ssh-keygen -t ed25519 -C "124448943+IsaacGHX@users.noreply.github.com" -f ~/.ssh/id_ed25519_IsaacGHX
  • -C: Comment, usually filled with email.
  • -f: Specify filename to avoid overwriting the default id_ed25519.

Step 3: Configure SSH Config (Multi-Key Management)

To let Git know which Key to use when connecting to GitHub, you need to configure the ~/.ssh/config file.

1
nano ~/.ssh/config

Add the following content:

1
2
3
4
5
6
# IsaacGHX GitHub Account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_IsaacGHX
IdentitiesOnly yes

Step 4: Copy Public Key to GitHub

Display public key content:

1
cat ~/.ssh/id_ed25519_IsaacGHX.pub

Copy the output (starting with ssh-ed25519), then:

  1. Open GitHub -> Settings -> SSH and GPG keys.
  2. Click New SSH key.
  3. Title fill in “IsaacGHX Laptop” (example), paste the content just copied into Key.
  4. Click Add SSH key.

2. Verify SSH Connection

After configuration is complete, verify if you can successfully connect to GitHub.

1
ssh -T git@github.com

If successful, you will see a message like:

Hi IsaacGHX! You’ve successfully authenticated, but GitHub does not provide shell access.

🔴 Common Issue: Showing Someone Else’s Name?

If the output shows not IsaacGHX, but someone else’s name (e.g., Hi OtherUser!), it means SSH is still using the default Key on the server.

Solution 1: Use Host Alias (Recommended, Safest)

Modify ~/.ssh/config and change Host github.com to an alias, for example Host github-isaac:

1
2
3
4
5
6
7
8
9
10
# Before modification
Host github.com
...

# After modification
Host github-isaac
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_IsaacGHX
IdentitiesOnly yes

Then test the connection:

1
ssh -T git@github-isaac

Note: When cloning projects in the future, the address must also be changed:
git clone git@github-isaac:IsaacGHX/MyProject.git

Solution 2: Adjust Config Order

If you insist on using github.com as the Host, you need to ensure your configuration block is at the very top of the ~/.ssh/config file. SSH will use the first match it finds.
Open the config file, cut your configuration block and paste it to the top of the file.

🔴 Common Issue: Fixing SSH Permissions Error

If you encounter the following error when running ssh -T:

1
Bad owner or permissions on /root/.ssh/config

This means the permissions of the ~/.ssh/config file are too open. SSH requires strict permissions.

Solution:

Run the following command to fix it:

1
chmod 600 ~/.ssh/config

3. Clone Project Using SSH

After configuring SSH, use the SSH URL instead of the HTTPS URL when cloning projects.

1
2
# Format: git clone git@github.com:username/repo_name.git
git clone git@github.com:IsaacGHX/MyProject.git

If you previously cloned using HTTPS, you can modify the remote url:

1
2
cd MyProject
git remote set-url origin git@github.com:IsaacGHX/MyProject.git

4. Configure Git User Info (Avatar Display)

In order for GitHub commit records to correctly display your avatar and link, the local Git email must match the email bound to your GitHub account.

1
2
3
4
5
6
7
8
# Enter project directory
cd MyProject

# Configure username
git config user.name "IsaacGHX"

# Configure email (Critical! Must match GitHub account)
git config user.email "124448943+IsaacGHX@users.noreply.github.com"

💡 How to find this numeric ID?

  1. Method A (Simple): Visit https://api.github.com/users/YOUR_USERNAME (e.g., https://api.github.com/users/IsaacGHX) and look for the "id": 124448943 field.
  2. Method B (Settings): In GitHub Settings -> Emails, check “Keep my email addresses private”, and GitHub will directly display your noreply email address.

Verify Configuration:

1
2
3
4
5
git config user.name
# Output: IsaacGHX

git config user.email
# Output: 124448943+IsaacGHX@users.noreply.github.com

This way, the pushed commits will show IsaacGHX’s avatar.


5. Git Stash (Temporarily Save Changes)

When you are developing feature A and suddenly need to fix a Bug, but don’t want to commit the current unfinished code, use Stash.

1. Stash Current Changes

1
2
3
4
5
# Simple stash
git stash

# Recommended: Add a message for easy finding later
git stash push -m "IsaacGHX: dev feature A pending"

2. View Stash List

1
git stash list

Output example:
stash@{0}: On main: IsaacGHX: dev feature A pending

3. Restore Stash

Scenario 1: Restore and Keep Stash (Recommended)
If you want to apply changes but keep a backup in the stash list:

1
git stash apply stash@{0}

Scenario 2: Restore and Delete Stash
If you are sure you no longer need this stash:

1
git stash pop stash@{0}

4. Delete Stash

1
2
3
4
5
# Delete specific stash
git stash drop stash@{0}

# Clear all stashes (Use with caution)
git stash clear
If you find this helpful, please give my project a ⭐on GitHub! =w=/