Git

前些日子因为某些原因,需要在windows上创建一个remote仓库。

由于实在是太麻烦而且时间紧急,就直接用了gitstack。

wKioL1RCMdHD3rdsAAA8KNzGzeM183.jpg

发现这个东西居然需要付费,未认证时只能创建两个用户。

其实对我而言足够了,但不是长久之计。

好在后来又有了一台机器装了linux,这就回到正轨。

因为我也是一直使用别人弄的remote仓库,这次便把创建过程记录下来。

git init是第一步,我经常很理所当然地进行如下操作:


[root@alvez-VM ~]# mkdir myProject
[root@alvez-VM ~]# cd myProject/
[root@alvez-VM myProject]# git init 
Initialized empty Git repository in /root/myProject/.git/
[root@alvez-VM myProject]# touch myProject.md
[root@alvez-VM myProject]# echo "##myProject">>myProject.md 
[root@alvez-VM myProject]# git add .
[root@alvez-VM myProject]# git commit -m"add md file"
[root@alvez-VM myProjet]# git remote add origin ssh://root@localhost/~/myProject/.git
[root@alvez-VM myProjet]# git remote -v
origin  ssh://root@localhost/~/myProject/.git (fetch)
origin  ssh://root@localhost/~/myProject/.git (push)

有一点需要注意,git init还是git init --bare,两者的区别是什么?

加了--bare的就是bare repository嘛 (╯‵□′)╯︵┻━┻

简单来说,bare仓库的意义在于在服务器集中管理,以管理多个开发者的提交。

即使没有加--bare,开发人员仍然可以对该工程进行clone,只不过向这个仓库push时会提示如下信息并失败:


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'.

若只是在server中对本地进行push则没有问题,也就是说git init只是给自己一个人玩的。

git init后的目录下包括两样东西,.git和真实的工作目录,而bare仓库下仅仅是.git。

也就是说,上面第一步就错了,对于init,只要在目录下git init --bare就足够了,也不需要remote add。

这样就可以clone并push了


$ git clone root@serverip:myProject
Cloning into 'myProject'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done
$ cd myProject/
$ touch pushTest.txt
$ echo "push test">pushTest.txt
$ git add .
warning: LF will be replaced by CRLF in pushTest.txt.
The file will have its original line endings in your working directory.
$ git commit -m"test file"
[master (root-commit) 05f0722] test file
warning: LF will be replaced by CRLF in pushTest.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 pushTest.txt
$ git push

 

clone或者push时会提示输入密码,输入个密码也未尝不可,

几个开发者使用一个用户名密码也没什么,每个人一套各自的用户名密码就上几次userAdd也没什么意义。

不如让开发者生成key pair并把公钥给我管理。

可以通过以下命令生成(rsa或者dsa,或者看情况加入其他参数):


ssh-keygen -t rsa

 

执行后会在用户目录下的.ssh下生成相应密钥,将.pub文件发给管理员即可。

假设管理员拿到的文件名为id_rsa.test.pub并放在用户目录下,执行如下操作加入权限并确认:


[root@alvez-VM ~]# cat id_dsa.test.pub >> ~/.ssh/authorized_keys 
[root@alvez-VM ~]# cd .ssh/
[root@alvez-VM .ssh]# cat authorized_keys

 

好了,以上就是远程库的创建与认证。

原文地址:https://www.cnblogs.com/kavlez/p/4033580.html