git 创建远程仓库详解

git 新建仓库
假设本地已有git分支的情况下:

设定A做为服务器供所有用户公用
设定B做为客户端

在A上初始化一个空库:
~/mygittest$ mkdir gitserver

~/mygittest$ cd gitserver

~/mygittest/gitserver$ git init –bare

Initialized empty Git repository in /home/zhangyafei/mygittest/gitserver/

在B上初始化一个仓库:
~/mygittest$ mkdir gitclient

~/mygittest$ cd gitclient/

~/mygittest/gitclient$ git init
Initialized empty Git repository in /home/zhangyafei/mygittest/gitclient/.git/

添加一个文件并commit,这样会自动产生master分支:
~/mygittest/gitclient$ echo “git test file ” > test

~/mygittest/gitclient$ git add test

~/mygittest/gitclient$ git commit -m “test commit”
[master (root-commit) ae8c788] test commit
1 file changed, 1 insertion(+)
create mode 100644 test

~/mygittest/gitclient$ git branch
* master

添加远程git服务器地址(从本地库,指向裸库),即裸库视为本地库的服务器(注:因为公司安全软件限制的原因,不能使用ssh只能使用file了)
~/mygittest/gitclient$ git remote add origin file:///home/zhangyafei/mygittest/gitserver

将本地分支push到远程服务器上
~/mygittest/gitclient$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To file:///home/zhangyafei/mygittest/gitserver
* [new branch] master -> master

新建一个目录,克隆远程服务器的仓库:
~/mygittest$ mkdir gitclient2

~/mygittest$ cd gitclient2

~/mygittest/gitclient2$ git clone file:///home/zhangyafei/mygittest/gitserver
Cloning into ‘gitserver’…
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

~/mygittest/gitclient2$ cd gitserver/

~/mygittest/gitclient2/gitserver$ git log
commit ae8c7889268b07e4ebb14af19169327d499b1323
Author: zhangyafei [email protected]
Date: Tue Feb 28 17:04:44 2017 +0800

test commit

结果显示,取到了最新的提交

再切回gitclient
~/mygittest/gitclient$ git branch
* master

~/mygittest/gitclient$ git pull
You asked me to pull without telling me which branch you
want to merge with, and ‘branch.master.merge’ in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. ‘git pull ’).
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:
[branch “master”]
remote =
merge =

[remote ""]
url = 
fetch = 

See git-config(1) for details.

执行pull后显示以上提示。
也就是说,我们没有指定我们要从哪个remote 上面pull代码,也没有指定应该与哪个分支merge.
查看配置:
~/mygittest/gitclient$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “origin”]
url = file:///home/zhangyafei/mygittest/gitserver
fetch = +refs/heads/:refs/remotes/origin/

这个配置里没有说明remote 和 merge,所以提示说,如果你需要经常merge分支的话,那么请修改配置。

我们来看gitclient2,由于这是从远程服务器克隆到本地的仓库,所以有相关的配置:
~/mygittest/gitclient2/gitserver$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “origin”]
fetch = +refs/heads/:refs/remotes/origin/
url = file:///home/zhangyafei/mygittest/gitserver
[branch “master”]
remote = origin
merge = refs/heads/master

相比之下,只是多了指定master分支的远程和merge,
如果你不修改配置,那么你需要每次在使用pull,push指令时指定remote 和 merge(如果你不嫌麻烦的话) 
命令:git pull <远程主机名> <远程分支名>:<本地分支名>
git pull origin master : master
origin master 是远程origin上的master分支,master是本地分支

该命令的用法是 : 将”哪个远程”上的”哪个分支”拉取到本地仓库的”哪个分支”

如果想删除远程的分支,如果:左边的分支为空,那么将删除:右边的远程的分支
git push origin :test // 刚提交到远程的test将被删除,但是本地还会保存的

git push的用法:git push origin local:master //push本地local分支作为远程的master分支

~/mygittest/gitclient2/gitserver$ git branch -a (显示所有已知的分支)
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master

~/mygittest/gitclient2/gitserver$ git branch -r (仅显示远程分支)
origin/HEAD -> origin/master
origin/master

这里可以看出master就是本地分支,而orgin/master则是远程上命名为master的分支。
remotes/origin/master和origin/master的指向是相同的。
HEAD指向当前工作的branch,master不一定指向当前工作的branch

git config branch.master.remote origin
git config branch.master.merge refs/heads/master
使用这两个命令,为config配置了master分支的remote 和 merge

你可能感兴趣的:(工具及相关)