git-work-flows

git工作流,持续踩坑

因为我同时使用两个操作昔日,因此github仓库就产生一个同步的问题。但是只会 add commit push 三步,因此学习一下

工作区:本地文件。git clone 下载到工作区,git pull 更新到工作区
暂存区:add 提交到暂存区。
本地仓库: commit 提交到本地仓库。fetch 从远程更新到本地仓库
远程仓库:push 提交到远程仓库。

工作流

  1. git clone 后切换分支
1
2
3
git clone <remote>
git checkout -b feature/windows
git branch -a
  1. 写点东西提交
1
2
3
4
# 提交到本地
git diff # 本地仓库和本地工作区文件的diff
git add xxx
git commit # 使用 vim 写 commit 信息
  1. push 到仓库
  • 经过这一步,仓库应该存在两个branch
1
2
3
4
5
6
7
8
9
10
# 分支同步:main 分支
git checkout main
git fetch origin
git diff
git pull origin main
git checkout feature/windows

# rebase: 先合并main分支commit,在合并本地的commit
git rebase main
git push -f origin feature/windows
  1. main 远程分支合并
  • 在 github 类似的地方提交一个 pr(pull request),将 feature/windows 合并到 main 分支里
  • 经过项目开发者审查,会进行合并。
  • 删除远端 feature/windows
  • 删除本地分支
1
2
3
4
git checkout main
git branch -D feature/windows
# 最后需要我们在进行合并一下
git pull origin main

因此创建三个分支来维护。

子模块

  1. 将子模块文件夹加入到 .gitignore 文件内容中,这样主项目就能够无视子项目的存在。
  2. git submodule 使用
1
2
3
4
# 将仓库clone下来,出现一个 .submodule 文件
git submodule add https://github.com/chaconinc/DbConnector

git submodule update --remote <repo>

版本回退

最新的不一定是最好的,还可能是bug 多多的

1
2
3
4
5
6
7
8
9
10
11
git branch -a  # 先查看所有分支

# 有的项目比较存在不同的tag
git checkout <branch> <tag>

# 有的项目存在版本 分支
git checkout <branch>

# commit 回退
git log
git checkout <comit_hash>

删除

1
2
3
git remote rm -r --cached <dir/filename>
git commit -m "delete"
git push origin main

但是现在比较好的IDE只需要点击一下鼠标就行,也是很方便。