本文主要是介绍Linux Git从零开始(以Ubuntu12.04为例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Git在好多公司都用于代码管理,这里就从零开始介绍如何使用Git进行项目管理。
1.注册GitHub
2.在本地安装Git
sudo apt-get install git git-core
sudo addgroup github #添加github用户组
sudo adduser --ingroup github git #在github用户组添加git用户
$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libssl-dev
之后解压源码包
tar xzvf git-1.8.1.2.tar.gz
cd git-1.8.1.2
安装的时候可以简单的使用(只有当前用户可用,可参考源码目录下INSTALL文件):
$ make
$ make install
或者使用(所有用户可用):
$ ./configure
sudo make prefix=/usr all doc info # 当前用户安装
# make prefix=/usr install install-doc install-html install-info # root用户安装
如果想以后升级git可以跟踪源码
git clone git://git.kernel.org/pub/scm/git/git.git
3.配置本地Git环境
(1)设置用户名
$ git config --global user.name "Your Name Here" # 设置默认用户名(注册GitHub时使用的用户名)
(2)设置邮件
$ git config --global user.email "your_email@example.com" # 设置默认邮件地址(注册GitHub时使用的邮件地址)
(3)设置密码缓存
$ git config --global credential.helper cache # Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600' # Set the cache to timeout after 1 hour (By default git will cache your password for 15 minutes. You can change this if you like. Setting is in seconds)
4.在GitHub上部署代码
(1)新建项目
(2)将代码同步到本地
$ cd /home/github/ #假设github目录为我的目录
$ git init
$ git clone "远程代码地址"
(3)(编写)修改代码
$ cd FindFile/
在该目录下编写或者修改代码(可以直接将写好的代码复制到这里,然后修改代码的属主为github用户)。
$ git status
将文件改变添加到git:
$ git add modified_file #请按照自己的内容修改
准备提交:
$ git commit
添加修改备注,保存,然后提交。
$ git push
按照要求输入用户名和密码,完成代码修改。
5.为GitHub添加SSH Keys
使用git clone命令从github上同步github上的代码库时,如果使用SSH链接,而你的SSH key没有添加到github帐号设置中,系统会报下面的错误:
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
这时需要在本地创建SSH key,然后将生成的SSH key文件内容添加到github帐号上去。
创建SSH key的方法很简单,执行如下命令就可以:
cd ~/.ssh # 切换到git用户的工作目录下的.ssh/目录下
ssh-keygen -t rsa -C "usr.email" # 用户的邮件地址
然后系统提示输入文件保存位置等信息,连续敲三次回车即可,生成的SSH key文件保存在中~/.ssh/id_rsa.pub
接着拷贝.ssh/id_rsa.pub文件内的所以内容,将它粘帖到github帐号管理中的添加SSH key界面中。
打开github帐号管理中的添加SSH key界面的步骤如下:
1. 登录github
2. 点击右上方的Accounting settings图标
3. 选择 SSH Key
4. 点击 Add SSH key
在出现的界面中填写SSH key的名称,填一个你自己喜欢的名称即可,然后将上面拷贝的~/.ssh/id_rsa.pub文件内容粘帖到key一栏,在点击“add key”按钮就可以了。
添加过程github会提示你输入一次你的github密码
通过使用如下命令来测试SSH Key成功。
ssh -v git@github.com
如果看到如下结果则表示成功。
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
Hi twlkyao! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Connection to github.com closed.
Transferred: sent 2984, received 2704 bytes, in 0.7 seconds
Bytes per second: sent 4349.1, received 3941.0
debug1: Exit status 1
添加完成后再次执行git clone就可以成功克隆github上的代码库了。
这篇关于Linux Git从零开始(以Ubuntu12.04为例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!