使用rebase和merge进行多人协作开发
使用rebase和merge进行多人协作开发
·
使用rebase和merge进行多人协作开发
1、master分支准备
# master分支
$ cd tm
$ git init
touch a.txt
git add a.txt
git commit -m "add a.txt"
touch b.txt
git add b.txt
git commit -m "add b.txt"
touch c.txt
git add c.txt
git commit -m "add c.txt"
touch d.txt
git add d.txt
git commit -m "add d.txt"
touch e.txt
git add e.txt
git commit -m "add e.txt"
touch f.txt
git add f.txt
git commit -m "add f.txt"
$ git remote add origin https://gitee.com/zsx242030/tm.git
$ git push -u origin "master"
Counting objects: 13, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (13/13), 1.04 KiB | 1.04 MiB/s, done.
Total 13 (delta 4), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/zsx242030/tm.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
$ git log --pretty=format:"%h: %cd %s" --graph
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
2、A、B、C三个用户拉取代码并切换分支
# A用户branch_a分支
$ git clone https://gitee.com/zsx242030/tm.git
$ cd tm
$ git log --pretty=format:"%h: %cd %s" --graph
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
$ git checkout -b branch_a
Switched to a new branch 'branch_a'
$ git branch -a
* branch_a
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
# B用户branch_b分支
$ git clone https://gitee.com/zsx242030/tm.git
$ cd um
$ git log --pretty=format:"%h: %cd %s" --graph
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
$ git checkout -b branch_b
Switched to a new branch 'branch_b'
$ git branch -a
* branch_b
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
# C用户branch_c分支
$ git clone https://gitee.com/zsx242030/tm.git
$ cd tm
$ git log --pretty=format:"%h: %cd %s" --graph
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
$ git checkout -b branch_c
Switched to a new branch 'branch_c'
$ git branch -a
* branch_c
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
3、A用户添加文件和修改文件
# branch_a分支操作
$ echo branch_a > a.txt
$ echo branch_a_new > new.txt
$ git add .
$ git commit -m "branch_a | update a.txt | add new.txt"
[branch_a 8b9e9ee] branch_a | update a.txt | add new.txt
2 files changed, 2 insertions(+)
create mode 100644 new.txt
# 从分支上拉取,拉取的是该分支都没有提交过的
$ git fetch origin
$ git rebase origin/master
Current branch branch_a is up to date.
$ git push origin branch_a:branch_a
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 340 bytes | 340.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_a' on Gitee by visiting:
remote: https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_a...zsx242030:master
To https://gitee.com/zsx242030/tm.git
* [new branch] branch_a -> branch_a
$ git log --pretty=format:"%h: %cd %s" --graph
* 8b9e9ee: Sat May 27 08:30:50 2023 +0800 branch_a | update a.txt | add new.txt
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
4、A用户的分支合并到master分支
# 有的公司使用的是gitlab只需要在上面进行mr操作即可
# 有的需要在本地合并然后推送到主分支
# master分支操作
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
# 合并
$ git merge origin/branch_a
$ git merge branch_a
Updating 3915ec1..8b9e9ee
Fast-forward
a.txt | 1 +
new.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 new.txt
# 推送
$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/zsx242030/tm.git
3915ec1..8b9e9ee master -> master
$ git log --pretty=format:"%h: %cd %s" --graph
* 8b9e9ee: Sat May 27 08:30:50 2023 +0800 branch_a | update a.txt | add new.txt
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
5、B用户添加文件和修改文件
# branch_b分支操作
$ echo branch_b > a.txt
$ echo branch_b_new > new.txt
$ git status
On branch branch_b
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
new.txt
no changes added to commit (use "git add" and/or "git commit -a")
下面将进行拉取远程代码,拉取远程代码之前需要保证代码是 commit
的或者是 stash
的。
5.1 stash
$ git stash
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
Saved working directory and index state WIP on branch_b: 3915ec1 add f.txt
$ git fetch origin
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://gitee.com/zsx242030/tm
* [new branch] branch_a -> origin/branch_a
3915ec1..8b9e9ee master -> origin/master
# 由于这里也添加了new.txt,所以会产生冲突,必须进行处理
$ git rebase origin/master
First, rewinding head to replay your work on top of it...
error: The following untracked working tree files would be overwritten by checkout:
new.txt
Please move or remove them before you switch branches.
Aborting
could not detach HEAD
$ mv new.txt new.bak
# 重新拉取
$ git rebase origin/master
First, rewinding head to replay your work on top of it...
Fast-forwarded branch_b to origin/master.
# 查看拉取的情况
# 发现内容都是用户A提交的
$ cat a.txt
branch_a
$ cat new.txt
branch_a_new
$ git log --pretty=format:"%h: %cd %s" --graph
* 8b9e9ee: Sat May 27 08:30:50 2023 +0800 branch_a | update a.txt | add new.txt
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
# 下面弹出工作
# a.txt也添加了内容有冲突需要解决
$ git stash pop
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
# a.txt的内容
$ cat a.txt
<<<<<<< Updated upstream
branch_a
=======
branch_b
>>>>>>> Stashed changes
# 解决之后的内容
$ cat a.txt
branch_a
branch_b
# 我们将new.bak和new.txt的文件进行合并,然后删除new.bak
$ cat new.txt
branch_a_new
branch_b_new
$ git status
On branch branch_b
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: new.txt
no changes added to commit (use "git add" and/or "git commit -a")
# 添加文件提交
$ git add .
$ git commit -m "branch_b | update a.txt | update new.txt"
[branch_b a79a1b2] branch_b | update a.txt | update new.txt
2 files changed, 2 insertions(+)
$ git push origin branch_b:branch_b
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 342 bytes | 342.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_b' on Gitee by visiting:
remote: https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_b...zsx242030:master
To https://gitee.com/zsx242030/tm.git
* [new branch] branch_b -> branch_b
# 删除该远程分支演示commit
5.2 commit
$ git add .
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in new.txt.
The file will have its original line endings in your working directory.
$ git commit -m "branch_b | update a.txt | add new.txt"
[branch_b 1103c61] branch_b | update a.txt | add new.txt
2 files changed, 2 insertions(+)
create mode 100644 new.txt
# 从分支上拉取,拉取的是该分支都没有提交过的
$ git fetch origin
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://gitee.com/zsx242030/tm
* [new branch] branch_a -> origin/branch_a
3915ec1..8b9e9ee master -> origin/master
# 这里有冲关系需要解决冲突
$ git rebase origin/master
First, rewinding head to replay your work on top of it...
Applying: branch_b | update a.txt | add new.txt
error: Failed to merge in the changes.
Using index info to reconstruct a base tree...
M a.txt
Falling back to patching base and 3-way merge...
Auto-merging new.txt
CONFLICT (add/add): Merge conflict in new.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Patch failed at 0001 branch_b | update a.txt | add new.txt
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
# 解决冲突
$ cat a.txt
<<<<<<< HEAD
branch_a
=======
branch_b
>>>>>>> branch_b | update a.txt | add new.txt
$ cat new.txt
<<<<<<< HEAD
branch_a_new
=======
branch_b_new
>>>>>>> branch_b | update a.txt | add new.txt
# 解决
$ cat a.txt
branch_a
branch_b
$ cat new.txt
branch_a_new
branch_b_new
# 继续rebase
# 直接运行这个会报错
$ git rebase --continue
a.txt: needs merge
new.txt: needs merge
You must edit all merge conflicts and then
mark them as resolved using git add
# 需要先添加
$ git add a.txt new.txt
$ git rebase --continue
Applying: branch_b | update a.txt | add new.txt
# push
$ git push origin branch_b:branch_b
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 348 bytes | 348.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_b' on Gitee by visiting:
remote: https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_b...zsx242030:master
To https://gitee.com/zsx242030/tm.git
* [new branch] branch_b -> branch_b
$ git log --pretty=format:"%h: %cd %s" --graph
* 2e76154: Sat May 27 08:51:37 2023 +0800 branch_b | update a.txt | add new.txt
* 8b9e9ee: Sat May 27 08:30:50 2023 +0800 branch_a | update a.txt | add new.txt
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
6、B用户的分支合并到master分支
# master分支操作
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
# 合并
$ git merge origin/branch_b
$ git merge branch_b
Updating 3915ec1..2e76154
Fast-forward
a.txt | 2 ++
new.txt | 2 ++
2 files changed, 4 insertions(+)
create mode 100644 new.txt
# 推送
$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/zsx242030/tm.git
8b9e9ee..2e76154 master -> master
$ git log --pretty=format:"%h: %cd %s" --graph
* 2e76154: Sat May 27 08:51:37 2023 +0800 branch_b | update a.txt | add new.txt
* 8b9e9ee: Sat May 27 08:30:50 2023 +0800 branch_a | update a.txt | add new.txt
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
# 查看文件情况
$ cat a.txt
branch_a
branch_b
$ cat new.txt
branch_a_new
branch_b_new
7、C用户删除文件和修改文件
# branch_c分支操作
$ echo branch_c > a.txt
$ rm -f e.txt
$ git status
On branch branch_c
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a.txt
deleted: e.txt
no changes added to commit (use "git add" and/or "git commit -a")
7.1 stash
$ git stash
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
Saved working directory and index state WIP on branch_c: 3915ec1 add f.txt
$ git status
On branch branch_c
nothing to commit, working tree clean
$ git fetch origin
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
From https://gitee.com/zsx242030/tm
* [new branch] branch_a -> origin/branch_a
* [new branch] branch_b -> origin/branch_b
3915ec1..2e76154 master -> origin/master
$ git rebase origin/master
First, rewinding head to replay your work on top of it...
Fast-forwarded branch_c to origin/master.
$ git stash pop
Removing e.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
$ cat a.txt
<<<<<<< Updated upstream
branch_a
branch_b
=======
branch_c
>>>>>>> Stashed changes
# 解决冲突
$ cat a.txt
branch_a
branch_b
branch_c
$ git add .
$ git commit -m "branch_c | update a.txt | delete e.txt"
[branch_c 6eeb59a] branch_c | update a.txt | delete e.txt
2 files changed, 2 insertions(+), 1 deletion(-)
delete mode 100644 e.txt
$ git push origin branch_c:branch_c
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_c' on Gitee by visiting:
remote: https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_c...zsx242030:master
To https://gitee.com/zsx242030/tm.git
* [new branch] branch_c -> branch_c
# 删除branch_c分支,演示commit
7.2 commit
$ git add .
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
$ git commit -m "branch_c | update a.txt | delete e.txt"
[branch_c da20016] branch_c | update a.txt | delete e.txt
2 files changed, 1 insertion(+)
delete mode 100644 e.txt
$ git fetch origin
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
From https://gitee.com/zsx242030/tm
* [new branch] branch_a -> origin/branch_a
* [new branch] branch_b -> origin/branch_b
3915ec1..2e76154 master -> origin/master
$ git rebase origin/master
First, rewinding head to replay your work on top of it...
Applying: branch_c | update a.txt | delete e.txt
error: Failed to merge in the changes.
Using index info to reconstruct a base tree...
M a.txt
Falling back to patching base and 3-way merge...
Removing e.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Patch failed at 0001 branch_c | update a.txt | delete e.txt
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
# 解决冲突
$ cat a.txt
<<<<<<< HEAD
branch_a
branch_b
=======
branch_c
>>>>>>> branch_c | update a.txt | delete e.txt
$ cat a.txt
branch_a
branch_b
branch_c
$ git add .
$ git rebase --continue
Applying: branch_c | update a.txt | delete e.txt
$ git push origin branch_c:branch_c
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_c' on Gitee by visiting:
remote: https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_c...zsx242030:master
To https://gitee.com/zsx242030/tm.git
* [new branch] branch_c -> branch_c
9、C用户的分支合并到master分支
# master分支操作
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
# 合并
$ git merge origin/branch_c
$ git merge branch_c
Updating 3915ec1..244cbdb
Fast-forward
a.txt | 3 +++
e.txt | 0
new.txt | 2 ++
3 files changed, 5 insertions(+)
delete mode 100644 e.txt
create mode 100644 new.txt
# 推送
$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/zsx242030/tm.git
2e76154..244cbdb master -> master
$ git log --pretty=format:"%h: %cd %s" --graph
* 244cbdb: Sat May 27 09:13:06 2023 +0800 branch_c | update a.txt | delete e.txt
* 2e76154: Sat May 27 08:51:37 2023 +0800 branch_b | update a.txt | add new.txt
* 8b9e9ee: Sat May 27 08:30:50 2023 +0800 branch_a | update a.txt | add new.txt
* 3915ec1: Sat May 27 08:22:14 2023 +0800 add f.txt
* b175a77: Sat May 27 08:22:13 2023 +0800 add e.txt
* 8d8fcd0: Sat May 27 08:22:12 2023 +0800 add d.txt
* 89d3c7c: Sat May 27 08:22:12 2023 +0800 add c.txt
* de08e10: Sat May 27 08:22:11 2023 +0800 add b.txt
* 5d5fb2b: Sat May 27 08:22:11 2023 +0800 add a.txt
更多推荐
已为社区贡献2条内容
所有评论(0)