Git Clone 三种方式 + 本地提交代码完整流程

Git Clone 三种方式 + 本地提交代码完整流程 · Clone & Push Guide

Git Clone 三种方式 + 本地提交完整流程

HTTPS / SSH / GitHub CLI 的区别与选型,从拉取代码到提交推送的完整指南

一、Clone 的三种方式概览

git clone 是从远程仓库下载代码到本地的标准方式。根据不同的认证和网络环境,主要有三种方式可选:

方式 地址格式示例 认证方式 推荐场景
HTTPS https://github.com/user/repo.git 账号 + 密码 / Personal Access Token (PAT) 新手、公司内网、防火墙环境
SSH git@github.com:user/repo.git SSH 密钥对(公钥上传平台) 长期高频开发、个人电脑
GitHub CLI gh repo clone user/repo gh auth login 登录 终端重度使用者、需管理 PR/Issue

二、HTTPS 方式详解

HTTPS 克隆方式

git clone https://github.com/user/repo.git
  • 内网、防火墙环境几乎不会被拦截,公司网络通用
  • 无需提前配置密钥,开箱即用
  • 支持 Git 凭证缓存(credential.helper),记忆后免重复输入
  • 频繁操作需要重复输入凭证(可配置缓存缓解)
  • GitHub 已不再支持密码认证,必须使用 Personal Access Token (PAT)
  • Token 过期后需要重新生成和配置

HTTPS 凭证配置

配置凭证缓存,避免每次操作输入账号和 Token:

# 开启凭证缓存(默认 15 分钟)
git config --global credential.helper cache

# 长期缓存(例如 1 小时)
git config --global credential.helper 'cache --timeout=3600'

# Windows 使用 Git Credential Manager(永久保存)
git config --global credential.helper manager-core

三、SSH 方式详解

SSH 克隆方式

git clone git@github.com:user/repo.git
  • 一次配置永久免密,操作流畅
  • 安全性高,基于密钥对认证
  • 适合脚本自动化、CI/CD 流水线使用
  • 企业严格防火墙可能拦截 22 端口,无法连接
  • 需要额外生成、上传密钥,首次有一定上手成本
  • 更换电脑需重新配置密钥

SSH 密钥配置步骤

  1. 生成 SSH 密钥对 ssh-keygen -t ed25519 -C “your_email@example.com” 按回车使用默认路径(~/.ssh/id_ed25519),可设置密码短语(passphrase)或留空
  2. 查看公钥内容 cat ~/.ssh/id_ed25519.pub 复制输出的完整内容(以 ssh-ed25519 开头)
  3. 将公钥添加到代码平台 GitHub → Settings → SSH and GPG keys → New SSH key,粘贴公钥并保存
  4. 测试连接 ssh -T git@github.com 看到 “Hi username! You’ve successfully authenticated” 即表示配置成功

四、GitHub CLI (gh) 方式

GitHub CLI 克隆方式

gh repo clone user/repo
  • 除了 clone,还可一键创建 PR、查看 Issue、管理仓库
  • 语法更简洁,无需记忆 Git 地址
  • 统一管理 GitHub 相关操作
  • 需要单独安装 gh 工具
  • 仅适合终端重度使用者,普通开发没必要
  • 不如原生 Git 通用(其他代码平台不适用)

安装与初始化

  1. 安装 GitHub CLI Windows: winget install GitHub.cli 或从 cli.github.com 下载 macOS: brew install gh
  2. 登录 GitHub gh auth login 选择 GitHub.com,选择 HTTPS 或 SSH 协议,使用浏览器或 Token 完成登录
  3. 克隆仓库 gh repo clone user/repo 会自动创建 user/repo 文件夹并拉取代码

五、三种方式对比总表

维度 HTTPS SSH GitHub CLI
首次配置门槛 低(需生成 PAT) 中(需生成上传密钥) 中(需安装 gh + 登录)
是否需额外安装 不需要 不需要 需要安装 gh 工具
防火墙兼容性 高(443 端口通用) 低(22 端口可能被拦截) 取决于底层协议
推送是否需要凭证 需要(可缓存) 免密(密钥认证) 免密(gh 会话)
适合的场景 新手、企业内网 长期个人开发 重度 CLI 用户
通用的代码平台 GitHub / GitLab / Gitee 等全部支持 全部支持 仅 GitHub

六、其他获取代码的方式

GitHub Desktop 图形化客户端

GitHub 官方提供的免费桌面工具,不用敲命令,可视化完成克隆、提交、推送、创建分支等操作。适合不熟悉命令行的用户。点击仓库页面的 “Open with GitHub Desktop” 按钮即可自动克隆。

Download ZIP 下载压缩包

仅一次性下载源码快照,不是 Git 仓库。下载的是普通文件夹,没有 .git 目录,不能提交、推送代码,没有版本追踪。仅适合临时查看源码。

注意:Download ZIP 下载的文件无法执行 git push 提交代码。如果需要参与项目开发,必须使用 git clone。

七、本地提交代码完整流程

以下假设场景:已经 git clone 了远程仓库到本地,现在需要修改代码并推送回远程仓库。

方案 A:命令行 Git(通用,推荐)

git status git add . git commit -m git push
  1. 查看文件变更状态 git status 红色 = 未追踪/修改的文件;绿色 = 已加入暂存区
  2. 将改动加入暂存区 git add . . 表示添加所有变更文件;也可指定文件如 git add src/index.ts
  3. 填写提交说明 git commit -m “完成 DWG 地层解析标准化逻辑” 提交信息应清晰说明本次改动的目的和内容,建议使用英文或中文均可
  4. 推送到远程仓库 git push HTTPS 首次推送会弹出窗口要求输入账号 + Personal Access Token
    SSH 配置好密钥后直接推送,无需输入凭证
提示:如果推送时提示 “The requested URL returned error: 403” 或认证失败,说明需要配置 Personal Access Token 或 SSH 密钥。HTTPS 方式建议在 Token 生成后通过 git config --global credential.helper manager-core 缓存凭证。

完整终端操作示例

D:\Code\borehole-log-standardizer> git status
Changes not staged for commit:
  modified: src/parser.py


D:\Code\borehole-log-standardizer> git add .

D:\Code\borehole-log-standardizer> git commit -m “Implement DWG borehole log parsing standardization”
[main abc1234] Implement DWG borehole log parsing standardization
 1 file changed, 45 insertions(+), 12 deletions(-)


D:\Code\borehole-log-standardizer> git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Writing objects: 100% (4/4), 1.2 KiB done.
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/user/repo.git
  abc1234..def5678 main -> main

方案 B:GitHub Desktop 图形化工具

  1. 打开 GitHub Desktop,点击 File → Clone Repository,或直接在仓库页面点击 “Open with GitHub Desktop” 选择本地存放路径后,工具自动完成克隆
  2. 在本地文件夹中修改代码(使用 VS Code 或其他编辑器) GitHub Desktop 左侧自动显示变更文件列表,文件旁有 + 标记新增行、 标记删除行
  3. 在 GitHub Desktop 左下角填写提交摘要(Summary)和可选描述(Description) 点击 Commit to main 完成本地提交
  4. 点击窗口右上角的 Push origin 按钮 代码推送到远程仓库后,按钮变为灰色表示已同步
注意:GitHub Desktop 底层同样使用 Git,只是将命令行操作封装为可视化界面。提交历史(commit log)、分支管理(branch)等功能均与命令行一致。

八、已有本地仓库的更新维护

假设场景:本地已经 clone 过仓库,但远程仓库已被其他人更新,需要拉取最新代码并继续开发。

日常开发循环

git pull 修改代码 git add . git commit git push

1. 拉取远程最新代码

# 拉取远程 main 分支的最新代码并自动合并到本地
git pull origin main

# 如果只需查看远程有哪些更新(不合并)
git fetch origin
建议:每次开始工作前先执行 git pull,确保本地代码与远程同步,减少冲突。

2. 解决代码冲突

如果 git pull 提示冲突(CONFLICT),需要手动解决:

  1. 查看哪些文件冲突 git status 冲突文件显示为 both modified
  2. 打开冲突文件,搜索 <<<<<<< 标记 冲突区域以 <<<<<<< HEAD(本地改动)和 =======(分隔线)和 >>>>>>> remote(远程改动)标识
  3. 手动编辑保留正确内容,删除冲突标记 可使用 VS Code 等编辑器内置的冲突解决工具(Accept Current / Incoming / Both)
  4. 标记为已解决并提交 git add .
    git commit -m “Resolve merge conflict between local and remote”

3. 分支管理

# 创建并切换到新分支
git checkout -b feature/new-function

# 切换到已有分支
git checkout main

# 查看所有分支
git branch -a

# 将新分支推送到远程
git push origin feature/new-function

4. 查看提交历史

# 查看提交记录
git log --oneline -10

# 查看某次提交的详细改动
git show abc1234

九、场景选型建议

推荐 HTTPS
公司内网、防火墙严格:优先使用 HTTPS,443 端口几乎不会被拦截,配置 PAT + 凭证缓存后可流畅使用。
推荐 SSH
自己电脑长期开发、无网络限制:配置 SSH 密钥后永久免密,日常推送拉取更流畅。
推荐 GitHub Desktop
完全不想敲命令:使用 GitHub Desktop 图形化界面,可视化完成所有 Git 操作。
推荐 gh CLI
终端重度用户、需要管理 PR/Issue:安装 GitHub CLI,clone 只是其功能之一,结合 PR、Issue 管理可提升效率。
不推荐
仅 Download ZIP:仅临时查看源码可以,但无法提交代码,不适合任何开发参与场景。
总结:对绝大多数开发者来说,HTTPS 是最通用稳妥的选择;SSH 是提升体验的进阶方案;GitHub Desktop 是零命令入门方案。日常开发循环记住四个命令即可:git pullgit add .git commit -mgit push

Git Clone 3 Ways + Local Commit Workflow

HTTPS vs SSH vs GitHub CLI — the complete guide from cloning to pushing

1. Clone Methods Overview

git clone is the standard way to download a remote repository to your local machine. There are three main methods depending on authentication and network environment:

Method URL Example Authentication Best For
HTTPS https://github.com/user/repo.git Password / Personal Access Token (PAT) Beginners, corporate networks, firewalls
SSH git@github.com:user/repo.git SSH key pair (public key uploaded) Long-term development, personal machines
GitHub CLI gh repo clone user/repo gh auth login Heavy terminal users, PR/Issue management

2. HTTPS in Detail

HTTPS Clone

git clone https://github.com/user/repo.git
  • Works reliably through corporate firewalls and proxies
  • No key configuration needed upfront
  • Supports credential caching (credential.helper)
  • Requires repeated credential entry without caching
  • GitHub no longer accepts passwords — must use PAT
  • PAT expires and needs regeneration

Credential Caching

# Cache credentials (default 15 min)
git config --global credential.helper cache

# Long-term cache (e.g. 1 hour)
git config --global credential.helper 'cache --timeout=3600'

# Windows: Git Credential Manager (permanent)
git config --global credential.helper manager-core

3. SSH in Detail

SSH Clone

git clone git@github.com:user/repo.git
  • Password-free after initial setup — seamless push/pull
  • High security via key-pair authentication
  • Ideal for automation scripts and CI/CD pipelines
  • Port 22 may be blocked by strict corporate firewalls
  • Requires key generation and upload — some upfront effort
  • Must re-configure when changing machines

SSH Key Setup

  1. Generate SSH key pair ssh-keygen -t ed25519 -C “your_email@example.com” Press Enter for default path (~/.ssh/id_ed25519). Optionally set a passphrase.
  2. View your public key cat ~/.ssh/id_ed25519.pub Copy the full output (starts with ssh-ed25519)
  3. Add public key to your code platform GitHub → Settings → SSH and GPG keys → New SSH key. Paste and save.
  4. Test connection ssh -T git@github.com “Hi username! You’ve successfully authenticated” confirms success.

4. GitHub CLI (gh)

GitHub CLI Clone

gh repo clone user/repo
  • Create PRs, view Issues, manage repos from terminal
  • Cleaner syntax — no need to remember Git URLs
  • Unified tool for all GitHub operations
  • Requires installing the gh tool separately
  • Overkill for casual development
  • GitHub-only (not for GitLab/Gitee etc.)

Installation & Setup

  1. Install GitHub CLI Windows: winget install GitHub.cli or download from cli.github.com macOS: brew install gh
  2. Log in to GitHub gh auth login Choose GitHub.com, select HTTPS or SSH, authenticate via browser or Token
  3. Clone a repository gh repo clone user/repo Automatically creates the folder and downloads the code

5. Comparison Table

Dimension HTTPS SSH GitHub CLI
Initial setup effort Low (need PAT) Medium (generate + upload key) Medium (install gh + login)
Extra software needed No No Yes (gh tool)
Firewall compatibility High (port 443) Low (port 22 may be blocked) Depends on underlying protocol
Push requires credentials Yes (can cache) No (key authentication) No (gh session)
Best for Beginners, corporate Long-term personal dev Heavy CLI users
Platform support GitHub / GitLab / Gitee, all platforms All platforms GitHub only

6. Other Ways to Get Code

GitHub Desktop GUI Client

Free desktop tool from GitHub — no commands needed. Visual clone, commit, push, branch management. Click “Open with GitHub Desktop” on any repo page to auto-clone.

Download ZIP

Downloads a one-time snapshot — not a Git repository. No .git directory, no version tracking, no ability to push code. Only suitable for temporary code review.

Warning: Code downloaded via ZIP cannot execute git push. Use git clone if you plan to contribute.

7. Local Commit Workflow

Assuming: you have already cloned the repository and now need to modify code and push changes back.

Option A: Command Line Git (Recommended)

git status git add . git commit -m git push
  1. Check changed files git status Red = untracked/modified; Green = staged
  2. Stage changes git add . . adds all changes; or specify files like git add src/index.ts
  3. Write a commit message git commit -m “Implement DWG borehole log parsing standardization” The message should clearly describe what was changed and why
  4. Push to remote git push HTTPS first push: enter username + PAT in the popup window
    SSH: pushes directly without credential prompt
Tip: If push returns “403” or authentication errors, configure PAT (HTTPS) or SSH keys. For HTTPS, use git config --global credential.helper manager-core to cache credentials.

Full Terminal Example

D:\Code\borehole-log-standardizer> git status
Changes not staged for commit:
  modified: src/parser.py


D:\Code\borehole-log-standardizer> git add .

D:\Code\borehole-log-standardizer> git commit -m “Implement DWG borehole log parsing standardization”
[main abc1234] Implement DWG borehole log parsing standardization
 1 file changed, 45 insertions(+), 12 deletions(-)


D:\Code\borehole-log-standardizer> git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Writing objects: 100% (4/4), 1.2 KiB done.
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/user/repo.git
  abc1234..def5678 main -> main

Option B: GitHub Desktop GUI (Zero Commands)

  1. Open GitHub Desktop, click File → Clone Repository, or click “Open with GitHub Desktop” on the repo page Choose a local path and the tool auto-clones
  2. Edit code in your local folder (VS Code or other editor) GitHub Desktop shows changed files on the left with + (additions) and (deletions)
  3. Fill in Summary (required) and Description (optional) in the bottom-left corner Click Commit to main
  4. Click Push origin in the top-right corner Button turns gray when synced with remote

8. Updating an Existing Local Repo

Scenario: local repo already exists but remote has new changes from other contributors.

Daily Development Loop

git pull Edit code git add . git commit git push

1. Pull Latest Code

# Pull latest changes from remote main branch (auto-merge)
git pull origin main

# Just check for updates without merging
git fetch origin
Best practice: Always git pull before starting work to minimize merge conflicts.

2. Resolving Merge Conflicts

If git pull reports CONFLICT, resolve manually:

  1. Check conflicted files git status Conflicted files show as both modified
  2. Open the file and search for <<<<<<< markers <<<<<<< HEAD = local changes, ======= = divider, >>>>>>> remote = remote changes
  3. Edit to keep the correct content, remove conflict markers VS Code has built-in conflict resolution (Accept Current / Incoming / Both Changes)
  4. Mark as resolved and commit git add .
    git commit -m “Resolve merge conflict”

3. Branch Management

# Create and switch to a new branch
git checkout -b feature/new-function

# Switch to an existing branch
git checkout main

# List all branches
git branch -a

# Push a new branch to remote
git push origin feature/new-function

4. Viewing History

# View recent commits
git log --oneline -10

# View details of a specific commit
git show abc1234

9. Scenario Recommendations

HTTPS
Corporate network, strict firewall: HTTPS uses port 443 which is rarely blocked. Configure PAT + credential caching for smooth use.
SSH
Personal machine, long-term development: One-time SSH setup provides permanent password-free operation.
GitHub Desktop
No desire to use command line: Visual interface for all Git operations — clone, commit, push, branch.
gh CLI
Heavy terminal user, PR/Issue management: GitHub CLI does more than clone — create PRs, view issues, manage repos from one tool.
Not Recommended
Download ZIP only: Acceptable for quick code review, but cannot push changes. Not suitable for any development work.
Summary: For most developers, HTTPS is the most universal and reliable choice. SSH is the friction-free upgrade. GitHub Desktop is the zero-command entry point. Remember the four-command loop: git pullgit add .git commit -mgit push.