新建项目

从已有项目中导入

1
2
3
4
# 从github中导入项目
$ git clone https://github.com/example/example.git
# 新建feature分支
$ git checkout -b my-feature

本地新建项目

1
2
3
4
5
# 在github中新建仓库
# 在本地IDEA中设置Git Remotes
$ git push origin master
# 新建feature分支
$ git checkout -b my-feature

在feature分支中开发

写bug….

1
2
3
4
5
6
7
8
# 查看修改了哪些文件
$ git diff
# 将新建的文件加入local git (IDEA中可以直接加入)
$ git add <changed_file>
# 提交
$ git commit
# 推送到remote
$ git push origin my-feature

提交代码

在开发中, 常见的情况是当我们准备向master分支pull request时, master分支又有更新了, 如下图的update

Snipaste_2024-08-10_22-49-13

那么, 我们可能要测试一下代码在新的update下是否一切正常, 为此, 我们需要先将feature分支 rebase onto master, 具体做法:

1
2
3
# 更新local git master分支
$ git checkout master
$ git pull origin master
1
2
3
# my-feature分支变基
$ git checkout my-feature
$ git rebase master
1
2
# 重新提交到remote git
$ git push -f origin my-feature

PR

1
# 代码被(自己)审查之后, 将 merge 或者 squash and merge(大多数情况) 进主分支

善后工作

1
2
3
4
5
6
7
8
# 删除Remote上的feature分支
# 删除local的feature分支
$ git checkout master
$ git branch -D my-feature
# 拉取更新
$ git pull origin master
# 新建feature分支, 开始下一次的开发
$ git checkout -b my-feature

参考资料:

十分钟学会正确的github工作流,和开源作者们使用同一套流程