Skip to content
← 返回Git列表
Git任务与配置

Git 远端同步 ​

克隆与远端同步 ​

git clone · 克隆现有的代码仓库 ​

更多信息:https://git-scm.com/docs/git-clone。

  • 克隆一个现有的代码仓库到指定目录:
bash
git clone {{远程代码库地址}} {{路径/到/目录}}
  • 克隆一个现有的代码库和它的子模块:
bash
git clone --recursive {{远程代码库地址}}
  • 正常克隆但不检出 HEAD 文件(仍创建普通仓库,不是裸仓库):
bash
git clone {{[-n|--no-checkout]}} {{远程_代码仓库_地址}}
  • 克隆一个本地的代码库:
bash
git clone {{[-l|--local]}} {{路径/到/本地/代码库名}}
  • 静默克隆,不打印任何日志:
bash
git clone {{[-q|--quiet]}} {{远程代码库地址}}
  • 克隆一个现有的代码库,只获取默认分支上10个最新的提交(对节省时间很有用):
bash
git clone --depth 10 {{远程代码库地址}}
  • 克隆一个现有的、特定远程分支的代码库:
bash
git clone {{[-b|--branch]}} {{分支名称}} --single-branch {{远程代码库地址}}
  • 使用 SSH 命令克隆一个现有的代码库:
bash
git clone {{[-c|--config]}} core.sshCommand="{{ssh -i 路径/到/ssh_私钥}}" {{远程代码库地址}}

git pull · 从远程仓库获取分支,并将其合并到本地仓库 ​

更多信息:https://git-scm.com/docs/git-pull。

  • 从默认远端获取并按配置整合到当前分支;分叉时需明确 merge/rebase/ff-only 策略:
bash
git pull
  • 从默认的远程仓库拉取代码并采用变基策略实现合并(将你的本地提交“移植”到远程获取的分支上):
bash
git pull {{[-r|--rebase]}}
  • 从给定的远程仓库和分支中拉取代码,并整合到当前检出的本地分支:
bash
git pull {{远程仓库名}} {{分支名}}

git push · 推送提交到远程仓库 ​

更多信息:https://git-scm.com/docs/git-push。

  • 将当前分支的本地更改推送到其默认的远程对应分支:
bash
git push
  • 将指定本地分支的更改推送到其远程对应分支:
bash
git push {{远程仓库名}} {{本地分支名}}
  • 将指定本地分支的更改推送到其远程对应分支,并将该远程分支设为该本地分支 push/pull 的默认目标分支:
bash
git push {{[-u|--set-upstream]}} {{远程仓库名}} {{本地分支名}}
  • 将指定本地分支的更改推送到指定的远程分支:
bash
git push {{远程仓库名}} {{本地分支名}}:{{远程分支名}}
  • 将所有本地分支的更改推送到指定远程仓库中各个对应分支:
bash
git push --all {{远程仓库名}}
  • 删除远程仓库中的分支:
bash
git push {{远程仓库名}} {{[-d|--delete]}} {{远程分支名}}
  • 删除没有本地对应分支的远程分支:
bash
git push --prune {{远程仓库名}}
  • 推送本地的新标签到远程仓库:
bash
git push --tags

git remote · 管理跟踪的远程仓库(remotes) ​

更多信息:https://git-scm.com/docs/git-remote。

  • 列出已经存在的远程仓库,包括它们的名字和 URL:
bash
git remote {{[-v|--verbose]}}
  • 查看某个远程仓库的信息:
bash
git remote show {{远程仓库名字}}
  • 添加远程仓库:
bash
git remote add {{远程仓库名字}} {{远程仓库url}}
  • 更改远程仓库地址链接(使用 --add 选项不会移除现有的 URL):
bash
git remote set-url {{远程仓库名字}} {{新url}}
  • 查看远程仓库的 URL:
bash
git remote get-url {{远程仓库名字}}
  • 移除远程仓库:
bash
git remote remove {{远程仓库名字}}
  • 重命名远程仓库:
bash
git remote rename {{旧名字}} {{新名字}}

示例整理自 tldr-pages,作者为 tldr-pages team and contributors,遵循 CC BY 4.0。

继续阅读