每次组队项目队员都不会用git合作,总是一个文件在QQ微信传来传去
所以我觉得有必要整理一套简单的笔记以供使用

🍉 工作流程
笔记源于:https://www.bilibili.com/video/BV19e4y1q7JJ
我们已经有一个项目的远端(Remote)仓库且其中有主分支main(master)
- 需要将Remote仓库复制到本地(Local)
1
| git clone <https://github.com/example/example.git>
|
- 建立分支(feature branch)用于开发,下面命令会复制一份main到新分支my-feature
1
| git checkout -b my-feature
|
- 修改磁盘Disk(刚下载到本地)的源码并保存
- 将修改的文件添加至Local
使用 git add .
添加所有文件
- 保存文件的修改
1 2 3
| git commit
git commit -m “提交项目”
|
Local新增一个commit,feature branch不同于main branch了
- 将Local git的内容同步到Remote git
1
| git push origin my-feature
|
- Remote的main branch更新,需要同步到my-feature branch
1 2
| git checkout main git pull origin master
|
- 回到feature branch,尝试同步main branch的代码
1 2
| git checkout my-feature git rebase main
|
- 建议用rebase而不是merge:
- rebase可以保留节点
- rebase是在新的main branch后添加my-feature的修改
- 如果出现了rebase conflict则需要手动选择代码
- 更新完后同步至远程(由于做了rebase所以要force)
1
| git push -f origin my-feature
|
- Pull Request: 在Remote将新代码合并到main
main branch维护者进行Squash and merge,把分支上所有的改变合并成一个然后commit到main branch
一般会delete branch删除Remote的feature branch
- Local git的处理,同样删除feature branch
1 2
| git checkout main git branch -D my-feature
|
- 同步远程最终内容
其他常用命令
1 2 3 4 5 6 7 8 9 10 11
| git config --list
git status
git branch
git remote
|
其他材料推荐:
参考材料
https://www.bilibili.com/video/BV19e4y1q7JJ
https://zhuanlan.zhihu.com/p/265864717
https://git-scm.com/docs