u盘作为git仓库完成不同地方的代码同步

本文介绍了如何利用Git将U盘作为仓库,实现在不同地点的代码同步。通过在U盘上创建裸仓库,然后在本地创建项目并将其与U盘上的仓库关联,使用`git push`和`git pull`命令即可完成代码的同步。这种方法适用于对私密性要求不高的情况,避免了使用云服务的费用。

https://www.cnblogs.com/sl21100/p/5613862.html

参考网上一篇 “把Git Repository建到U盘上去”

1.我经常有这样的需求,工作、家里需要对同一份代码或文档编辑,并希望在不同地方能同步;

如果是私密性不那么强,可以直接使用github托管哈,当然付费也是可以私密的~~

2.首先U盘上创建一个repository   名称: git_mycbb_repos

   git init –bare  git_mycbb_repos  //裸库,没有work目录

3.本地再建一个源项目  名称:git_mycbb

   git init git_mycbb

   放入需要同步管理的文件

   git add .    //添加文件、如果有不需要管理的文件,加入 .gitignore文件;

   git commit -m "initialized."   //提交到本地仓库

   git remote add myusb F:/git_mycbb_repos    //把u盘上的git_mycbb_repos添加为远程仓库

   git push myusb master

4.再另一台电脑或其他地方,插上u盘

   git init  mygittest //建一个本地仓库

   git remote add myusb F:/git_mycbb_repos     //把u盘上的git_mycbb_repos添加为远程仓库

   git pull myusb master   //完成代码同步

A2AE9DD1AE9D9E7B/git_LVYSince2021_repos$ git init --bare

cd ~/LVYsince2021/
(base) liufeng@liufeng-Lenovo:~/LVYsince2021$ git remote add usbGitRepos /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
(base) liufeng@liufeng-Lenovo:~/LVYsince2021$ git push usbGitRepos master

git remote add /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/

git pull
fatal: 该操作必须在一个工作区中运行
(base) liufeng@liufeng-Lenovo:~/testLVY$ ll

(base) liufeng@liufeng-Lenovo:~$ git init git_mycbb
已初始化空的 Git 仓库于 /home/liufeng/git_mycbb/.git/
(base) liufeng@liufeng-Lenovo:~$ cd git_mycbb/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ ll
总用量 12
drwxrwxr-x  3 liufeng liufeng 4096 9月   5 16:42 ./
drwxr-xr-x 58 liufeng liufeng 4096 9月   5 16:42 ../
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 16:42 .git/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ emacs aa.py
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ ll
总用量 16
drwxrwxr-x  3 liufeng liufeng 4096 9月   5 16:43 ./
drwxr-xr-x 58 liufeng liufeng 4096 9月   5 16:42 ../
-rw-rw-r--  1 liufeng liufeng   19 9月   5 16:43 aa.py
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 16:42 .git/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ git add .
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ ll
总用量 16
drwxrwxr-x  3 liufeng liufeng 4096 9月   5 16:43 ./
drwxr-xr-x 58 liufeng liufeng 4096 9月   5 16:42 ../
-rw-rw-r--  1 liufeng liufeng   19 9月   5 16:43 aa.py
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 16:43 .git/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ git commit -m "initialized"
[master (根提交) de547f4] initialized
 1 file changed, 1 insertion(+)
 create mode 100644 aa.py
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ git remote add myusb /media/liufeng/A2AE9DD1AE9D9E7B/git_mycbb_repos/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ git push myusb master
枚举对象: 3, 完成.
对象计数中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 222 字节 | 222.00 KiB/s, 完成.
总共 3 (差异 0),复用 0 (差异 0)
To /media/liufeng/A2AE9DD1AE9D9E7B/git_mycbb_repos/
 * [new branch]      master -> master
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ ll
总用量 16
drwxrwxr-x  3 liufeng liufeng 4096 9月   5 16:43 ./
drwxr-xr-x 58 liufeng liufeng 4096 9月   5 16:42 ../
-rw-rw-r--  1 liufeng liufeng   19 9月   5 16:43 aa.py
drwxrwxr-x  8 liufeng liufeng 4096 9月   5 16:44 .git/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ git log
commit de547f469923411e4453411f0adbb3134c9d9969 (HEAD -> master, myusb/master)
Author: liufeng <lifeng@zemt.cn>
Date:   Sun Sep 5 16:43:55 2021 +0800

    initialized
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ git status
位于分支 master
无文件要提交,干净的工作区
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ cd /media/
(base) liufeng@liufeng-Lenovo:/media$ cd liufeng/
(base) liufeng@liufeng-Lenovo:/media/liufeng$ ll
总用量 12
drwxr-x---+ 3 root    root    4096 9月   5 16:21 ./
drwxr-xr-x  4 root    root    4096 6月  11  2020 ../
drwxrwxrwx  1 liufeng liufeng 4096 9月   5 16:41 A2AE9DD1AE9D9E7B/
(base) liufeng@liufeng-Lenovo:/media/liufeng$ cd A2AE9DD1AE9D9E7B/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ ll
总用量 8
drwxrwxrwx  1 liufeng liufeng 4096 9月   5 16:41 ./
drwxr-x---+ 3 root    root    4096 9月   5 16:21 ../
drwxrwxrwx  1 liufeng liufeng  448 9月   5 16:44 git_mycbb_repos/
drwxrwxrwx  1 liufeng liufeng  240 9月   5 16:21 .Trash-1000/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ cd git_mycbb_repos/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B/git_mycbb_repos$ ll
总用量 7
drwxrwxrwx 1 liufeng liufeng  448 9月   5 16:44 ./
drwxrwxrwx 1 liufeng liufeng 4096 9月   5 16:41 ../
drwxrwxrwx 1 liufeng liufeng    0 9月   5 16:41 branches/
-rwxrwxrwx 1 liufeng liufeng   67 9月   5 16:42 config*
-rwxrwxrwx 1 liufeng liufeng   73 9月   5 16:42 description*
-rwxrwxrwx 1 liufeng liufeng   23 9月   5 16:42 HEAD*
drwxrwxrwx 1 liufeng liufeng  528 9月   5 16:42 hooks/
drwxrwxrwx 1 liufeng liufeng  144 9月   5 16:41 info/
drwxrwxrwx 1 liufeng liufeng  528 9月   5 16:44 objects/
drwxrwxrwx 1 liufeng liufeng  240 9月   5 16:42 refs/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B/git_mycbb_repos$ cd ~
(base) liufeng@liufeng-Lenovo:~$ cd git_mycbb/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ ll
总用量 16
drwxrwxr-x  3 liufeng liufeng 4096 9月   5 16:43 ./
drwxr-xr-x 58 liufeng liufeng 4096 9月   5 16:42 ../
-rw-rw-r--  1 liufeng liufeng   19 9月   5 16:43 aa.py
drwxrwxr-x  8 liufeng liufeng 4096 9月   5 16:49 .git/
(base) liufeng@liufeng-Lenovo:~/git_mycbb$ cd /media/liufeng/A2AE9DD1AE9D9E7B/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ ll
总用量 8
drwxrwxrwx  1 liufeng liufeng 4096 9月   5 16:41 ./
drwxr-x---+ 3 root    root    4096 9月   5 16:21 ../
drwxrwxrwx  1 liufeng liufeng  448 9月   5 16:44 git_mycbb_repos/
drwxrwxrwx  1 liufeng liufeng  240 9月   5 16:21 .Trash-1000/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ rm -fr git_mycbb_repos/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ ll
总用量 8
drwxrwxrwx  1 liufeng liufeng 4096 9月   5 16:54 ./
drwxr-x---+ 3 root    root    4096 9月   5 16:21 ../
drwxrwxrwx  1 liufeng liufeng  240 9月   5 16:21 .Trash-1000/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ mkdir git_LVYSince2021_repos
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B$ cd git_LVYSince2021_repos/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos$ ll
总用量 4
drwxrwxrwx 1 liufeng liufeng    0 9月   5 16:55 ./
drwxrwxrwx 1 liufeng liufeng 4096 9月   5 16:55 ../
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos$ git init --bare
已初始化空的 Git 仓库于 /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
(base) liufeng@liufeng-Lenovo:/media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos$ cd ~/LVYsince2021/
(base) liufeng@liufeng-Lenovo:~/LVYsince2021$ git remote add usbGitRepos /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
(base) liufeng@liufeng-Lenovo:~/LVYsince2021$ git push usbGitRepos master
枚举对象: 1044, 完成.
对象计数中: 100% (1044/1044), 完成.
使用 4 个线程进行压缩
压缩对象中: 100% (1030/1030), 完成.
写入对象中: 100% (1044/1044), 346.93 MiB | 11.07 MiB/s, 完成.
总共 1044 (差异 297),复用 0 (差异 0)
remote: 处理 delta 中: 100% (297/297), 完成.
To /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
 * [new branch]      master -> master
(base) liufeng@liufeng-Lenovo:~/LVYsince2021$ cd ..
(base) liufeng@liufeng-Lenovo:~$ mkdir testLVY
(base) liufeng@liufeng-Lenovo:~$ cd testLVY/
(base) liufeng@liufeng-Lenovo:~/testLVY$ git init --bare
已初始化空的 Git 仓库于 /home/liufeng/testLVY/
(base) liufeng@liufeng-Lenovo:~/testLVY$ ll
总用量 40
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 16:59 ./
drwxr-xr-x 59 liufeng liufeng 4096 9月   5 16:59 ../
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 branches/
-rw-rw-r--  1 liufeng liufeng   66 9月   5 16:59 config
-rw-rw-r--  1 liufeng liufeng   73 9月   5 16:59 description
-rw-rw-r--  1 liufeng liufeng   23 9月   5 16:59 HEAD
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 hooks/
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 info/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 objects/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 refs/
(base) liufeng@liufeng-Lenovo:~/testLVY$ git remote add /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
用法:git remote add [<选项>] <名称> <地址>

    -f, --fetch           抓取远程的分支
    --tags                抓取时导入所有的标签和关联对象
                          或不抓取任何标签(--no-tags)
    -t, --track <分支>    跟踪的分支
    -m, --master <分支>   主线分支
    --mirror[=(push|fetch)]
                          把远程设置为用以推送或抓取的镜像

(base) liufeng@liufeng-Lenovo:~/testLVY$ git pull
fatal: 该操作必须在一个工作区中运行
(base) liufeng@liufeng-Lenovo:~/testLVY$ ll
总用量 40
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 16:59 ./
drwxr-xr-x 59 liufeng liufeng 4096 9月   5 16:59 ../
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 branches/
-rw-rw-r--  1 liufeng liufeng   66 9月   5 16:59 config
-rw-rw-r--  1 liufeng liufeng   73 9月   5 16:59 description
-rw-rw-r--  1 liufeng liufeng   23 9月   5 16:59 HEAD
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 hooks/
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 info/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 objects/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 refs/
(base) liufeng@liufeng-Lenovo:~/testLVY$ git remote add usbGitRepos /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
(base) liufeng@liufeng-Lenovo:~/testLVY$ git pull usbGitRepos master
fatal: 该操作必须在一个工作区中运行
(base) liufeng@liufeng-Lenovo:~/testLVY$ git init --bare
重新初始化已存在的 Git 仓库于 /home/liufeng/testLVY/
(base) liufeng@liufeng-Lenovo:~/testLVY$ ll
总用量 40
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 17:01 ./
drwxr-xr-x 59 liufeng liufeng 4096 9月   5 16:59 ../
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 branches/
-rw-rw-r--  1 liufeng liufeng  202 9月   5 17:01 config
-rw-rw-r--  1 liufeng liufeng   73 9月   5 16:59 description
-rw-rw-r--  1 liufeng liufeng   23 9月   5 16:59 HEAD
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 hooks/
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 info/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 objects/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 refs/
(base) liufeng@liufeng-Lenovo:~/testLVY$ git init
已初始化空的 Git 仓库于 /home/liufeng/testLVY/.git/
(base) liufeng@liufeng-Lenovo:~/testLVY$ ll
总用量 44
drwxrwxr-x  8 liufeng liufeng 4096 9月   5 17:02 ./
drwxr-xr-x 59 liufeng liufeng 4096 9月   5 16:59 ../
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 branches/
-rw-rw-r--  1 liufeng liufeng  202 9月   5 17:01 config
-rw-rw-r--  1 liufeng liufeng   73 9月   5 16:59 description
drwxrwxr-x  7 liufeng liufeng 4096 9月   5 17:02 .git/
-rw-rw-r--  1 liufeng liufeng   23 9月   5 16:59 HEAD
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 hooks/
drwxrwxr-x  2 liufeng liufeng 4096 9月   5 16:59 info/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 objects/
drwxrwxr-x  4 liufeng liufeng 4096 9月   5 16:59 refs/
(base) liufeng@liufeng-Lenovo:~/testLVY$ git remote add /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/
用法:git remote add [<选项>] <名称> <地址>

    -f, --fetch           抓取远程的分支
    --tags                抓取时导入所有的标签和关联对象
                          或不抓取任何标签(--no-tags)
    -t, --track <分支>    跟踪的分支
    -m, --master <分支>   主线分支
    --mirror[=(push|fetch)]
                          把远程设置为用以推送或抓取的镜像

(base) liufeng@liufeng-Lenovo:~/testLVY$ git pull /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos/ master
remote: 枚举对象: 1044, 完成.
remote: 对象计数中: 100% (1044/1044), 完成.
remote: 压缩对象中: 100% (733/733), 完成.
remote: 总共 1044 (差异 297),复用 1044 (差异 297)
接收对象中: 100% (1044/1044), 346.93 MiB | 42.25 MiB/s, 完成.
处理 delta 中: 100% (297/297), 完成.
来自 /media/liufeng/A2AE9DD1AE9D9E7B/git_LVYSince2021_repos
 * branch            master     -> FETCH_HEAD
正在更新文件: 100% (1017/1017), 完成.
(base) liufeng@liufeng-Lenovo:~/testLVY$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值