建立一个 git 本地仓库

通常实际项目中会使用一台独立的机器作为
git 服务器,然后在 git 服务器中建立
一个远程的仓库,这样项目中所有的人都可 以通过局域网来访问这个 git 服务器 。 我们也可以自己在本地同一台机器来模拟这个 git 服务器 玩。

1.远程git仓库的创建

首先需要在服务器端建立一个目录然后初始化这个 git 仓库 。假设我们在“/home/remote/”目录下面来创建。

$ cd /home/remote/
$ mkdir code.git
$ cd code.git/
$ git --bare init
Initialized empty Git repository in /home/code.git/

通过git bare init 命令创建了一个空的远程仓库。

2. 本地仓库创建

在本地创建/home/test目录,作为本地仓库目录。

$ mkdir /home/test
$ cd /home/test

初始化本地的git 仓库。

$ git init
Initialized empty Git repository in /home/remote/test/.git/
在仓库下创建一个test.c空文件,然后git status查看状态。
$ touch test.c
$ git status
On branch master
No commits yet
Untracked files:
(use “git add …” to include in what will be committed)
test.c
nothing added to commit but untracked files present (use “git add” to track)

3.本地仓库和远程仓库关联

要把本地的 git 仓库推送到远程仓库中。
首先需要通过git remote add 命令添加刚才远程仓库的地址 。

查看本机IP地址:

$ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::ba9:6929:c885:cbea prefixlen 64 scopeid 0x20
ether 08:00:27:0e:ae:7b txqueuelen 1000 (Ethernet)
RX packets 79761 bytes 116091398 (116.0 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20958 bytes 1318603 (1.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

可知,本地机器(远程服务器)ip地址为10.0.2.15。

然后,在本地仓库执行:

$ git remote add origin ssh://xx@10.0.2.15:/home/code.git

其中“
"10.0.2.15 ”是服务器端的 IP 地址,“ xx ”是服务器端的登录名。 最后用git push 命令来推送即可 。

4.对远程仓库的操作

推送本地提交到远程:

$ git push origin master

复制远程仓库到本地:

$ git clone ssh://xx@10.0.2.15:/home/code.git

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老衲不依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值