Skip to content

如何在Github上贡献代码

马金凯 edited this page Nov 20, 2018 · 1 revision

操作顺序

  1. 找到心仪的开源项目,Fork它,Star它
  2. 根据自己的项目地址Clone到本地
  3. 将被Fork的项目添加为upstream,用以跟踪其所有修改
  4. 创建一个新的分支(补丁/特性都可以,名称跟你要开发的内容相关)
  5. 编写你的代码
  6. 提交N次代码(如果想把这N次提交合并成一次,显得漂亮一些的话,可以执行提交合并[1])
  7. 将创建的分支推送到origin
  8. 在Github上面发起Pull Request请求等待合并
  9. 如果代码被合并了,可以删除自己的分支
  10. 拉取upstream的最新提交
  11. 创建新的分支
  12. 将新分支rebase到upstream的最新提交点
  13. 重复5~12即可

[1] 提交合并

我们在本地开发时,可以随时去提交写好的代码,但是这样会导致提交历史比较多,推送到远端或者发起Pull Request显得比较杂乱,这时我们就可以将几次提交或者全部提交合并成一次提交,执行顺序如下:

  • 切换到需要合并提交的分支
  • 输入git rebase -i HEAD~N(N=你想合并的最后几次提交),看到如下输出:
pick 708fa0a commit message A
pick 8baaa26 commit message B
pick dba177a commit message C

# Rebase 2d2fb07..bfa62a9 onto 2d2fb07 (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  • 根据Commands注释修改成你想要的效果,例如:
pick 708fa0a commit message A
squash 8baaa26 commit message B
squash dba177a commit message C

# ...
  • 输入:wq保存退出,会出现如下提交信息:
# This is a combination of 3 commits.
# The first commit's message is:
commit message A

# This is the 2nd commit message:
commit message B

# This is the 3rd commit message:
commit message C
  • 根据实际情况修改提交信息
  • 再次输入:wq就会看到合并完毕的提示信息
  • 这时候Rebase合并就成功了,并且三次提交也合并在一起成为一次提交