TIPS之 对比github,gitee使用习惯不同点

对比github,gitee使用习惯不同点: git fetch/rebase/squash

Posted by 董江 on Thursday, July 27, 2023

习惯使用github的同学,一定会发现github非常实用的两个功能:

  • Sync fork
  • Squash and merge/Rebase and merge

Sync fork

fork 开源代码到个人分支后,由于主分支有其他人员提交,将 源仓库变更 同步到 fork仓库, 可以通过以下按钮🔘 一键式执行。

然而,在gitee目前无此功能。

可以通过git指令完成 Sync fork 能力

第一步: 通过git remote 是否设置 upstream

dongjiang@MacBook Pro:new $ git remote -v
origin	git@gitee.com:dongjiang1989/community.git (fetch)
origin	git@gitee.com:dongjiang1989/community.git (push)

只有fork分支地址,说明gitee在fork工程时候在仓库的.git文件夹中未自动添加upstream

第二步: 手动添加upstream

dongjiang@MacBook Pro:new $ git remote add upstream https://gitee.com/openeuler/community.git

dongjiang@MacBook Pro:new $ git remote -v                                                    
origin	git@gitee.com:dongjiang1989/community.git (fetch)
origin	git@gitee.com:dongjiang1989/community.git (push)
upstream	https://gitee.com/openeuler/community.git (fetch)
upstream	https://gitee.com/openeuler/community.git (push)

dongjiang@MacBook Pro:new $ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = git@gitee.com:dongjiang1989/community.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[branch "codec"]
	remote = origin
	merge = refs/heads/codec
[remote "upstream"]
	url = https://gitee.com/openeuler/community.git
	fetch = +refs/heads/*:refs/remotes/upstream/*

目测已经添加成功。 需要注意的是 添加 upstream url 需要是 https协议地址

第三步: 同步 upstream 元数据

dongjiang@MacBook Pro:new $ git fetch upstream
From https://gitee.com/openeuler/community
 * [new branch]          feature/test_branch      -> upstream/feature/test_branch
 * [new branch]          master                   -> upstream/master
 * [new branch]          revert-merge-1991-master -> upstream/revert-merge-1991-master
 * [new branch]          revert-merge-2642-master -> upstream/revert-merge-2642-master
 * [new branch]          revert-merge-2733-master -> upstream/revert-merge-2733-master
 * [new branch]          revert-merge-2745-master -> upstream/revert-merge-2745-master

第四步: 同步分支数据到 本地local仓库分支, 并提交远端分支

dongjiang@MacBook Pro:new $ git checkout master
...

dongjiang@MacBook Pro:new $ git push -f //代码将fork local分支同步到 fork remote分支
...

Squash and merge/Rebase and merge

给开源项目提交 PR 时候,由于比较长时间没有 rebase master 分支,并且在此PR提交的 commit 又很多,有些 commit 又是实验性质的. 最终merge到源仓库可以通过以下2模式,可以通过以下按钮🔘 一键式执行。

gitee目前也无此功能,可通过git命令行,进行merge

第一步: 查看git log,需要对多少次push进行 commitid 合并

dongjiang@MacBook Pro:Computing $ git log   
commit bbbdba1f9b6ce13646f328a1ff869457a6fa4589 (HEAD -> codec, origin/codec)
Author: dongjiang1989 <dongjiang1989@126.com>
Date:   Thu Jul 27 10:28:52 2023 +0800

    update sig-info.yaml
    
    Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

commit 07074c24c0222c5d74275cc33e59217cf9cfaf21
Author: dongjiang1989 <dongjiang1989@126.com>
Date:   Thu Jul 27 09:54:27 2023 +0800

    Apply to add kspack-xx repo
    
    Signed-off-by: dongjiang1989 <dongjiang1989@126.com>

commit 4b65fd1e9fb5b3b1d816112e410d126d402e0f46
Merge: 06a8bb391 1c48c13c2
Author: openeuler-ci-bot <george@openeuler.sh>
Date:   Thu Jul 27 01:21:31 2023 +0000

......

第二步:通过git rebase -i <commit个数> 进行压缩commitid

dongjiang@MacBook Pro:Computing $ git rebase -i HEAD~2
[detached HEAD fb74d5ca9] Apply to add kspack-xx repo
 Date: Thu Jul 27 09:54:27 2023 +0800
 6 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 sig/Computing/openeuler/k/kspack-c.yaml
 create mode 100644 sig/Computing/openeuler/k/kspack-go.yaml
 create mode 100644 sig/Computing/openeuler/k/kspack-java.yaml
 create mode 100644 sig/Computing/openeuler/k/kspack-rust.yaml
Successfully rebased and updated refs/heads/codec.

第三步:将 pick 改成 squash

pick 07074c24c Apply to add kspack-xx repo
pick bbbdba1f9 update sig-info.yaml           // 将第二个 pick 改成 squash, 将2个commit添加到一个commit

# Rebase 07074c24c..bbbdba1f9 onto bbbdba1f9 (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
.....

第四步:保存推出后,未新合并的 commit 添加 message

// 在首行添加新的commit message

Conbine 2 commit message into 1   //此行新添加的的 commit message说明

# This is a combination of 2 commits.
# This is the 1st commit message:

Apply to add kspack-xx repo

# This is the commit message #2:

update sig-info.yaml 

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

第五步:将本地代码强制推送到远端分支

dongjiang@MacBook Pro:Computing $ git push -f
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (12/12), 2.81 KiB | 2.81 MiB/s, done.
Total 12 (delta 4), reused 5 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:dongjiang1989/community.git
 + bbbdba1f9...fb74d5ca9 codec -> codec (forced update)

需要添加-f,进行强制推动

「如果这篇文章对你有用,请随意打赏」

Kubeservice博客

如果这篇文章对你有用,请随意打赏

使用微信扫描二维码完成支付