Git建立本地远程仓库
2016-10-20 20:42
453 查看
前言
今天有个同事突然问我:怎么创建一个本地的远程仓库,并且还要能看到仓库文件,能够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对于开发人员来说实在是神器啊。相关文章推荐
- git先有本地目录然后再建立远程仓库的处理方法
- git 将自己本地的代码建立分支推送到远程仓库
- git 远程仓库和本地仓库建立连接
- GIT: 远程建立一个仓库,然后复制到本地
- git本地建立仓库推送到远程
- git clone 远程仓库到本地(或服务器)非空目录下
- windows下多个git账号配置并且将本地仓库项目推送到远程仓库
- git本地仓库重新建立错误,无法push。
- 本地git仓库提交远程失败的解决办法
- Git实现从本地加入项目到远程仓库
- 用命令将本地项目上传到git 远程仓库
- 使用git建立远程仓库,让别人git clone下来
- 使用git工具,在本地建立一个数据仓库,保存日常工作日志
- Linux在本地建立git仓库管理代码
- 在本地用命令行创建一个git仓库,并推送到远程
- git建立本地仓库、gitHub上传项目以及git和gitHub的关系
- 建立github本地仓库与远程仓库
- git把本地项目push到远程仓库
- 创建局域网内远程git仓库,并将本地仓库push推到远程仓库中