当前位置: 首页 > news >正文

Git 案例1:不同设备的文件同步

实际需求

我有两台Linux设备,记为A和B。A放在家里用,B放在工作的地方“摸鱼”。A和B几乎没有同时通电的时候,不在同一个无线局域网内。最近在学bash脚本,有时在A上写,有时在B上写,希望里面的内容实现同步,该如何做?我想到了最近初学的 Git,但还没学到多端同步的程度。

A 上和 B 上学 bash 的目录均为~/bash

Step 1:建立 GitHub Repository

https://github.com/


注意不要勾选Add README,否则会自动将README.mdcommit 进去了,到时候把本地的内容上传到 GitHub Repository时,会出现内容冲突的错误。

Step 2:在A上初始化 Git 仓库

以在A上为例,在~/bash/上建立本地的 Git Repository:

user@localhost:~/bash$gitinit 提示:使用'master'作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中 提示:配置使用初始分支名,并消除这条警告,请执行: 提示: 提示:gitconfig--globalinit.defaultBranch<名称>提示: 提示:除了'master'之外,通常选定的名字有'main''trunk''development'。 提示:可以通过以下命令重命名刚创建的分支: 提示: 提示:gitbranch-m<name>已初始化空的 Git 仓库于 ~/bash/.git/

分支,指的是:

查看当前分支:

gitbranch

此时没有任何输出结果。因为只有git commit之后才会创建分支。从输出结果看出,初次创建的分支名为master,还可以对此进行修改:

gitbranch-mmain# 对当前仓库生效gitconfig--globalinit.defaultBranch main# 对所有仓库均生效

不同分支下,仓库的内容是不同的。比如刚才 GitHub 上的初始分支是main

若想修改 GitHub Repository 的默认分支,请在 GitHub 的 Settings -> Repositories -> Repository default branch 中进行设置。

Step 3:在 A 上 commit 自己的内容

user@localhost:~/bash$echo"# LearnBash">>README.md user@localhost:~/bash$gitadd.user@localhost:~/bash$gitcommit-m"first commit on Mint"[main (根提交) 54c8274]first commit on Mint7files changed,78insertions(+)create mode100644answer_yn.sh create mode100644args.sh create mode100644calc_pi.sh create mode100644csv_analysis.sh create mode100644data.csv create mode100644multiply.sh create mode100644read.sh

Step 4:关联 GitHub Repository 远程库

创建密钥对 {#A创建密钥对}

user@localhost:~/bash$ ssh-keygen-ted25519-C"<your_email>"Generating public/private ed25519 key pair. Enterfileinwhichto save the key(/home/binzz/.ssh/id_ed25519): Enter passphrase(emptyforno passphrase): Enter same passphrase again: Your identification has been savedin/home/binzz/.ssh/id_ed25519 Your public key has been savedin/home/binzz/.ssh/id_ed25519.pub The key fingerprint is:# ... 省略The key's randomart image is: +--[ED25519256]--+# ... 省略+----[SHA256]-----+ user@localhost:~/bash$cat~/.ssh/id_ed25519.pub# ... 省略输出内容 # 拷贝一下,需要在 GitHub 上添加到 SSH Keys 中

补充说明:

  1. ssh-keygen命令用于生成密钥对。
    For modern security, Ed25519 is recommended:
    ssh-keygen-ted25519-C"your_email@example.com"
    IfEd25519is unsupported, useRSAwith strong encryption:
    ssh-keygen-trsa-b4096-C"your_email@example.com"
    • -tspecifies the algorithm.
    • -bsets key length (RSA only).
    • -Cadds a label (usually your email).

在 GitHub 上打开 Settings -> SSH and GPG keys -> New SSH key,粘贴刚才拷贝的内容。

添加远程库 {#A添加远程库}

在 A 上操作。

gitremoteaddorigin git@github.com:hhhqdfzz/LearnBash.git

origin是远程库的名字,是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库。hhhqdfzz是用户名,LearnBash是仓库名。

查看远程库:

user@localhost:~/bash$gitremote-vorigin git@github.com:hhhqdfzz/LearnBash.git(fetch)origin git@github.com:hhhqdfzz/LearnBash.git(push)

Step 5:把A上的内容上传到远程库

gitpush-uorigin main

推送到远程库originmain分支。由于远程库是空的,我们第一次推送main分支时,加上了-u参数,Git不但会把本地的main分支内容推送的远程新的main分支,还会把本地的main分支和远程的main分支关联起来,在以后的推送或者拉取时就可以简化命令。
以后再上传时,就不需要加上-u参数了,即:

gitpush origin main

Step 6:在B上拉取 GitHub Repository

创建密钥对

方法同 A。

克隆仓库

user@localhost:~$gitclone git@github.com:hhhqdfzz/LearnBash.git Cloning into'LearnBash'... The authenticity ofhost'github.com (xx.xx.xx.xx)'can't be established. ED25519 key fingerprint is <省略>. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com'(ED25519)to the list of known hosts. remote: Enumerating objects:12, done. remote: Counting objects:100%(12/12), done. remote: Compressing objects:100%(10/10), done. remote: Total12(delta1), reused12(delta1), pack-reused0(from0)Receiving objects:100%(12/12), done. Resolving deltas:100%(1/1), done. user@localhost:~$mvLearnBash/bash

里面有.git目录,与 A 和 GitHub Repository 中的.git目录的内容是相同的,因此不需要再添加远程库。

拉取更新

假如在 A 上更新了内容并上传:

user@localhost:~/bash$echo"The repository is for Huihui Han.">>README.md user@localhost:~/bash$gitadd.user@localhost:~/bash$gitcommit-m"Modified README on Mint."[main 2e8a84c]Modified README on Mint.1filechanged,1insertion(+)user@localhost:~/bash$gitpush origin main 枚举对象中:5, 完成. 对象计数中:100%(5/5), 完成. 使用32个线程进行压缩 压缩对象中:100%(2/2), 完成. 写入对象中:100%(3/3),328字节|328.00KiB/s, 完成. 总共3(差异1),复用0(差异0),包复用0remote: Resolving deltas:100%(1/1), completed with1localobject. To github.com:hhhqdfzz/LearnBash.git 7cdd8a5..2e8a84c main ->main

则 在 B 上需要拉取更新:

gitpull origin main

可以看到,的确更新了:

user@localhost:~/bash$catREADME.md# LearnBashThe repository isforHuihui Han. user@localhost:~/bash$

附:反悔药

删除 ssh 密钥对

rm~/.ssh/id_ed25519*

再去 GitHub 上删除。

删除远程库

gitremotermorigin

origin是远程库的名称。

http://www.jsqmd.com/news/637436/

相关文章:

  • 新手必看:从10W到2000W,不同功率下开关电源拓扑怎么选?
  • 【四川电影电视学院主办】第五届科学教育与艺术鉴赏国际学术会议(SEAA 2026)
  • rk3399平台rtl8723DS Wi-Fi模块SDIO接口驱动移植与双模配置实战
  • riscv64-unknown-elf-gdb 安装与配置全指南
  • Schema核心功能详解:从数据验证到函数注解
  • Axios供应链攻击波及OpenAI,安全防线再受考验
  • 为什么92%的AIAgent项目卡在世界建模阶段?深度拆解6个被忽略的感知-记忆-推理对齐断点
  • AI Agent开发者如何准备秋招:时间线与重点
  • ice_cube实战案例:如何用Ruby库构建智能提醒系统
  • douyin-downloader:基于智能降级策略的抖音视频批量下载架构深度解析
  • 【SPIE-电子科技大学主办】第三届计算机视觉、机器人与自动化工程国际学术会议(CRAE 2026)
  • 终极Windows 11系统瘦身指南:用Win11Debloat重获系统控制权
  • 嵌入式linux设备内存泄露排查思路
  • 全网最全:计算机视觉需要哪些数学基础?如何高效学习线性代数和概率论?
  • Hewlett Packard 44701A 数字电压表
  • OmenSuperHub:让你的游戏本性能飙升,告别臃肿官方软件
  • PDS 2020.3 联合 ModelSim 仿真避坑指南:从编译库到解决 GRS_INST 报错的全流程
  • 大模型应用开发实例学习笔记 - 大模型集成、RAG、Tool Calling、MCP协议、智能体.etc
  • J-Link烧录的原因
  • 后 Zoom 时代:视频会议平台的多元竞争与选择
  • DLSS Swapper终极指南:三步轻松提升游戏画质与性能
  • 5分钟快速掌握Docker日志管理:从入门到精通实战指南
  • AI Agent高薪岗位盘点:哪些方向最赚钱
  • Vulnserver终极指南:快速掌握Windows缓冲区溢出漏洞利用技巧
  • 西电计科/软工智能软件大作业实战复盘:从选题到展示的全流程指南
  • 51单片机PWM调光不精准?可能是你的定时器初值没算对(附误差分析与修正代码)
  • STM32G4霍尔有感运行实战:从零配置到电流环闭环调试(附完整代码)
  • 【SPIE出版、EI检索稳定】2026年智慧油气与可持续发展国际学术会议(SOGSD 2026)
  • Translumo:免费实时屏幕翻译工具终极指南
  • mPDF实战指南:5个核心场景深度解析PHP到PDF的高效转换方案