240 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			240 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| # 1. git使用手册
 | ||
| 
 | ||
| ## 1.1. 概述
 | ||
| 
 | ||
|   git是分布式版本控制系统,在多人开发中,git可以很好的管理代码的版本。
 | ||
| 
 | ||
| ## 1.2. 源码托管服务器
 | ||
| 
 | ||
|   github和gitlab还有gitee(国产)都是开源的代码托管服务器,可以用来管理源码。
 | ||
| 
 | ||
| ## 1.3. git安装
 | ||
| 
 | ||
| ## 1.4. git分支管理
 | ||
| 
 | ||
| ### 1.4.1. git创建本地分支
 | ||
| 
 | ||
| * 基于远端分支创建一个本地分支,同时新建一个对应的远端分支:
 | ||
| 
 | ||
|   当主干发生较大变化,例如:原厂更新sdk时,需要新建分支,划分界限。
 | ||
| 
 | ||
| ```code
 | ||
| $ git branch -a
 | ||
| ----------------
 | ||
| * master
 | ||
|   remotes/origin/HEAD -> origin/master
 | ||
|   remotes/origin/app_test
 | ||
|   remotes/origin/master
 | ||
| $ git checkout -b master-sdk-202405 origin/master
 | ||
| -------------------------------------------------
 | ||
| M       ipc-sdk
 | ||
| Branch 'master-sdk-202405' set up to track remote branch 'master' from 'origin'.
 | ||
| Switched to a new branch 'master-sdk-202405'
 | ||
| $ git branch -a
 | ||
| ----------------
 | ||
|   master
 | ||
| * master-sdk-202405
 | ||
|   remotes/origin/HEAD -> origin/master
 | ||
|   remotes/origin/app_test
 | ||
|   remotes/origin/master
 | ||
| $ git push origin master-sdk-202405:sdk-202405
 | ||
| ----------------------------------------------
 | ||
| Enumerating objects: 3, done.
 | ||
| Counting objects: 100% (3/3), done.
 | ||
| Delta compression using up to 8 threads
 | ||
| Compressing objects: 100% (2/2), done.
 | ||
| Writing objects: 100% (2/2), 250 bytes | 250.00 KiB/s, done.
 | ||
| Total 2 (delta 1), reused 0 (delta 0)
 | ||
| remote: Powered by GITEE.COM [GNK-6.4]
 | ||
| remote: Create a pull request for 'sdk-202405' on Gitee by visiting:
 | ||
| remote:     https://gitee.com/shenzhen-jiuyilian/ipc-rk1106/pull/new/shenzhen-jiuyilian:sdk-202405...shenzhen-jiuyilian:master
 | ||
| To gitee.com:shenzhen-jiuyilian/ipc-rk1106.git
 | ||
|  * [new branch]      master-sdk-202405 -> sdk-202405
 | ||
| $ git branch -a
 | ||
| ---------------
 | ||
|   master
 | ||
| * master-sdk-202405
 | ||
|   remotes/origin/HEAD -> origin/master
 | ||
|   remotes/origin/app_test
 | ||
|   remotes/origin/master
 | ||
|   remotes/origin/sdk-202405
 | ||
| ```
 | ||
| 
 | ||
| ### 1.4.2. git获取远端分支
 | ||
| 
 | ||
|   当想知道远端是否新建了分支,可使用git fetch命令获取远端分支。
 | ||
| 
 | ||
| **git fetch示例:**
 | ||
| 
 | ||
| ```code
 | ||
| $ git fetch
 | ||
| ------------
 | ||
| remote: Enumerating objects: 21, done.
 | ||
| remote: Counting objects: 100% (21/21), done.
 | ||
| remote: Compressing objects: 100% (11/11), done.
 | ||
| remote: Total 14 (delta 8), reused 0 (delta 0), pack-reused 0
 | ||
| Unpacking objects: 100% (14/14), 2.14 KiB | 156.00 KiB/s, done.
 | ||
| From gitee.com:shenzhen-jiuyilian/ipc-rk1106
 | ||
|    bf71a01..2b9b803  master     -> origin/master
 | ||
|  * [new branch]      sdk-202402 -> origin/sdk-202402  // 此处发现远端新建了分支
 | ||
| Fetching submodule ipc-sdk
 | ||
| From gitee.com:shenzhen-jiuyilian/ipc
 | ||
|    7c261bd..eec9fb4  master-develop -> origin/master-develop
 | ||
| ```
 | ||
| 
 | ||
| **多个远端仓库git fetch示例:**
 | ||
| 
 | ||
| ```code
 | ||
| $ git remote -v
 | ||
| ----------------
 | ||
| dgiot   git@gitee.com:shenzhen-jiuyilian/fastbootserver.git (fetch)
 | ||
| dgiot   git@gitee.com:shenzhen-jiuyilian/fastbootserver.git (push)
 | ||
| rk      https://gerrit.rock-chips.com:8443/linux/linux/ipc/app/fastboot_server (fetch)
 | ||
| rk      https://gerrit.rock-chips.com:8443/linux/linux/ipc/app/fastboot_server (push)
 | ||
| $ git fetch dgiot  // git fetch + 远端仓库名称
 | ||
| ---------------------------------------------
 | ||
| remote: Enumerating objects: 9, done.
 | ||
| remote: Counting objects: 100% (9/9), done.
 | ||
| remote: Compressing objects: 100% (7/7), done.
 | ||
| remote: Total 9 (delta 3), reused 5 (delta 2), pack-reused 0
 | ||
| Unpacking objects: 100% (9/9), 8.74 KiB | 746.00 KiB/s, done.
 | ||
| From gitee.com:shenzhen-jiuyilian/fastbootserver
 | ||
|  * [new branch]      sdk-202405 -> dgiot/sdk-202405
 | ||
| ```
 | ||
| 
 | ||
| ### 1.4.3. git新增远端地址
 | ||
| 
 | ||
|   在一个git仓库中,可以同时管理多个远端地址,例如:在原厂的git仓库中,可以在仓库添加一个私人的git仓库,这样后续把修改提交到自己的仓库进行代码管理。
 | ||
| 
 | ||
| **git remote add示例:**
 | ||
| 
 | ||
| 命令格式:
 | ||
| 
 | ||
| ```code
 | ||
| git remote add <remote-name> <remote-url>
 | ||
| ```
 | ||
| 
 | ||
| 示例:
 | ||
| 
 | ||
| ```code
 | ||
| $ git remote add dgiot git@gitee.com:shenzhen-jiuyilian/fastbootserver.git
 | ||
| ---------------------------------------------------------------------------
 | ||
| $ git remote -v
 | ||
| ----------------
 | ||
| dgiot   git@gitee.com:shenzhen-jiuyilian/fastbootserver.git (fetch)
 | ||
| dgiot   git@gitee.com:shenzhen-jiuyilian/fastbootserver.git (push)
 | ||
| rk      https://gerrit.rock-chips.com:8443/linux/linux/ipc/app/fastboot_server (fetch)
 | ||
| rk      https://gerrit.rock-chips.com:8443/linux/linux/ipc/app/fastboot_server (push)
 | ||
| $ git fetch dgiot
 | ||
| ------------------
 | ||
| remote: Enumerating objects: 107, done.
 | ||
| remote: Counting objects: 100% (104/104), done.
 | ||
| remote: Compressing objects: 100% (45/45), done.
 | ||
| remote: Total 99 (delta 47), reused 89 (delta 42), pack-reused 0
 | ||
| Unpacking objects: 100% (99/99), 29.55 KiB | 315.00 KiB/s, done.
 | ||
| From gitee.com:shenzhen-jiuyilian/fastbootserver
 | ||
|  * [new branch]      master     -> dgiot/master
 | ||
|  * [new branch]      sdk-202405 -> dgiot/sdk-202405
 | ||
| $ git branch -a
 | ||
| ----------------
 | ||
| * (HEAD detached at bf91101)
 | ||
|   remotes/dgiot/master
 | ||
|   remotes/dgiot/sdk-202405
 | ||
|   remotes/m/master
 | ||
|   remotes/rk/master
 | ||
| $ git checkout -b sdk-202405  m/master
 | ||
| --------------------------------------
 | ||
| Switched to a new branch 'sdk-202405'
 | ||
| $ git branch -a
 | ||
| ----------------
 | ||
| * sdk-202405
 | ||
|   remotes/dgiot/master
 | ||
|   remotes/dgiot/sdk-202405
 | ||
|   remotes/m/master
 | ||
|   remotes/rk/master
 | ||
| $ git pull dgiot sdk-202405
 | ||
| ---------------------------
 | ||
| From gitee.com:shenzhen-jiuyilian/fastbootserver
 | ||
|  * branch            sdk-202405 -> FETCH_HEAD
 | ||
| Updating bf91101..dc76264
 | ||
| Fast-forward
 | ||
|  .clang-format  | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 | ||
|  .gitmodules    |   3 +++
 | ||
|  README.md      |  30 ++++++++++++++++++++++++++++++
 | ||
|  rv1106_ipc_sdk |   1 +
 | ||
|  4 files changed, 170 insertions(+)
 | ||
|  create mode 100644 .clang-format
 | ||
|  create mode 100644 .gitmodules
 | ||
|  create mode 100755 README.md
 | ||
|  create mode 160000 rv1106_ipc_sdk
 | ||
| $ git submodule update --init
 | ||
| -----------------------------
 | ||
| Submodule 'rv1106_ipc_sdk' (git@gitee.com:shenzhen-jiuyilian/ipc-rk1106.git) registered for path 'rv1106_ipc_sdk'
 | ||
| Cloning into '/home/xiaojiazhu/project/rkipc/ipc_20240517/project/app/component/fastboot_server/rv1106_ipc_sdk'...
 | ||
| Submodule path 'rv1106_ipc_sdk': checked out 'ff8da760b201d365300aed78190de8564f0d2171'
 | ||
| ```
 | ||
| 
 | ||
| ### 1.4.4. git删除远端地址
 | ||
| 
 | ||
| ```code
 | ||
| # git remote remove <remote-name>
 | ||
| $ git remote -v
 | ||
| ---------------
 | ||
| Germany payton@git.affgt.com:Germany/hunting.git (fetch)
 | ||
| Germany payton@git.affgt.com:Germany/hunting.git (push)
 | ||
| origin  git@gitee.com:shenzhen-jiuyilian/ipc.git (fetch)
 | ||
| origin  git@gitee.com:shenzhen-jiuyilian/ipc.git (push)
 | ||
| $ git remote remove Germany
 | ||
| ---------------------------
 | ||
| $ git remote -v
 | ||
| ----------------
 | ||
| origin  git@gitee.com:shenzhen-jiuyilian/ipc.git (fetch)
 | ||
| origin  git@gitee.com:shenzhen-jiuyilian/ipc.git (push)
 | ||
| ```
 | ||
| 
 | ||
| ### 1.4.5. git删除一个远端分支
 | ||
| 
 | ||
| ```code
 | ||
| # git push <remote-name> --delete <branch-name>
 | ||
| $ git branch -r
 | ||
| ---------------
 | ||
|   origin/Branch_QT
 | ||
|   origin/HEAD -> origin/master
 | ||
|   origin/master
 | ||
|   origin/master-develop
 | ||
|   origin/without-testtools
 | ||
| $ git push origin --delete Branch_QT
 | ||
| -------------------------------------
 | ||
| remote: Powered by GITEE.COM [GNK-6.4]
 | ||
| To gitee.com:shenzhen-jiuyilian/ipc.git
 | ||
|  - [deleted]         Branch_QT
 | ||
| ```
 | ||
| 
 | ||
| ## 1.5. 多仓库管理
 | ||
| 
 | ||
| ### 1.5.1. 合并两个无关联记录的仓库
 | ||
| 
 | ||
|   在一个仓库上合并另外一个无关联记录的仓库。
 | ||
| 
 | ||
| ```code
 | ||
| # 假设A仓库的代码合入到B仓库
 | ||
| # 在需要合并的仓库A下面执行
 | ||
| $ rm -rf .git              # 如果不需要A仓库的修改记录,需要重新创建一个新的本地分支和记录;
 | ||
| $ git init                 # 初始化一个空的仓库,并创建.git目录;
 | ||
| $ git add .                # 添加A仓库的代码;
 | ||
| $ git commit -m "new log"  # 保存代码,创建新的log;此时创建了一个本地的master分支;
 | ||
| $ git remote add B <B仓库的地址> # 添加B仓库的地址;
 | ||
| $ git fetch B              # 拉取B仓库的信息;
 | ||
| $ git checkout -b open B/master                # 创建一个B仓库的本地分支,并切换到该分支;
 | ||
| $ git merge master --allow-unrelated-histories # 合并A仓库的代码,并允许两个分支没有关联记录;
 | ||
| $ git checkout --theirs .                         # 合并如果有冲突,选择A仓库的代码;
 | ||
| $  git restore --source=master output_files/libs/ # 按需,恢复A仓库的某些代码;
 | ||
| $ git add .                 # 添加需要合并的代码;
 | ||
| $ git commit -m "merge log" # 合并后的代码,保存到本地仓库;
 | ||
| $ git push B open:master    # 推送到B仓库的master分支;
 | ||
| ```
 | ||
| 
 | ||
| ## 1.6. 存疑
 | ||
| 
 | ||
| * 不同的分支之间如何同步某个文件?
 | 
