Skip to content

Commit aa13c2c

Browse files
authored
Update README.md
1 parent 9b27446 commit aa13c2c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ git-tips
2929
- [去掉某个commit](#去掉某个commit)
3030
- [把 A 分支的某一个 commit,放到 B 分支上](#把-a-分支的某一个-commit放到-b-分支上)
3131
- [获取最近一次提交的 commit id](#获取最近一次提交的-commit-id)
32+
- [两个 git 仓库合并](#两个-git 仓库合并)
3233
- [合并多个commit](#合并多个commit)
3334
- [修改远程Commit记录](#修改远程commit记录)
3435
- [利用commit关闭一个issue](#利用commit关闭一个issue)
@@ -452,6 +453,36 @@ git rev-parse HEAD # e10721cb8859b2cd340d31a52ef4bf4b9629ddda
452453
git rev-parse --short HEAD # e10721c
453454
```
454455

456+
### 两个 git 仓库合并
457+
458+
现在有两个仓库 [kktjs/kkt](https://github.com/kktjs/kkt.git)[kktjs/kkt-next](https://github.com/kktjs/kkt-next.git) 我们需要将 `kkt-next` 仓库合并到 `kkt` 并保留 `kkt-next` 的所有提交内容。
459+
460+
```bash
461+
# 1. 克隆主仓库代码
462+
git clone git@github.com:kktjs/kkt.git
463+
# 2. 将 kkt-next 作为远程仓库,添加到 kkt 中,设置别名为 other
464+
git remote add other git@github.com:kktjs/kkt-next.git
465+
# 3. 从 kkt-next 仓库中拉取数据到本仓库
466+
git fetch other
467+
# 4. 将 kkt-next 仓库拉取的 master 分支作为新分支 checkout 到本地,新分支名设定为 kkt-next
468+
git checkout -b kkt-next other/master
469+
# 5. 切换回 kkt 的 master 分支
470+
git checkout master
471+
# 6. 将 kkt-next 合并入 kkt 的 master 分支
472+
git merge kkt-next
473+
# 如果第 6 步报错 `fatal: refusing to merge unrelated histories`
474+
# 请执行下面命令 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
475+
git merge kkt-next --allow-unrelated-histories
476+
```
477+
478+
在合并时有可能两个分支对同一个文件都做了修改,这时需要解决冲突,对文本文件来说很简单,根据需要对冲突的位置进行处理就可以。对于二进制文件,需要用到如下命令:
479+
480+
```bash
481+
git checkout --theirs YOUR_BINARY_FILES # 保留需要合并进来的分支的修改
482+
git checkout --ours YOUR_BINARY_FILES # 保留自己的修改
483+
git add YOUR_BINARY_FILES
484+
```
485+
455486
### 合并多个commit
456487

457488
```bash

0 commit comments

Comments
 (0)