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

张开发
2026/4/15 11:55:24 15 分钟阅读

分享文章

Git 案例1:不同设备的文件同步
实际需求我有两台Linux设备记为A和B。A放在家里用B放在工作的地方“摸鱼”。A和B几乎没有同时通电的时候不在同一个无线局域网内。最近在学bash脚本有时在A上写有时在B上写希望里面的内容实现同步该如何做我想到了最近初学的 Git但还没学到多端同步的程度。A 上和 B 上学 bash 的目录均为~/bash。Step 1建立 GitHub Repositoryhttps://github.com/注意不要勾选Add README否则会自动将README.mdcommit 进去了到时候把本地的内容上传到 GitHub Repository时会出现内容冲突的错误。Step 2在A上初始化 Git 仓库以在A上为例在~/bash/上建立本地的 Git Repositoryuserlocalhost:~/bash$gitinit 提示使用master作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中 提示配置使用初始分支名并消除这条警告请执行 提示 提示gitconfig--globalinit.defaultBranch名称提示 提示除了master之外通常选定的名字有main、trunk和development。 提示可以通过以下命令重命名刚创建的分支 提示 提示gitbranch-mname已初始化空的 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 自己的内容userlocalhost:~/bash$echo# LearnBashREADME.md userlocalhost:~/bash$gitadd.userlocalhost:~/bash$gitcommit-mfirst 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.shStep 4关联 GitHub Repository 远程库创建密钥对 {#A创建密钥对}userlocalhost:~/bash$ ssh-keygen-ted25519-Cyour_emailGenerating 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 keys randomart image is: --[ED25519256]--# ... 省略----[SHA256]----- userlocalhost:~/bash$cat~/.ssh/id_ed25519.pub# ... 省略输出内容 # 拷贝一下需要在 GitHub 上添加到 SSH Keys 中补充说明ssh-keygen命令用于生成密钥对。For modern security, Ed25519 is recommended:ssh-keygen-ted25519-Cyour_emailexample.comIfEd25519is unsupported, useRSAwith strong encryption:ssh-keygen-trsa-b4096-Cyour_emailexample.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 gitgithub.com:hhhqdfzz/LearnBash.gitorigin是远程库的名字是Git默认的叫法也可以改成别的但是origin这个名字一看就知道是远程库。hhhqdfzz是用户名LearnBash是仓库名。查看远程库userlocalhost:~/bash$gitremote-vorigin gitgithub.com:hhhqdfzz/LearnBash.git(fetch)origin gitgithub.com:hhhqdfzz/LearnBash.git(push)Step 5:把A上的内容上传到远程库gitpush-uorigin main推送到远程库origin的main分支。由于远程库是空的我们第一次推送main分支时加上了-u参数Git不但会把本地的main分支内容推送的远程新的main分支还会把本地的main分支和远程的main分支关联起来在以后的推送或者拉取时就可以简化命令。以后再上传时就不需要加上-u参数了即gitpush origin mainStep 6在B上拉取 GitHub Repository创建密钥对方法同 A。克隆仓库userlocalhost:~$gitclone gitgithub.com:hhhqdfzz/LearnBash.git Cloning intoLearnBash... The authenticity ofhostgithub.com (xx.xx.xx.xx)cant 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. userlocalhost:~$mvLearnBash/bash里面有.git目录与 A 和 GitHub Repository 中的.git目录的内容是相同的因此不需要再添加远程库。拉取更新假如在 A 上更新了内容并上传userlocalhost:~/bash$echoThe repository is for Huihui Han.README.md userlocalhost:~/bash$gitadd.userlocalhost:~/bash$gitcommit-mModified README on Mint.[main 2e8a84c]Modified README on Mint.1filechanged,1insertion()userlocalhost:~/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可以看到的确更新了userlocalhost:~/bash$catREADME.md# LearnBashThe repository isforHuihui Han. userlocalhost:~/bash$附反悔药删除 ssh 密钥对rm~/.ssh/id_ed25519*再去 GitHub 上删除。删除远程库gitremotermoriginorigin是远程库的名称。

更多文章