git仓库基本使用

本文介绍了本地仓库和远程仓库的快速连接,下拉和上传代码。下面是用 SSH 公钥建立 Git 连接的一整套步骤(含 Windows/PowerShell 与 Git Bash 常用命令),按顺序做就行。


1) 检查是否已有密钥

bash 复制代码
# Git Bash / macOS / Linux
ls -l ~/.ssh

# Windows PowerShell
Get-ChildItem $env:USERPROFILE\.ssh

看到 id_ed25519/id_ed25519.pub 就说明已有密钥(可跳到步骤 3)。


2) 生成密钥(推荐 ed25519)

Git Bash / macOS / Linux:

bash 复制代码
ssh-keygen -t ed25519 -C "你的邮箱@example.com"
# 一路回车;可选设置 passphrase 保护私钥

Windows PowerShell:

powershell 复制代码
ssh-keygen -t ed25519 -C "你的邮箱@example.com" -f $env:USERPROFILE\.ssh\id_ed25519

生成后会得到:

私钥:~/.ssh/id_ed25519不要外传

公钥:~/.ssh/id_ed25519.pub(可上传到代码托管平台)


3) 启动 ssh-agent 并加载私钥(免每次输入)

Windows PowerShell(系统自带 OpenSSH):

powershell 复制代码
# 开机自启 & 立即启动
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

Git Bash / macOS / Linux:

bash 复制代码
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

4) 复制"公钥"并添加到代码平台

查看并复制:

bash 复制代码
# Git Bash / macOS / Linux
cat ~/.ssh/id_ed25519.pub

# Windows PowerShell(直接进剪贴板)
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard

到你的 Git 服务器(GitHub/GitLab/Gitea/公司私服)→ 个人设置 SSH KeysAdd new key → 粘贴保存。


5) (你有自定义端口 1024)写 SSH 配置更省事

编辑 ~/.ssh/config(没有就新建),加入:

复制代码
Host wpsit
  HostName git.wpsit.cn
  User git
  Port 1024
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

以后就能用简写 wpsit:owner/repo.git 当远程地址了。


6) 测试 SSH 连接

bash 复制代码
# 直接写主机和端口
ssh -T git@git.wpsit.cn -p 1024
# 或者用上一步的别名
ssh -T wpsit

首次会问 Are you sure you want to continue connecting (yes/no)? 输入 yes

成功会看到欢迎/鉴权成功提示。

【注】只完成1,2,4步就可以完成基本的连接,此时可以使用下述指令测试:

Git Bash / macOS / Linux:

bash 复制代码
ssh -T -p 1024 -i ~/.ssh/id_ed25519 git@git.wpsit.cn

Windows PowerShell:

bash 复制代码
ssh -T -p 1024 -i "$env:USERPROFILE\.ssh\id_ed25519" git@git.wpsit.cn

说明:

-T 不分配终端(Git 服务器常用),成功会回显类似 "successfully authenticated / welcome ...(但禁止 shell)"。第一次会提示 Are you sure you want to continue connecting (yes/no)?,输入 yes


7) 绑定/切换 Git 远程地址

新增远程:

bash 复制代码
# 用 Host 别名(推荐)
git remote add origin wpsit:hust-2027-fullstack-tasks/chenqiling.git

# 或者显式 URL(含端口)
git remote add origin ssh://git@git.wpsit.cn:1024/hust-2027-fullstack-tasks/chenqiling.git

切换已存在的远程:

bash 复制代码
git remote set-url origin wpsit:hust-2027-fullstack-tasks/chenqiling.git
# 或者
git remote set-url origin ssh://git@git.wpsit.cn:1024/hust-2027-fullstack-tasks/chenqiling.git

确认:

bash 复制代码
git remote -v

8) 下拉与上传(验证公钥是否生效)

克隆(下拉):

bash 复制代码
git clone wpsit:hust-2027-fullstack-tasks/chenqiling.git
# 或 ssh://git@git.wpsit.cn:1024/hust-2027-fullstack-tasks/chenqiling.git

已有仓库首次推送:

bash 复制代码
git add .
git commit -m "init"
git push -u origin master   # 或 main,按你的分支名

常见故障快查

  • Permission denied (publickey)

    1. 公钥没加到平台;2) ssh-add -l 看看私钥是否已加入 agent;
    2. ~/.ssh/config 是否指向对的 IdentityFile
    3. 端口 1024 是否放行;
    4. 逐条排查:ssh -vvv -T git@git.wpsit.cn -p 1024
  • Windows 权限问题(罕见):在 Git Bash 执行

    bash 复制代码
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_ed25519
    chmod 644 ~/.ssh/id_ed25519.pub

快速使用:

bash 复制代码
# 新功能
git switch -c feature/xxx
# ...开发...
git add .
git commit -m "feat(xxx): 完成初版"
git pull --rebase
git push -u origin feature/xxx
# 提 PR / 合并后
git switch main
git pull --rebase
git branch -d feature/xxx
相关推荐
我要升天!3 小时前
Git的原理与使用 -- 基础操作
大数据·服务器·git·elasticsearch
__Witheart__3 小时前
记git status不显示已追踪文件文件的更改
git
仰泳的熊猫4 小时前
LeetCode:773. 滑动谜题
数据结构·c++·算法·leetcode
夏鹏今天学习了吗4 小时前
【LeetCode热题100(50/100)】岛屿数量
算法·leetcode·职场和发展
墨染点香4 小时前
LeetCode 刷题【134. 加油站】
算法·leetcode·职场和发展
yi碗汤园4 小时前
【一文了解】八大排序-冒泡排序、选择排序
开发语言·前端·算法·unity·c#·1024程序员节
二倍速播放5 小时前
贪心算法 with Gemini
算法·贪心算法
oliveira-time5 小时前
整数划分问题
算法
liu****5 小时前
笔试强训(十三)
开发语言·c++·算法·1024程序员节