Thursday, May 2, 2013

git新手指南

在网上搜来搜去,最后发现还是下面这个最有用,简洁明了,对于新手来说零失败(总结翻译见后):
https://help.github.com/articles/set-up-git

这个git virtual reference 也很棒:
http://marklodato.github.io/visual-git-guide/index-en.html


然后就是入门宝典:http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository

常用命令:
http://gitref.org/creating/

read bookmark:
http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository 
-----------------新手指南--------------------
第一步: 下载并安装git
 
第二步: 打开git bash, 设置用户名和电子邮件地址,命令如下:

$git config --global user.name "yumei165"
$git config --global user.email "yumei165@example.com" 
 
注: 如果不同的项目想使用不同的用户信息,比如有一个项目personal_repo,先设置工作目录到personal_repo,
然后重新设置(注意这里不使用global关键字)
 
$cd personal_repo path
$git config user.name "other name"
$git config user.email "differentemail@email.com" 
 
第三步: 新建自己的项目
 1, 在本地电脑新建项目(不用下面的命令直接操作也可以):
 
 $mkdir Hello-World
 $cd Hello-World 
 
 2, 变为GIT项目,新建一个文档README
 
 $git init
 $touch README 
 
 3, 把本地项目和github链接起来
  3i, 在github网站上登入账户,新建项目
  3ii, 在本地bash输入以下命令,这样本地git项目对应到github中的Hello-world项目,并取名为origin
      (也可以是别的名字,origin是默认名)
  $git remote add origin https://github.com/username/Hello-World.git 
 
 4, 把本地项目内容存到github上对应项目中,三部曲: stage-commit-push
 
  $git add README anotherFile
  $git commit -m 'my first commit'
  $git push origin master
  --------other useful commands-------
  $git status -s (s for short)
  $git add . (files in current and all sub-directories)
  $git add * (files in current directory) 
 
 注: master是origin的主干默认名,如果需要可以建立分支,并在分支中存储本地项目的修改内容  
 
 5, 在分支中工作
 $git branch mybranch
 $git checkout mybranch 
 或者
 $git checkout -b mybranch
 #switch between branches
 $git checkout master 

 合并分支到master并删掉分支
 $git checkout master
 $git merge mybranch 
 $git branch -d mybranch
 
第四步: 在别人的项目上工作
 1, 在目标项目上点击fork,之后项目就会被复制到自己的github目录中
  
 2, 现在从自己的github里把项目复制到本地电脑中:
 
 $git clone https://github.com/myUserName/Spoon-Knife.git 
 
 3, clone来的本地项目和自己github上fork来的项目是通过origin对应链接的,如果想和别人的源项目接,
    用如下命令,这样源项目的命名为upstream,最后一行命令用来同步更新
 
 $cd Spoon-Knife
 $git remote add upstream https://github.com/originalUserName/Spoon-Knife.git
 
 4, 本地项目和origin之间的联系和第三步相同。如果需要和源项目upstream联系,用如下命令:
  4i, 同步更新
 
  $git fetch upstream
  $git merge upstream/master
 
  or
  $ git pull upstream
  注:如果正在修改某个文件,为了避免冲突应该选择第一种方法 
  
  4ii, 把自己的更新送到源项目upstream:
    pull request
    基本都在网上完成,这里就不写了

第五步: 删除项目
  github repository->settings->Delete this repository
 
------------------------------------------
常用命令:
ls -a: all files including hidden 
 

No comments:

Post a Comment