Push

$ git push -u origin master

-u让本地的master知道哪个remote branch来做fetch和push。

Fetch

Fetching changes from a remote repository

$ git fetch

只是更新remote/master的HEAD,并没有对local的master做改动。正因为fetch的这个特性,所以:

  • always fetch before you work
  • fetch before you push
  • fetch often

merge只是master与origin/master之间的merge,不是master直接与remote的merge。

Pull

git pull = git fetch + git merge

Remote操作

Checking out remote branches把remote的branch移到本地

$ git branch <branch_name> origin/<branch_name>

Pushing to an updated remote

如果你编辑了local的master,准备push到remote上的时候。发现别人已经对remote的master做了更改。这时,你要先fetch,把你的master与更新的origin/master做merge,然后再push。如果有confict,要先解决才能merge成功。大部分时候没有conflict。

Change remote url

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

Deleting a remote branch

$ git push origin :<branch_name>
$ git push origin --delete :<branch_name>

Collaboration Workflow

$ git checkout master #start at master
$ git fetch # see if changes happen in remote master
$ git merge origin/master # 与remote master保持sync
$ git checkout -b feedback_form # start new feature in a new branch

# 工作内容commit到新branch
$ git add feedback.html
$ git commit -m 'Add customer feedback form'

# 把new branch push到remote之前
$ git fetch # 看下remote那里有没有更新
$ git push -u origin feedback_form # push the whole branch,not commit

collaborator角度

$ git checkout master
$ git fetch
$ git merge origin/master # 如果还有其他人的commit,sync到自己的master
$ git checkout -b feedback_form origin/feedback_form # 把我的branch clone下来到本地并切换进去。
$ git log # 查看变化
$ git show <checksum> # 查看细节
$ git commit -am 'Add tour selector to feedback form'
$ git fetch
$ git push

再回到我这边

$ git fetch
$ git log -p feedback_form..origin/feedback_form # 查看我本地和remotre的feedback_form的变化
$ git merge origin/feedback_form # 把它merge到我的feedback_form branch

这时,我,remote和collaborator三者的feedback_form都是sync的。可以把它merge到master里。

$ git checkout master
$ git fetch
$ git merge origin/master # 确保我这里的master是最新的master。
$ git merge feedback_form
$ git push

How to fetch all git branches

clone一个特定的branch

$ git clone -b <branch_name> <http:// address>

Fork and pull request

参与别人的项目,fork,编辑,然后pull request。