当我们和团队成员在同一个项目合作时,我们需要提交自己的MR走review流程,直到MR被merge,之间有哪些操作是常用的呢?
fork upstream并时时和upstream保持一致
我们会需要先fork项目的官方repo到我们自己的项目空间,这时我们就有了和当前官方项目一样的代码,但是当官方项目有更新后,我们则需要用以下步骤让我们的代码和官方代码保持一致。
[wlin@dhcp-136-3 botas] # 查看我们现在关联的branch
[wlin@dhcp-136-3 botas]$ git remote -v
origin git@gitlab.cee.redhat.com:wlin/botas.git (fetch)
origin git@gitlab.cee.redhat.com:wlin/botas.git (push
[wlin@dhcp-136-3 botas] # 确认我们关联的branch是wlin/botas.git,即是我们forked的repo
[wlin@dhcp-136-3 botas] # 而我们的目标是期望其和被forked的、和官方的devops/botas.git保持一致,则我们需要如下步骤:
[wlin@dhcp-136-3 botas] # 关联官方的repo并确认结果
[wlin@dhcp-136-3 botas]$ git remote add upstream git@gitlab.cee.redhat.com:devops/botas.git
[wlin@dhcp-136-3 botas]$ git remote -v
origin git@gitlab.cee.redhat.com:wlin/botas.git (fetch)
origin git@gitlab.cee.redhat.com:wlin/botas.git (push)
upstream git@gitlab.cee.redhat.com:devops/botas.git (fetch)
upstream git@gitlab.cee.redhat.com:devops/botas.git (push)
[wlin@dhcp-136-3 botas] #抓取原仓库的修改文件:
[wlin@dhcp-136-3 botas]$ git fetch upstream
From gitlab.cee.redhat.com:devops/botas
* [new branch] adhoc-v2 -> upstream/adhoc-v2
* [new branch] filter-filter -> upstream/filter-filter
* [new branch] graceful-disabled-release -> upstream/graceful-disabled-release
* [new branch] master -> upstream/master
* [new branch] release -> upstream/release
[wlin@dhcp-136-3 botas] # 切换到master分支
[wlin@dhcp-136-3 botas]$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
[wlin@dhcp-136-3 botas] # 合并远程的master分支:
[wlin@dhcp-136-3 botas]$ git merge upstream/master
Already up to date.
[wlin@dhcp-136-3 botas] # 我们本地的已经和upstream保持一致
[wlin@dhcp-136-3 botas] # 则如果不同,则需要将最新的本地的更新git push到你的gitlab repo上
以上步骤中,绑定upstream仅需做一次,后面的fetch/checkout/merge/push都是每次更新代码时必须的。
提交MR和更新MR
我们fork后,通过以下步骤提交并创建我们的MR
[wlin@dhcp-136-3 botas] # 创建新的branch
[wlin@dhcp-136-3 botas]$ git checkout -b new_branch
[wlin@dhcp-136-3 botas] # 更新code
[wlin@dhcp-136-3 botas] echo "testing" >> new_branch
[wlin@dhcp-136-3 botas]$ git add new_branch
[wlin@dhcp-136-3 botas]$ git commit -m 'submit new MR'
[new_branch 968c1cd] submit new MR
1 file changed, 1 insertion(+)
create mode 100644 botas/new_branch
nothing added to commit but untracked files present (use "git add" to track)
[wlin@dhcp-136-3 botas] # dry run push确定push到确定的repo后git push
[wlin@dhcp-136-3 botas]$ git push --set-upstream origin new_branch --dry-run
To gitlab.cee.redhat.com:wlin/botas.git
* [new branch] new_branch -> new_branch
Would set upstream of 'new_branch' to 'new_branch' of 'origin'
[wlin@dhcp-136-3 botas]$ git push --set-upstream origin new_branch
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 343 bytes | 343.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for new_branch, visit:
remote: https://gitlab.cee.redhat.com/wlin/botas/-/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch
remote:
To gitlab.cee.redhat.com:wlin/botas.git
* [new branch] new_branch -> new_branch
Branch 'new_branch' set up to track remote branch 'new_branch' from 'origin'.
[wlin@dhcp-136-3 botas] # 则我们点击那个remote的merge_requests的url就会跳转创建MR的web界面,则可以完成创建。
更新MR
通常我们希望提交/更新多次,一个MR仍是只有一个commit,则我们可实现如下
[wlin@dhcp-136-3 test_git] # 进行更新并添加更新
[wlin@dhcp-136-3 test_git]$ git add cara_testing
[wlin@dhcp-136-3 test_git] # 创建新的commit
[wlin@dhcp-136-3 test_git]$ git commit "second commit"
error: pathspec 'second commit' did not match any file(s) known to git
[wlin@dhcp-136-3 test_git]$ git commit
[cara_testing 6b1099f] second commit
1 file changed, 1 insertion(+)
[wlin@dhcp-136-3 test_git]$ git log
commit 6b1099f508629b5c0b8951a762acd14c165560f7 (HEAD -> cara_testing)
Author: Cara Wang <wlin@redhat.com>
Date: Thu Jul 8 13:03:23 2021 +0800
second commit
commit 5a36f988a79931ddb26798331c26d3d5ccced28f (origin/cara_testing)
Author: Cara Wang <wlin@redhat.com>
Date: Thu Jul 8 12:27:38 2021 +0800
add new file
commit e6968dc2d1c86b2b60d75feddcf8e7ea310b10d3 (origin/master, origin/HEAD, master)
Author: Chenglong Wang <chenwang@redhat.com>
Date: Wed Dec 23 04:37:54 2020 +0000
Update ET_Pipeline.groovy
[wlin@dhcp-136-3 test_git] # 合成为第一个commit并查看log进行确认
[wlin@dhcp-136-3 test_git]$ git rebase -i e6968dc2d1c8
[detached HEAD 5ef562e] add new file
Date: Thu Jul 8 12:27:38 2021 +0800
2 files changed, 3 insertions(+)
create mode 100644 cara_testing
create mode 100644 new_file
Successfully rebased and updated refs/heads/cara_testing.
[wlin@dhcp-136-3 test_git]$ git log
commit 5ef562e161b69dc2127f24acaf15eb31916efa6a (HEAD -> cara_testing)
Author: Cara Wang <wlin@redhat.com>
Date: Thu Jul 8 12:27:38 2021 +0800
add new file
commit e6968dc2d1c86b2b60d75feddcf8e7ea310b10d3 (origin/master, origin/HEAD, master)
Author: Chenglong Wang <chenwang@redhat.com>
Date: Wed Dec 23 04:37:54 2020 +0000
Update ET_Pipeline.groovy
[wlin@dhcp-136-3 test_git] # 提交更新
[wlin@dhcp-136-3 test_git]$ git push -f
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 406 bytes | 406.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: View merge request for cara_testing:
remote: https://gitlab.cee.redhat.com/chenwang/test_git/-/merge_requests/5
remote:
To gitlab.cee.redhat.com:wlin/test_git.git
+ 5a36f98...5ef562e cara_testing -> cara_testing (forced update)

本文介绍了在GitLab上与团队协作时如何进行有效的版本控制。内容包括fork上游仓库并保持同步,提交和更新Merge Request(MR)的步骤,确保在MR被merge前能及时与官方代码保持一致。

3674

被折叠的 条评论
为什么被折叠?



