Git建立本地远程仓库

前言

今天有个同事突然问我:怎么创建一个本地的远程仓库,并且还要能看到仓库文件,能够push的。。
大家都知道Git仓库是分布式的,也可以这么理解每一个本地仓库同时也能是一个远程仓库。
既然这样,我们直接建一个目录,然后创建一个本地仓库就可以了。然后再到另外一个目录进行clone操作就相当于是有远程仓库了。
想法是好的,然而要完整的实现他的需求还是有一些坑要走的,下面来进行具体的操作吧。

创建远程仓库

上面已经把基本概念说了,在这里创建远程仓库其实也就是创建本地仓库。用过git的朋友都知道这个,找到一个地方创建一个空目录,执行以下操作:

lu@E5:~/Temp/git_server/Test$ git init
Initialized empty Git repository in /home/lu/Temp/git_server/Test/.git/

或者创建一个裸仓库

lu@E5:~/Temp/git_server/Test$ git init --bare
Initialized empty Git repository in /home/lu/Temp/git_server/Test/

裸仓库一般是无法直接看到源码的,所以也就不符合需求了,这里就不作过多详述。

关联远程仓库

在创建远程仓库之前,项目其实已经有一些文件了,所以可以直接在他那个项目中再创建一个本地仓库,并把文件提交到本地仓库,最后再push到远程仓库即可,下面来模拟那个环境一步步操作。

  • 创建一个目录并添加一些文件
lu@E5:~/Temp$ mkdir MyTest
lu@E5:~/Temp$ cd MyTest/
lu@E5:~/Temp/MyTest$ echo "test" > test
lu@E5:~/Temp/MyTest$ echo "hello" > hello
  • 创建本地仓库并将文件都提交到仓库
lu@E5:~/Temp/MyTest$ git init
Initialized empty Git repository in /home/lu/Temp/MyTest/.git/

lu@E5:~/Temp/MyTest$ git add .

lu@E5:~/Temp/MyTest$ git commit -m "init"
[master (root-commit) 7be5b2a] init
 2 files changed, 2 insertions(+)
 create mode 100644 hello
 create mode 100644 test
  • 将本地仓库与远程仓库关联
    在这里关联远程(本地)仓库与一般我们关联互联网上的远程仓库有所不同,我们可以直接写本地的仓库路径即可。
lu@E5:~/Temp/MyTest$ git remote add origin ~/Temp/git_server/Test/
  • 提交代码到远程仓库
    注意坑来了,默认情况下直接push到远程(本地)仓库会出错的,如下:
lu@E5:~/Temp/MyTest$ git push -u origin master
Counting objects: 4, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 245 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /home/lu/Temp/git_server/Test/
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/home/lu/Temp/git_server/Test/

因为我们的远程仓库也只是一般的仓库而不是裸仓库,如果是裸仓库的话就不会出现这个问题了。
根据错误提示,我们可以进行相应的配置:

lu@E5:~/Temp/MyTest$ git config --global receive.denyCurrentBranch ignore

也可以直接修改配置文件:

lu@E5:~/Temp/MyTest$ vim ~/.gitconfig
在文件中添加以下内容即可
[receive]
    denyCurrentBranch = ignore

配置完后,我们再试着push一次看看。

lu@E5:~/Temp/MyTest$ git push -u origin master
Counting objects: 4, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 245 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To /home/lu/Temp/git_server/Test/
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

终于成功了?还早呢,虽然代码是提交上去了,但是我们再到远程仓库看看有木有文件吧。

lu@E5:~/Temp/MyTest$ cd ../git_server/Test/
lu@E5:~/Temp/git_server/Test$ ls -al
total 12
drwxr-xr-x 3 lu lu 4096 Oct 20 19:53 .
drwxr-xr-x 3 lu lu 4096 Oct 20 19:26 ..
drwxr-xr-x 8 lu lu 4096 Oct 20 20:05 .git

怎么回事呢,我们提交的2个文件并没有看到呢。我们在远程仓库看看状态和log记录。

lu@E5:~/Temp/git_server/Test$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    hello
    deleted:    test

lu@E5:~/Temp/git_server/Test$ git log
commit 7be5b2ab70e3a7aa14089e432a4066c935241f30
Author: lusyoe <lusyoe@163.com>
Date:   Thu Oct 20 19:57:26 2016 +0800

    init

可以看到文件是有提交到远程仓库的,只是还没有checkout 出来而已。那如何每次提交到远程仓库都使其自动checkout出文件来呢?这里就需要使用到git hook了,具体的概念这里不多讲,直接说使用吧。

添加git hook

git hook是一个非常强大的功能,它可以监听我们对仓库的修改,继而触发相应的逻辑命令。这里为了完成同事的需求,只说说post-update,就是提交有代码更新到仓库时需要执行的操作。

lu@E5:~/Temp/git_server/Test$ cd .git/hooks/

lu@E5:~/Temp/git_server/Test/.git/hooks$ ls
applypatch-msg.sample  post-update.sample     pre-commit.sample          pre-push.sample    update.sample
commit-msg.sample      pre-applypatch.sample  prepare-commit-msg.sample  pre-rebase.sample

可以看到默认有许多的hook脚本,我们只需要关注post-update.sample即可,下面来根据需求进行操作。

  • 重命名脚本
    首先重命名脚本,因为默认.sample结尾的是不会执行的,名字也不能乱改。
lu@E5:~/Temp/git_server/Test/.git/hooks$ mv post-update.sample post-update
  • 编辑脚本
    我们注释掉默认的操作指令exec git update-server-info,然后输入我们想要执行的操作命令。
lu@E5:~/Temp/git_server/Test/.git/hooks$ vim post-update
  #!/bin/sh
  #
  # An example hook script to prepare a packed repository for use over
  # dumb transports.
  #
  # To enable this hook, rename this file to "post-update".

  #exec git update-server-info
  # 添加以下三行即可
  unset GIT_DIR
  cd ..
  git checkout -f

好了,远程仓库修改完毕。

再次push远程仓库

我们再到本地仓库那添加一个文件然后push到远程仓库看看。

lu@E5:~/Temp/MyTest$ echo "demo" > demo

lu@E5:~/Temp/MyTest$ git add .

lu@E5:~/Temp/MyTest$ git commit -m "add demo"
[master 9da7116] add demo
 1 file changed, 1 insertion(+)
 create mode 100644 demo

lu@E5:~/Temp/MyTest$ git push
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /home/lu/Temp/git_server/Test/
   7be5b2a..9da7116  master -> master

去远程仓库看看是否有出现新文件。

lu@E5:~/Temp/git_server/Test$ ls -al
total 24
drwxr-xr-x 3 lu lu 4096 Oct 20 20:33 .
drwxr-xr-x 3 lu lu 4096 Oct 20 19:26 ..
-rw-r--r-- 1 lu lu    5 Oct 20 20:33 demo
drwxr-xr-x 8 lu lu 4096 Oct 20 20:33 .git
-rw-r--r-- 1 lu lu   18 Oct 20 20:33 hello
-rw-r--r-- 1 lu lu    5 Oct 20 20:29 test

后序

OK,非常成功,虽然像这种场景很少遇到,不过通过这一番操作能对git又多一点了解了。不得不说git对于开发人员来说实在是神器啊。

  • 13
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值