Git之初体验 持续更新

本文主要通过 git来同步本地与线上服务器项目,从而熟悉git管理的一些常用命令,非新手请跳过

初次接触git

  1. https://github.com 注册帐号
  2. 登陆后在右上角点击 + 号,选择并点击 New repository 新建代码仓库,私人项目选择private,公开项目可选择public
  3. 成功建立repository后,官方提供HTTP/SSH,以下是HTTP方法的操作命令:
  4. touch README.md 
    git init   //初使化目录,执行后当前目录将生成.git文件夹,其中保存了一些该项目的信息
    git add README.md //将文件添加到缓存区,于svn add类似
    git commit -m "first commit" //将缓存区提交到本地仓库,-m后面是自定义信息,会记录到日志
    git remote add origin https://github.com/fuemoshi/test.git //将本地仓库添加到远程缓存区,origin这里可自定义
    git push -u origin master //将仓库推送到远程目录,这里master是默认建立的主分支名称

进入本地 /www/test 目录,执行以上命令,到git commit 时,提示:

Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 0 files changed
 create mode 100644 README.md

按提示,进行git本地配置

git config --global user.name "username" //配置全局github 帐号
git config --global user.email  username@github.com //配置全局github邮箱
git config --list //查看当前配置

 继续执行至 git push -u origin master,此时要输入用户名密码,push成功后,即可在github网站的个人管理界面,看到test项目

 

深入git - 1 使用ssh方式操作git

使用ssh方式提交,和http方式基本一样,只不过add的地址不同

git remote add origin git@github.com:fuemoshi/test.git //注意这里是 git@github.com

在相同项目下执行后,报

fatal: remote origin already exists. //因为前面的操作已经为远程建立了origin

这里可以用新的名字,或者用

git remote rm origin //删除远程缓存区

然后执行

git remote add origin git@github.com:fuemoshi/test.git
git push -u origin master

报错

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

这是由于ssh需要安全链接,参考官网步骤

https://help.github.com/articles/generating-ssh-keys

主要命令如下:

ssh-keygen -t rsa -C "fuemoshi@gmail.com" //将在~/.ssh/ 目录下生成 id_rsa私钥 和 id_rsa.pub公钥
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa //加入到ssh agent

如果执行时报错

Could not open a connection to your authentication agent.

执行下面命令即可:

ssh-agent bash

 

深入git -2 使用git进行同步更新

//登录线上服务器,在相应目录下执行
git init git add remote origin github@github.com:fuemoshi
/test.git git pull origin master //将远程origin仓库合并、更新入当前目录分支

 

 

持续更新....

posted @ 2014-08-21 14:39  _Simba_  阅读(237)  评论(0)    收藏  举报