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
相关推荐
C雨后彩虹几秒前
计算疫情扩散时间
java·数据结构·算法·华为·面试
weixin_462446234 分钟前
Git 本地忽略 application-dev.yml 的最佳实践:不提交 .gitignore,不影响团队协作!
git
yyy(十一月限定版)1 小时前
寒假集训4——二分排序
算法
星火开发设计1 小时前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识
醉颜凉1 小时前
【LeetCode】打家劫舍III
c语言·算法·leetcode·树 深度优先搜索·动态规划 二叉树
达文汐1 小时前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
一匹电信狗1 小时前
【LeetCode_21】合并两个有序链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl
User_芊芊君子1 小时前
【LeetCode经典题解】搞定二叉树最近公共祖先:递归法+栈存路径法,附代码实现
算法·leetcode·职场和发展
算法_小学生1 小时前
LeetCode 热题 100(分享最简单易懂的Python代码!)
python·算法·leetcode
执着2591 小时前
力扣hot100 - 234、回文链表
算法·leetcode·链表