git/github 使用流程

Posted on 2023-02-26 17:29  lyc2002  阅读(24)  评论(0编辑  收藏  举报
# 克隆远程仓库到本地
git clone https://github.com/example/example.git

# 创建一个新的分支(复制一份当前的 branch 到新 branch 上),git 会把这个 branch 上面的所有源文件同步给硬盘
git checkout -b my_feature

# 修改源文件,保存后硬盘上的文件已改变,但此时 git 一无所知
vim test.cpp

# 查看硬盘上的改变和 git 保存的分支有什么区别
git diff

# 把修改的文件告知 git
git add test.cpp

# 把修改真正放到 git 里
git commit -m "update"

#-----------------------------------------------------------------------------

# 如果 main branch 没有更新,就直接告知 github,github 上会多一个 branch
git push origin my_feature

#-----------------------------------------------------------------------------

# 如果修改完代码后,main branch 又有更新,假如多了 update 这个 commit,那么需要测试我的 my_feature 在 update 版本下是否好用,所以要将 main branch 的更新同步到 my_feature

# 切换到 main branch
git checkout main

# 把远端的 main 同步到本地的 main
git pull origin master

# 回到 my_feature
git checkout my_feature

# 同步新 main 的改变
git rebase main

# 把本地 branch 给 push 到 github 上
git push -f origin my_feature
#-----------------------------------------------------------------------------

# 在 github 上,点击 pull request,表示 requset 这个项目的主人把我这个新的分支的改变 pull 到项目里去
# 当 main branch 的维护者审查完你的代码后,用 squash and merge,把这一个分支上的所有改变合并成一个改变,把这个 commit 放到 main branch 上
# 一般情况下,还会把这个 my_feature branch 在 github 上删掉

# 切换到 main branch
git checkout main

# 将 my_feature branch 从本地删除
git branch -D my_feature

# 拿到最新的 main branch
git pull origin master