Git-Memo0-Config on New Server and Stash
Last Update:
Word Count:
Read Time:
Page View: loading...
0. Preface
When developing on a shared Team server, we often face two core challenges:
- 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).
- 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 | |
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 | |
-C: Comment, usually filled with email.-f: Specify filename to avoid overwriting the defaultid_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 | |
Add the following content:
1 | |
Step 4: Copy Public Key to GitHub
Display public key content:
1 | |
Copy the output (starting with ssh-ed25519), then:
- Open GitHub -> Settings -> SSH and GPG keys.
- Click New SSH key.
- Title fill in “IsaacGHX Laptop” (example), paste the content just copied into Key.
- Click Add SSH key.
2. Verify SSH Connection
After configuration is complete, verify if you can successfully connect to GitHub.
1 | |
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 | |
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 | |
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 | |
3. Clone Project Using SSH
After configuring SSH, use the SSH URL instead of the HTTPS URL when cloning projects.
1 | |
If you previously cloned using HTTPS, you can modify the remote url:
1 | |
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 | |
💡 How to find this numeric ID?
- Method A (Simple): Visit
https://api.github.com/users/YOUR_USERNAME(e.g., https://api.github.com/users/IsaacGHX) and look for the"id": 124448943field.- 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 | |
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. View Stash List
1 | |
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 | |
Scenario 2: Restore and Delete Stash
If you are sure you no longer need this stash:
1 | |
4. Delete Stash
1 | |