git本地实现local->remote推送

我前面说"附加终端"不准确:当前 Codex 任务不能手动接管一个已经打开的外部 WSL 终端,所以没有可操作的"添加"入口。

请直接在你当前的 WSL 终端粘贴执行下面这段:

bash 复制代码
mkdir -p ~/GitProject/tmp
cd ~/GitProject/tmp

git init -b main
git init --bare ~/GitProject/tmp-remote.git

if git remote get-url local >/dev/null 2>&1; then
  git remote set-url local ~/GitProject/tmp-remote.git
else
  git remote add local ~/GitProject/tmp-remote.git
fi

if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
  git -c user.name="Local User" \
      -c user.email="local@example.invalid" \
      commit --allow-empty -m "Initial commit"
fi

git push -u local HEAD:main
git remote -v

结果是:

  • 工作仓库:~/GitProject/tmp
  • 本地远程裸仓库:~/GitProject/tmp-remote.git
  • 远程名称:local
  • 后续推送:git push local main

我查到的当前官方资料没有说明可以把外部 WSL 终端附加到 Codex 桌面任务;当前任务接口也明确显示没有关联终端会话。

这套操作的核心,是在同一台机器上模拟 GitHub/GitLab 的"本地仓库 → 远程仓库"模式。

整体结构

text 复制代码
日常工作目录                         接收推送的远程仓库
~/GitProject/tmp        push       ~/GitProject/tmp-remote.git
┌─────────────────┐  ──────────>  ┌─────────────────────────┐
│ 源代码和 .git    │               │ 只有 Git 数据,没有工作区 │
│ 可以编辑、提交    │  <──────────  │ 一般不直接编辑文件         │
└─────────────────┘    fetch       └─────────────────────────┘

这里的"远程"是 Git 概念上的远程,不一定是另一台服务器。只要另一个 Git 仓库能够接收推送,它就可以作为远程仓库。

1. 创建工作仓库

bash 复制代码
mkdir -p ~/GitProject/tmp
cd ~/GitProject/tmp
git init -b main

含义:

  • mkdir -p:创建目录;已经存在也不会报错。
  • cd:进入工作目录。
  • git init:把普通目录变成 Git 仓库。
  • -b main:初始分支使用 main

初始化后结构类似:

text 复制代码
tmp/
├── .git/       # Git 历史、分支和配置
└── 你的文件

这是"非裸仓库",既有 .git 数据,也有可以编辑的工作目录。

2. 创建裸远程仓库

bash 复制代码
git init --bare ~/GitProject/tmp-remote.git

--bare 创建裸仓库。裸仓库没有可编辑的工作目录,里面直接存放 Git 对象、分支和配置:

text 复制代码
tmp-remote.git/
├── HEAD
├── config
├── objects/
└── refs/

服务器上的 Git 仓库通常都是裸仓库,因为它们主要负责接收和提供 Git 数据,不负责直接编辑代码。

目录名以 .git 结尾只是惯例,不是强制要求。

3. 添加远程地址

bash 复制代码
git remote add local ~/GitProject/tmp-remote.git

这条命令给远程仓库起了一个本地别名:

text 复制代码
local  ->  ~/GitProject/tmp-remote.git

local 和常见的 origin 没有本质区别,只是名字不同。

常见写法:

bash 复制代码
git remote add origin https://github.com/user/project.git
git remote add company git@gitlab.company.com:team/project.git
git remote add backup /mnt/backup/project.git

查看配置:

bash 复制代码
git remote -v

修改已有远程地址:

bash 复制代码
git remote set-url local 新地址

删除远程配置:

bash 复制代码
git remote remove local

删除远程配置不会删除远程仓库本身。

4. 创建首次提交

空仓库还没有任何提交,因此没有内容可以推送:

bash 复制代码
git add .
git commit -m "Initial commit"

如果目录中暂时没有文件,也可以创建空提交:

bash 复制代码
git commit --allow-empty -m "Initial commit"

之前命令里的:

bash 复制代码
git -c user.name="Local User" \
    -c user.email="local@example.invalid" \
    commit --allow-empty -m "Initial commit"

表示只给这一次提交临时指定作者信息,不修改全局 Git 配置。

通常更建议提前配置自己的真实信息:

bash 复制代码
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

5. 首次推送并建立跟踪关系

bash 复制代码
git push -u local HEAD:main

拆开理解:

  • git push:推送提交。
  • local:推送到名为 local 的远程。
  • HEAD:当前检出的本地分支。
  • :main:写入远程的 main 分支。
  • -u:记录上游跟踪关系。

推送后形成:

text 复制代码
本地 main  ->  local/main

以后在本地 main 分支上通常可以简写:

bash 复制代码
git push
git pull

如果不使用 -u,以后可能需要完整写:

bash 复制代码
git push local main

日常工作流程

以后修改文件后:

bash 复制代码
cd ~/GitProject/tmp

git status
git add .
git commit -m "描述本次修改"
git push

查看提交历史:

bash 复制代码
git log --oneline --graph --decorate --all

从远程重新克隆

可以用另一个目录模拟另一位开发者或另一台机器:

bash 复制代码
git clone ~/GitProject/tmp-remote.git ~/GitProject/tmp-clone
cd ~/GitProject/tmp-clone

在克隆仓库中修改并推送:

bash 复制代码
git add .
git commit -m "Update from clone"
git push

回到原工作仓库获取修改:

bash 复制代码
cd ~/GitProject/tmp
git pull

以后遇到类似场景的通用模板

已有本地项目,要连接一个远程仓库:

bash 复制代码
cd 项目目录
git init -b main
git remote add origin 远程仓库地址
git add .
git commit -m "Initial commit"
git push -u origin main

远程仓库已经有内容,要下载到本地:

bash 复制代码
git clone 远程仓库地址 本地目录

同一项目配置多个远程仓库:

bash 复制代码
git remote add origin 主仓库地址
git remote add backup 备份仓库地址

git push origin main
git push backup main

需要特别注意:本地裸仓库能练习完整的 Git 推送流程,但它仍在同一台电脑上,不能代替真正的异机备份。磁盘损坏时,工作仓库和"远程"仓库可能一起丢失。

基本格式:

bash 复制代码
git clone <远程仓库地址> <本地目录>

针对刚才创建的本地远程仓库:

bash 复制代码
git clone ~/GitProject/tmp-remote.git ~/GitProject/tmp-clone

然后进入克隆出来的项目:

bash 复制代码
cd ~/GitProject/tmp-clone
git status
git remote -v

你会看到默认远程名是 origin

text 复制代码
origin  /home/你的用户名/GitProject/tmp-remote.git

之后的完整操作示例:

bash 复制代码
cd ~/GitProject/tmp-clone

echo "hello" > hello.txt
git add hello.txt
git commit -m "Add hello file"
git push

原来的仓库获取这次修改:

bash 复制代码
cd ~/GitProject/tmp
git pull

常见克隆方式:

bash 复制代码
# 克隆到自动命名的目录
git clone https://github.com/user/project.git

# 指定本地目录名
git clone https://github.com/user/project.git my-project

# 使用 SSH
git clone git@github.com:user/project.git

# 克隆指定分支
git clone -b develop https://github.com/user/project.git

# 只获取较新的少量历史
git clone --depth 1 https://github.com/user/project.git

注意:不能把仓库克隆到已经包含文件的非空目录。如果 ~/GitProject/tmp-clone 已存在且非空,请换一个目录名,例如:

bash 复制代码
git clone ~/GitProject/tmp-remote.git ~/GitProject/tmp-clone-2
相关推荐
Geek-Chow1 天前
Git 标签与发布流程:轻量标签、附注标签与签名标签
git·tags
wear工程师1 天前
Git 冲突怎么解决:别只删标记,先看懂暂存区的 1、2、3 阶段
git
村雨遥1 天前
覆盖提交不等于删除,这才是彻底清除 Git 历史的正确姿势
git·github
淮北4941 天前
ubuntu22 默认输入法调整频率
运维·服务器·git·ubuntu·elasticsearch
momo_aa1 天前
小白下载git步骤
git
dunge20261 天前
2026 ChatGPT Plus / Pro + Codex 实战:从 0 搭一套 AI 编程项目模板,AGENTS.md + Git + 测试一次配好
人工智能·git·chatgpt
福如意如我心意2 天前
git清除仓库敏感文件并同步至远端的操作
git
Brilliantwxx2 天前
【Linux】 开发|Git 版本控制与 gdb/cgdb 调试
linux·笔记·git
.Hypocritical.3 天前
Git 全套实操|配置 /.gitignore/ 分支合并 / 冲突解决 /git stash 完整教程
git
来日方长。。。。long3 天前
Hermes Agent橙皮书共读|第三篇:保姆级实战部署|本地/VPS从零搭建Hermes
ide·git·hermes