repository 是代码库,pull 是收取,push 是推送,remote 是远程主机,branch 是 repository 的分支。

创建 repository 有两种方法,一种是直接从当前目录创建,使用 init 命令,另一种是 clone,从现有 repository 创建。

一个 repository 就是一个文件夹,命令行上当前目录就代表当前的代码库,可以在这个文件进行增加,删除和修改文件的操作,可以通过 diff 命令来查看操作的结果。然后可以通过 add 命令来增加到 commit。

配置好 commit 之后可以使用 commit 来提交。

以上都是在单机的操作,要想和 github 同步的话需要配置一个 remote,通过 remote add 命令来添加 remote,添加的时候要起一个名字,自己的一般叫 origin。每个 repository 都有与自己关联的 remote。添加之后可以用 pull 和 push 来和这个 remote 进行同步。用 remote -v 来查看有哪些 remote。

自己的和别人的 remote 概念上讲是没有区别的,只是别人的 remote 不能 push。所以通常的做法是 github 上 fork 一个别人的remote,然后自己把本机的修改 push 到这个 forked 的 remote 上,也可以向作者申请 pull-request,请求作者合并你的修改。

由于有 branch,所以对 remote 操作的时候都要额外指定 branch才 可以。

一下是一些常用命令:

  • git version 查看版本信息
  • git config --global user.name "<Your Name>" 配置账户名字
  • git config --global user.email "<youremail@example.com>" 配置邮箱
  • git init 把当前文件夹设置为仓库
  • git status 查看当前仓库的状态
  • git add readme.txt 将文件提交
  • git add . 提交所有修改
  • git commit -m "<your commit message>" 确认提交修改
  • git diff 查看上次commit的的修改
  • git remote -v 查看有哪些remote连接
  • git remote add origin <URLFROMGITHUB> 增加一个远程库,这个库名字叫origin,参数填写来源网址
  • git remote set-url origin <URLFROMGITHUB> 重新设置remote的url
  • git push origin master 推送到origin库的master分支上
  • -u 设置关联的 upstream,下次 push 和 pull 就不需再填 origin master 了
  • git pull <REMOTENAME> <BRANCHNAME> 收取远程库的更新
  • git fetch --dry-run 在收取更新前线检查remote是否有变动
  • git clone <URLFROMGITHUB> 克隆一个仓库,它会自动创建文件夹(后面还需要用remote命令链接这个库)
  • git branch 查看分支
  • git branch <BRANCHNAME> 创建分支
  • git checkout <BRANCHNAME> 切换分支
  • git branch -m <NEWNAME> 重命名分支
  • git branch -d <BRANCHNAME> 删除分支
  • git merge <BRANCHNAME> 合并分支