奇忠诚 发表于 2018-1-15 09:35:55

git remote/client 学习笔记

  搞了两天git,尝试了几种git server的创建方法,最简单的就是apache http,最后还是选定使用SSH,因为安全、操作方便、可创建账号、client必须上传public key.
  了解更多:http://www.jedi.be/blog/2009/05/06/8-ways-to-share-your-git-repository/
  http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide
  git手册:http://kernel.org/pub/software/scm/git/docs/
  注:本文基于Mac OSX SL环境。
  一、安装git并配置用户名和密码
  已安装MacPorts的可以通过命令行安装:
  $ sudo port install git-core
  或者安装gitgui: http://code.google.com/p/git-osx-installer/
  配置全局用户名和密码:
  $ git config --global user.name "Elf Sundae"
  $ git config --global user.email youruser@email.com
  上述操作会在你的home directory创建 ~/.gitconfig文件。查看:
  $ git config --global --list
  你也可以在单独项目中设置特定的user.name和user.email,例如工作邮箱和私人邮箱区分开,方法是在remote时git config,不加--global即可。
  二、服务器配置
  1.设置服务器为静态ip,例如192.168.18.188
  2.如果需要在外网访问,在路由器添加端口映射,SSH一般用22,可以在路由器映射一个端口(如12345)到服务器的22端口。再申请一个动态DNS解析服务(http://www.dyndns.com/)
  3.在服务器创建一个standard的账户用于远程SSH登陆,名字随便起,并设置密码。你可以为每个客户端创建一个账户,但是没多大意义。
  4.开启ssh登陆:
https://pic002.cnblogs.com/images/2011/142469/2011070611184726.png
  5.配置sshd_config:(需要管理员权限)
  $ sudo open /etc/sshd_config

找到这行改成这样#PermitRootLogin yes
PermitRootLogin no
#PasswordAuthentication no
PasswordAuthentication no
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
#UsePAM yes
UsePAM no  至此,服务器地址类似这样:ssh://@ipAddressOrDomain/path/to/git例如:
  ssh://git@192.168.18.188
  ssh://git@example.com:12345
  6. 设置PATH
  log out管理员账户,log in git账户,
  

echo 'export PATH="$PATH:/usr/local/git/bin/"' >> ~/.bashrc  

  三、客户端安装git并获取Public key
  cd
  mkdir -p .ssh
  cd .ssh
  ssh-keygen -f elfmacbook_git_key -t rsa -q
  ssh-add -K elfmacbook_git_key
  cp elfmacbook_git_key.pub ~/Desktop
  其中passphrase可输可不输,但输了比较安全,且一定要记下,在OSX下在第一次使用时可以勾选记忆passphrase到系统keychain中。
  如果你创建pub后就要发给服务端(见下四),不用复制到桌面了,直接拷贝到剪切板:
  $ cat ~/.ssh/elfmacbook_git_key.pub | pbcopy
  或者:(http://help.github.com/mac-set-up-git/)
  $ cd ~/.ssh
  $ ssh-keygen -t rsa -C "your_email@email.com" (询问保存路径时直接敲回车)
  输入passphrase

  $ cat>  四、客户端提交public key到服务器,写入服务器的authorized_keys。
  每个客户端都要执行第三步,将获取到的pub文件用记事本打开将内容提交到服务器(只有一行),服务器端用支持不换行的文本编辑器(如Smultron)打开
  /Users/git/authorized_keys,写入客户端的pub key。每个客户机占一行。
  或者命令行:
  $ echo "" > /Users/git/.ssh/authorized_keys
  完成后重启服务器。
  五、测试
  ssh到git server 或在服务端git账户登陆
  $ ssh git@192.168.18.188
  $ cd
  $ mkdir Mytest.git
  $ cd Mytest.git
  $ git init --bare
  $ exit
  Add the remote repository and push:
  $ cd ~/Desktop/MyTestProject

  $ git remote add origin ssh://git@192.168.18.188/Users/git/Mytest.git

  $ git push origin master
页: [1]
查看完整版本: git remote/client 学习笔记