OpenHarmony开发者论坛
标题:
Git常用的操作指导
[打印本页]
作者:
润开鸿_闻飞
时间:
2024-5-17 15:13
标题:
Git常用的操作指导
[md][itopen组织](
https://gitee.com/itopen
)
1、提供OpenHarmony优雅实用的小工具
2、手把手适配riscv + qemu + linux的三方库移植
3、未来计划riscv + qemu + ohos的三方库移植 + 小程序开发
4、一切拥抱开源,拥抱国产化
## 一、常用的Git命令
基础命令git add, git commit等不做过多分析
## 二、Git远程交互
#### 3.1 远程仓添加新的分支
```shell
git push -u origin <branch_name>
```
#### 3.2 删除远程仓的分支
```shell
git push -u origin --delete <remote_branch_name>
```
#### 3.3 创建tag并上传远程仓
```shell
# 单仓创建tag
git tag -a tag_name -m "tag description"
git push -u origin tag_name
# repo多仓创建tag
repo forall -c "git tag -a tag_name -m 'tag description'"
repo forall -c "git push -u origin tag_name"
# 根据tag创建分支
git checkout -b branch_name tag_name
# 删除本地tag
git tag -d tag_name
# 删除远程tag
git push -u origin :remote_tag_name
git push -u origin :refs/tags/remote_tag_name
git push -u origin --delete remote_tag_name
```
## 四、常用的Git场景指导
#### 4.1 删除远程仓失败
**【现象描述】**
将远程仓拉取到本地后执行命令 `git push -u origin --delete remote_branch_name`报错,如下所示:
```shell
$ git push -u origin --delete remote_branch_name
error: dst refspec remote_branch_name matches more than one
error: failed to push some refs to 'xxxxxxxxxxxxx'
```
**【原因分析】**
远程仓中存在tag的名称和分支的名称一样引起的,需要先删除同名的tag然后再删除分支
**【处理方法】**
- 查看tags
- 删除对应的tag
- 删除分支
```shell
git ls-remote --tags origin
git push -u origin :refs/tags/remote_branch_name
git push -u origin --delete remote_branch_name
```
#### 4.2 使用 `git lfs`上传大文件
**【现象描述】**
在github和gitee中我们有时候需要上传某个大文件时报错失败
**【原因分析】**
github或者gitee这些网站对上传的文件大小都做了限制,所以出现上传失败
**【处理方法】**
使用 `git lfs track`命令创建.gitattributes文件进行上传
```shell
git lfs track <file>
```
#### 4.3 `git merge`失败处理
**【现象描述】**
`git merge`出现 `unrelated histories`,如下所示
```shell
$ git merge OpenHarmony-3.2-Beta2
fatal: refusing to merge unrelated histories
```
**【原因分析】**
两个branch的commit不相关导致拒绝合并不相关的历史
**【处理方法】**
merge的时候加上参数 `--allow-unrelated-histories`,如下所示
```shell
git merge OpenHarmony-3.2-Beta2 --allow-unrelated-histories
```
#### 4.4 `git clone`下载报错 `CAfile: none CRLfile`处理方法
**【现象描述】**
`git clone`下载报错 `CAfile: none CRLfile`,如下所示
```shell
$ git clone
https://192.168.3.200/gitlab.com ... rnel_linux_5.10.git
Cloning into 'kernel_linux_5.10'...
fatal: unable to access 'https://192.168.3.200/gitlab.com/openharmony/thead_project/kernel_linux_5.10.git/': server certificate verification failed. CAfile: none CRLfile: none
```
**【原因分析】**
在使用镜像网站或者代理进行git clone时,出现ssl证书验证失败
**【处理方法】**
关闭 `git clone`时的ssl验证流程即可
```shell
git config --global http.sslverify false
```
#### 4.5 git合并多个commit方法
**【需求描述】**
当前的代码在开发过程中提交了多个commit,现在开发完成,针对该特性我只需要一个commit即可,需要将多个commit合并操作。
例如如下的 `git log`,我要将8aef957和520752b合并成一个
```shell
$ git log --oneline
8aef957 (HEAD -> riscv, OpenHarmony-3.2-Beta2) Merge branch 'OpenHarmony-3.2-Beta2' of
https://gitee.com/ohos4riscv/productdefine_common
into OpenHarmony-3.2-Beta2
520752b add ark modules
0431f88 (riscv/OpenHarmony-3.2-Beta2, m/OpenHarmony-3.2-Beta2, develop/OpenHarmony-3.2-Beta2) !5 productdefine_common仓适配camera编译 Merge pull request !5 from 飞翔在天/OpenHarmony-3.2-Beta2
d1a278a add camera service
605d635 !4 add netmanager_ext component of communication for netmanager Merge pull request !4 from 侯忠波/OpenHarmony-3.2-Beta2
335ab1d add netmanager_ext component of communication for netmanager
d3e40cd !3 productdefine_common仓增加driver外设的配置 Merge pull request !3 from 飞翔在天/OpenHarmony-3.2-Beta2
3cb46d9 (riscv/develop, develop/develop) resolv lcd lib
2d2eda0 !2 适配chipset_common_thead.json文件,添加组件编译解决。 Merge pull request !2 from 温藤野/OpenHarmony-3.2-Beta2
3e73483 chipset_common_thead.json update
9626e58 !1 适配thed_rich.json文件,添加组件编译解决。 Merge pull request !1 from 温藤野/OpenHarmony-3.2-Beta2
8c4c27a thead_rich.json update
1b2a724 add c9xx json
```
**【处理方法】**
使用 `git rebase`命令即可,我们找到520752b下一个的commit是0431f88,使用以下命令即可实现
```shell
git rebase -i 0431f88
```
处理后我们使用 `git log --oneline`查看一下结果
```shell
$ git log --oneline
61d52db (HEAD -> riscv) add ark modules
0431f88 (riscv/OpenHarmony-3.2-Beta2, m/OpenHarmony-3.2-Beta2, develop/OpenHarmony-3.2-Beta2) !5 productdefine_common仓适配camera编译 Merge pull request !5 from 飞翔在天/OpenHarmony-3.2-Beta2
d1a278a add camera service
605d635 !4 add netmanager_ext component of communication for netmanager Merge pull request !4 from 侯忠波/OpenHarmony-3.2-Beta2
335ab1d add netmanager_ext component of communication for netmanager
d3e40cd !3 productdefine_common仓增加driver外设的配置 Merge pull request !3 from 飞翔在天/OpenHarmony-3.2-Beta2
3cb46d9 (riscv/develop, develop/develop) resolv lcd lib
2d2eda0 !2 适配chipset_common_thead.json文件,添加组件编译解决。 Merge pull request !2 from 温藤野/OpenHarmony-3.2-Beta2
3e73483 chipset_common_thead.json update
9626e58 !1 适配thed_rich.json文件,添加组件编译解决。 Merge pull request !1 from 温藤野/OpenHarmony-3.2-Beta2
8c4c27a thead_rich.json update
```
#### 4.6 git clone出现以下错误的处理方法

```shell
$ git clone
https://10.20.14.10/gitlab.com/wzl/ability_ability_lite.git
Cloning into 'ability_ability_lite'...
fatal: unable to access 'https://10.20.14.10/gitlab.com/wzl/ability_ability_lite.git/': server certificate verification failed. CAfile: none CRLfile: none
```
**【解决方法】**
关闭http.sslverify即可
```shell
git config --global http.sslverify false
```
#### 4.6 git push出现以下错误的处理方法

```shell
wen_fei@rh-Z790-UD:~/kernel_linux_5.10
$ git push -u thead OpenHarmony-v4.0-Release:OpenHarmony-v4.0-Release
Enumerating objects: 7892085, done.
Counting objects: 100% (7892085/7892085), done.
Delta compression using up to 32 threads
Compressing objects: 100% (1327299/1327299), done.
fatal: protocol error: bad line length 819275 GiB | 399.45 MiB/s
error: failed to push some refs to 'https://10.20.72.31:8088/gitlab.com/debug/rk3568_debug/kernel_linux_5.10.git'
```
**【解决方法】**
增加http.postBuffer的大小到7242880000即可
```shell
wen_fei@rh-Z790-UD:~/kernel_linux_5.10
$ git config --global http.postBuffer 7242880000
wen_fei@rh-Z790-UD:~/kernel_linux_5.10
$ git push -u thead OpenHarmony-v4.0-Release:OpenHarmony-v4.0-Release
Enumerating objects: 7892085, done.
Counting objects: 100% (7892085/7892085), done.
Delta compression using up to 32 threads
Compressing objects: 100% (1327299/1327299), done.
Writing objects: 100% (7892085/7892085), 1.81 GiB | 354.46 MiB/s, done.
Total 7892085 (delta 6516177), reused 7890796 (delta 6515355), pack-reused 0
remote: warning: object b2758f55614503d51c777c25faeff549faf6c26c: badFilemode: contains bad file modes
remote: warning: object 313d78fa8a105153164c39c96d494c8ccabd8c7f: badFilemode: contains bad file modes
remote: warning: object acafbc6b9b8ce5a90e40c673dd03abd898876929: badFilemode: contains bad file modes
remote: warning: object 8e9223ef4deb8137fd5a7722da13d753745c06aa: badFilemode: contains bad file modes
remote: warning: object 8264a1b0eeebd9cda625822a1ac6df22d631e8dc: badFilemode: contains bad file modes
remote: warning: object 956b25bd6ec3f1254affb2a47e059497e9c9d030: badFilemode: contains bad file modes
remote: warning: object b2a05c3c518908735ce88038c0043065ef6df11e: badFilemode: contains bad file modes
remote: warning: object ac816417fd510b93e6c95016a42400ef68a810ad: badFilemode: contains bad file modes
remote: warning: object 602207596acf39400a6bb96fdee83b9bb8b9e0b1: badFilemode: contains bad file modes
remote: warning: object 349794521329ca4029bdc25af77ff7a7e8aa9f83: badFilemode: contains bad file modes
remote: warning: object 8ff31038cc910c71b00524b5f38bd0ab24c55217: badFilemode: contains bad file modes
remote: warning: object 2b44639ced114a28737daa6cc4812f20e26ed96e: badFilemode: contains bad file modes
remote: warning: object d924c0ed349b7f92c528df97708cbe85d4b18f48: badFilemode: contains bad file modes
remote: Resolving deltas: 100% (6516177/6516177), done.
remote: Checking connectivity: 7892085, done.
To
https://10.20.72.31:8088/gitlab. ... rnel_linux_5.10.git
* [new branch] OpenHarmony-v4.0-Release -> OpenHarmony-v4.0-Release
branch 'OpenHarmony-v4.0-Release' set up to track 'thead/OpenHarmony-v4.0-Release'.
```
## 五、git submodule使用
### 5.1 应用场景
```
面对比较复杂的项目,我们有可能会将代码根据功能拆解成不同的子模块。主项目对子模块有依赖关系,却又并不关心子模块的内部开发流程细节。这种情况下,通常不会把所有源码都放在同一个 Git 仓库中。一般通常有两种方式进行管理,一种是通过repo进行对所有的git仓进行管理,当前OpenHarmony便是采用该方法,而另一种则是下面介绍的采用git工具中的submodule 功能管理。
```
```
git 工具的submodule 功能主要是建立了当前项目与子模块之间的依赖关系:子模块路径、子模块的远程仓库、子模块的版本号。
```
### 5.2 常用操作
#### 5.2.1 操作流程

#### 5.2.2 git仓准备
- 分别在远程组织中创建主项目和子项目的git仓
这里是测试使用,便在gitee的组织中创建了三个git仓:main_project、sub_project1和sub_project2,同时将sub_project2仓创建一个develop分支
- 将main_project仓拉取到本地
```shell
cd ~
git clone
https://gitee.com/personal-summary/main_project.git
```
#### 5.2.3 添加submodule<a name="添加submodule"></a>
- 使用如下命令将sub_project1和sub_project2仓关联到main_project仓
- sub_project1仓选择master分支,在main_project中的sub_project1目录
- sub_project2仓选择develop分支,在main_project中的sub2目录
- 此时在主仓根目录下会生成一个.gitmodules文件,里面记录了对应的sub仓name、path、url和branch(默认master分支不显示)
- 完成上述后将主仓代码上库,此时主仓就关联了当前子仓对应分支的最新commit,远程仓上会有如下显示

- 如果子仓有修改需要主仓同步关联修改,请参考[5.2.5 更新submodule](#更新submodule)
**【NOTE】**:正常项目开发过程中尽可能保持子项目对应的分支一致,这样方便使用foreach统一操作
```shell
# 设置sub子工程
cd ~/main_project
git submodule add
https://gitee.com/personal-summary/sub_project1
git submodule add
https://gitee.com/personal-summary/sub_project1
sub2
git submodule set-branch -b develop sub2 # 这只sub2取develop分支
# 查看生成的.gitmodules文件
$ cat .gitmodules
[submodule "sub_project1"]
path = sub_project1
url =
https://gitee.com/personal-summary/sub_project1
[submodule "sub2"]
path = sub2
url =
https://gitee.com/personal-summary/sub_project2
branch = develop
# 操作完成后提交代码上库
git add .
git commit . -m"xxx"
git push -u origin master
```
#### 5.2.4 获取submodule
按照[5.2.3 添加submodule](#添加submodule)操作之后,重新拉取主仓之后,对应的sub_project1和sub2目录其实是空的,此时我们可以通过以下操作将sub仓代码下载下来
```shell
# 方法一:下载主仓的同时下载子仓代码,加--recurse-submodules参数,推荐使用
git clone
https://gitee.com/personal-summary/main_project.git
--recurse-submodules
# 方法二:在主仓中执行以下命令,推荐使用
git submodule update --init --recursive
# 方法三:更新某一个仓的代码
git submodule update --init module
# 方法四:分步骤执行
git submodule init
git submodule update
```
#### 5.2.5 更新submodule<a name="更新submodule"></a>
当子仓项目对应的远程分支有更新的时候,但是主控仓仍然关联的是之前子仓的commit,此时就需要将子仓的更新同步给主控仓,我们一般分为以下几个操作步骤
- 进入子仓,将子仓的对应分支代码更新到本地
- 进入主仓代码执行代码提交流程只有,在远程仓中发现对应的commit发生了改变

- 如果有多个仓同时有修改,可以使用 `git submodule foreach`统一操作,此时要求所有子项目的分支必须一致才行
```shell
# 更新子模块代码
cd sub2
git pull origin develop
# 更新所有子仓代码
git submodule foreach 'git pull origin branch'
# 更新提交主控代码
cd ../
git add .
git commit . -m"xxx"
git push -u origin master
```
#### 5.2.6 切换submodule分支
当发现我们的子仓需要切换分支的时候我们需要执行如下操作
- 通过set-branch切换分支
- 进入子仓并切换到对应的分支
- 将主控分支代码修改提交
```shell
# 切换sub2的分支
git submodule set-branch -b master sub2
# sub2仓切换到对应分支
cd sub2
git checkout master
# 更新提交主控代码
git add .
git commit . -m"xxx"
git push -u origin master
```
#### 5.2.7 删除submodule
当我们不需要某个子仓的时候,可以通过 `git rm`实现
```shell
# 删除指定的子仓
git rm sub_project1
# 更新提交主控代码
git add .
git commit . -m"xxx"
git push -u origin master
```
## 六、`git lfs`使用
### 6.1 设置存储到 `lfs`的文件
执行如下命令后在 `git`仓下会自动生成 `.gitattributes`文件记录要提交到 `lfs`的文件
```shell
$ git lfs track "*.xz"
Tracking "*.xz"
$ ls -a
. .. .git .gitattributes
$ cat .gitattributes
*.xz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
```
### 6.2 提交 `lfs`文件
```shell
git add .
git commit -sm "text"
git push -u origin master
```
### 6.3 `lfs`文件下载
```shell
git clone xxx
git lfs pull
```
## 七、`git log`命令
### 7.1 查看10条单行log
```shell
git log --oneline -n 10
```
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5