每次组队项目队员都不会用git合作,总是一个文件在QQ微信传来传去

所以我觉得有必要整理一套简单的笔记以供使用

🍉 工作流程

笔记源于:https://www.bilibili.com/video/BV19e4y1q7JJ

我们已经有一个项目的远端(Remote)仓库且其中有主分支main(master)

  1. 需要将Remote仓库复制到本地(Local)
1
git clone <https://github.com/example/example.git>
  1. 建立分支(feature branch)用于开发,下面命令会复制一份main到新分支my-feature
1
git checkout -b my-feature
  1. 修改磁盘Disk(刚下载到本地)的源码并保存
1
git diff # 查看changes
  1. 将修改的文件添加至Local
1
git add <changed_file>

使用 git add .添加所有文件

  1. 保存文件的修改
1
2
3
git commit
# or
git commit -m “提交项目” # 添加修改提示

Local新增一个commit,feature branch不同于main branch了

  1. 将Local git的内容同步到Remote git
1
git push origin my-feature
  1. Remote的main branch更新,需要同步到my-feature branch
1
2
git checkout main # 切换到main branch
git pull origin master
  1. 回到feature branch,尝试同步main branch的代码
1
2
git checkout my-feature
git rebase main
  • 建议用rebase而不是merge:
    • rebase可以保留节点
    • rebase是在新的main branch后添加my-feature的修改
  1. 如果出现了rebase conflict则需要手动选择代码
  2. 更新完后同步至远程(由于做了rebase所以要force)
1
git push -f origin my-feature
  1. Pull Request: 在Remote将新代码合并到main

main branch维护者进行Squash and merge,把分支上所有的改变合并成一个然后commit到main branch

一般会delete branch删除Remote的feature branch

  1. Local git的处理,同样删除feature branch
1
2
git checkout main
git branch -D my-feature
  1. 同步远程最终内容
1
git pull origin master

其他常用命令

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