使用pull和merge进行多人协作开发

使用pull和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.03 KiB | 1.03 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
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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 tm

$ git log --pretty=format:"%h: %cd %s" --graph
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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

# 暂存,防止pull的时候本地修改的代码被覆盖
$ 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_a: 76e47a0 add f.txt

$ git status
On branch branch_a
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        new.txt

nothing added to commit but untracked files present (use "git add" to track)

# 拉取远程master分支的代码
$ git pull origin master
From https://gitee.com/zsx242030/tm
 * branch            master     -> FETCH_HEAD
Already up-to-date.

# 弹出修改的内容
$ git stash pop
On branch branch_a
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")
Dropped refs/stash@{0} (b83ec4c3f4ccc973719eaecd6b0aa02325b7173f)

$ git status
On branch branch_a
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")

$ git add .
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_a | update a.txt | add new.txt"
[branch_a 467223c] branch_a | update a.txt | add new.txt
 2 files changed, 2 insertions(+)
 create mode 100644 new.txt

$ 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
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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 76e47a0..467223c
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
   76e47a0..467223c  master -> master
   
$ git log --pretty=format:"%h: %cd %s" --graph
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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")
# 暂存
$ 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: 76e47a0 add f.txt

$ git status
On branch branch_b
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        new.txt

nothing added to commit but untracked files present (use "git add" to track)
# 拉取远程master分支的代码
$ git pull origin master
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
 * branch            master     -> FETCH_HEAD
   76e47a0..467223c  master     -> origin/master
error: The following untracked working tree files would be overwritten by merge:
        new.txt
Please move or remove them before you merge.
Aborting
Updating 76e47a0..467223c

# 这里是由于该分支新增了new.txt文件,而master分支上用户A也新增了new.txt文件
# 这里我们修改new.txt文件的名字
$ mv new.txt new.bak

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  new.bak

# 重新拉取代码
$ git pull origin master
From https://gitee.com/zsx242030/tm
 * branch            master     -> FETCH_HEAD
Updating 76e47a0..467223c
Fast-forward
 a.txt   | 1 +
 new.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 new.txt

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  new.bak  new.txt

$ cat a.txt
branch_a

$ cat new.txt
branch_a_new

$ cat new.bak
branch_b_new
# 弹出修改的内容
$ git stash pop
Auto-merging a.txt
CONFLICT (content): Merge conflict in 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 | add new.txt"
[branch_b bcb903c] branch_b | update a.txt | add 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), 345 bytes | 345.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
* bcb903c: Wed Jun 7 20:00:08 2023 +0800 branch_b | update a.txt | add new.txt
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 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 76e47a0..bcb903c
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
   467223c..bcb903c  master -> master

$ git log --pretty=format:"%h: %cd %s" --graph
* bcb903c: Wed Jun 7 20:00:08 2023 +0800 branch_b | update a.txt | add new.txt
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 2023 +0800 add a.txt

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")
# 暂存
$ 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: 76e47a0 add f.txt

$ git status
On branch branch_c
nothing to commit, working tree clean
# 拉取远程master分支的代码
$ git pull origin master
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
 * branch            master     -> FETCH_HEAD
   76e47a0..bcb903c  master     -> origin/master
Updating 76e47a0..bcb903c
Fast-forward
 a.txt   | 2 ++
 new.txt | 2 ++
 2 files changed, 4 insertions(+)
 create mode 100644 new.txt
# 弹出暂存
$ 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 status
On branch branch_c
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    e.txt

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   a.txt
        
$ git add .

$ git commit -m "branch_c | update a.txt | delete e.txt"
[branch_c 08db352] 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

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 76e47a0..08db352
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
   bcb903c..08db352  master -> master

$ git log --pretty=format:"%h: %cd %s" --graph
* 08db352: Wed Jun 7 20:03:36 2023 +0800 branch_c | update a.txt | delete e.txt
* bcb903c: Wed Jun 7 20:00:08 2023 +0800 branch_b | update a.txt | add new.txt
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 2023 +0800 add a.txt

10、使用commit代替stash解决pull时的代码覆盖

我们从 branch_a 分支上拉取 branch_d 分支:

$ git clone https://gitee.com/zsx242030/tm.git

$ cd tm/

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch_a
  remotes/origin/branch_b
  remotes/origin/branch_c
  remotes/origin/master

$ git checkout -b branch_d origin/branch_a
Switched to a new branch 'branch_d'
Branch branch_d set up to track remote branch branch_a from origin.

$ git log --oneline
467223c (HEAD -> branch_d, origin/branch_a) branch_a | update a.txt | add new.txt
76e47a0 add f.txt
e345ccf add e.txt
ecd639c add d.txt
b735dd8 add c.txt
cc0d7bc add b.txt
fb8e408 add a.txt

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  new.txt

修改 a.txt 和 e.txt 文件,删除 b.txt 文件:

echo branch_d > a.txt
echo branch_d > e.txt
rm b.txt

$ git status
On branch branch_d
Your branch is up-to-date with 'origin/branch_a'.

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:    b.txt
        modified:   e.txt

no changes added to commit (use "git add" and/or "git commit -a")
# 提交
$ git add .

$ git commit -m "branch_d | update a.txt | delete b.txt | update e.txt"
[branch_d 6ab24bd] branch_d | update a.txt | delete b.txt | update e.txt
 3 files changed, 2 insertions(+), 1 deletion(-)
 delete mode 100644 b.txt
# pull远程分支
$ git pull origin master
From https://gitee.com/zsx242030/tm
 * branch            master     -> FETCH_HEAD
CONFLICT (modify/delete): e.txt deleted in 08db352fadb00807509a4c397fbd2af5b51b9521 and modified in HEAD. Version HEAD of e.txt left in tree.
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
# 冲突
$ cat a.txt
<<<<<<< HEAD
branch_d
=======
branch_a
branch_b
branch_c
>>>>>>> 8cb57f667e7681be447d98051da7a3b5123f8c33

# 解决冲突
$ cat a.txt
branch_a
branch_b
branch_c
branch_d
$ git status
On branch branch_d
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

        modified:   new.txt

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)

        both modified:   a.txt
        deleted by them: e.txt
$ git add .
warning: LF will be replaced by CRLF in e.txt.
The file will have its original line endings in your working directory.

$ git status
On branch branch_d
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        modified:   a.txt
        modified:   new.txt

$ git commit -m "branch_d | update a.txt | delete b.txt | update e.txt"
[branch_d 44c6d88] branch_d | update a.txt | delete b.txt | update e.txt

$ git status
On branch branch_d
Your branch is ahead of 'origin/branch_a' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git log --oneline
44c6d88 (HEAD -> branch_d) branch_d | update a.txt | delete b.txt | update e.txt
6ab24bd branch_d | update a.txt | delete b.txt | update e.txt
08db352 (origin/master, origin/branch_c, origin/HEAD, master) branch_c | update a.txt | delete e.txt
bcb903c (origin/branch_b) branch_b | update a.txt | add new.txt
467223c (origin/branch_a) branch_a | update a.txt | add new.txt
76e47a0 add f.txt
e345ccf add e.txt
ecd639c add d.txt
b735dd8 add c.txt
cc0d7bc add b.txt
fb8e408 add a.txt

这里我们进行了两次提价,将两个提交合并为一个提交。

# 合并commit
# 会有冲突
$ git rebase -i 08db352
error: could not apply 6ab24bd... branch_d | update a.txt | delete b.txt | update e.txt

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".

Could not apply 6ab24bd... branch_d | update a.txt | delete b.txt | update e.txt
CONFLICT (modify/delete): e.txt deleted in HEAD and modified in 6ab24bd... branch_d | update a.txt | delete b.txt | update e.txt. Version 6ab24bd... branch_d | update a.txt | delete b.txt | update e.txt of e.txt left in tree.
Removing b.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt

# 冲突
$ cat a.txt
<<<<<<< HEAD
branch_a
branch_b
branch_c
=======
branch_d
>>>>>>> 75dc0b6... branch_d | update a.txt | update b.txt | update e.txt

# 解决冲突
$ cat a.txt
branch_a
branch_b
branch_c
branch_d

$ git add .

$ git rebase --continue
[detached HEAD 5aacb5f] branch_d | update a.txt | delete b.txt | update e.txt
 3 files changed, 3 insertions(+), 1 deletion(-)
 delete mode 100644 b.txt
 create mode 100644 e.txt
Successfully rebased and updated refs/heads/branch_d.

$ git status
On branch branch_d
Your branch is ahead of 'origin/branch_a' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

$ git log --oneline
5aacb5f (HEAD -> branch_d) branch_d | update a.txt | delete b.txt | update e.txt
08db352 (origin/master, origin/branch_c, origin/HEAD, master) branch_c | update a.txt | delete e.txt
bcb903c (origin/branch_b) branch_b | update a.txt | add new.txt
467223c (origin/branch_a) branch_a | update a.txt | add new.txt
76e47a0 add f.txt
e345ccf add e.txt
ecd639c add d.txt
b735dd8 add c.txt
cc0d7bc add b.txt
fb8e408 add a.txt
# 推送到远程
$ git push origin branch_d:branch_d
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 386 bytes | 386.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_d' on Gitee by visiting:
remote:     https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_d...zsx242030:master
To https://gitee.com/zsx242030/tm.git
 * [new branch]      branch_d -> branch_d
# 合并分支
# master分支操作
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

# 合并
$ git merge origin/branch_d
$ git merge branch_d
Updating 08db352..5aacb5f
Fast-forward
 a.txt | 3 ++-
 b.txt | 0
 e.txt | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
 delete mode 100644 b.txt
 create mode 100644 e.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
   08db352..5aacb5f  master -> master
   
$ git log --pretty=format:"%h: %cd %s" --graph
* 5aacb5f: Wed Jun 7 20:58:08 2023 +0800 branch_d | update a.txt | delete b.txt | update e.txt
* 08db352: Wed Jun 7 20:03:36 2023 +0800 branch_c | update a.txt | delete e.txt
* bcb903c: Wed Jun 7 20:00:08 2023 +0800 branch_b | update a.txt | add new.txt
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 2023 +0800 add a.txt

这里我们进行了两次提价,同时发生了两次冲突,需要解决两次冲突,我们使用下面的命令只需要进行一次冲突解

决。

$ git pull --rebase origin master

我们从 branch_a 分支上拉取 branch_e 分支:

$ git clone https://gitee.com/zsx242030/tm.git

$ cd tm/

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch_a
  remotes/origin/branch_b
  remotes/origin/branch_c
  remotes/origin/branch_d
  remotes/origin/master

$ git checkout -b branch_e origin/branch_a
Switched to a new branch 'branch_e'
Branch branch_e set up to track remote branch branch_a from origin.

$ git log --oneline
467223c (HEAD -> branch_e, origin/branch_a) branch_a | update a.txt | add new.txt
76e47a0 add f.txt
e345ccf add e.txt
ecd639c add d.txt
b735dd8 add c.txt
cc0d7bc add b.txt
fb8e408 add a.txt

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  new.txt

修改 a.txt 和 e.txt 文件:

echo branch_e > a.txt
echo branch_e > e.txt

$ git status
On branch branch_e
Your branch is up-to-date with 'origin/branch_a'.

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
        modified:   e.txt

no changes added to commit (use "git add" and/or "git commit -a")
# 提交
$ git add .

$ git commit -m "branch_e | update a.txt | update e.txt"
[branch_e 670efff] branch_e | update a.txt | update e.txt
 2 files changed, 2 insertions(+), 1 deletion(-)
 
$ git log --oneline
670efff (HEAD -> branch_e) branch_e | update a.txt | update e.txt
467223c (origin/branch_a) branch_a | update a.txt | add new.txt
76e47a0 add f.txt
e345ccf add e.txt
ecd639c add d.txt
b735dd8 add c.txt
cc0d7bc add b.txt
fb8e408 add a.txt
# pull远程分支
$ git pull --rebase origin master
From https://gitee.com/zsx242030/tm
 * branch            master     -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: branch_e | update a.txt | update e.txt
error: Failed to merge in the changes.
Using index info to reconstruct a base tree...
M       a.txt
M       e.txt
Falling back to patching base and 3-way merge...
Auto-merging e.txt
CONFLICT (content): Merge conflict in e.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Patch failed at 0001 branch_e | update a.txt | update 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_d
=======
branch_e
>>>>>>> branch_e | update a.txt | update e.txt

zhangshixing@DESKTOP-CR3IL33 MINGW64 ~/Desktop/E/tm (branch_e|REBASE 1/1)
$ cat e.txt
<<<<<<< HEAD
branch_d
=======
branch_e
>>>>>>> branch_e | update a.txt | update e.txt

# 解决冲突
$ cat a.txt
branch_a
branch_b
branch_c
branch_d
branch_e

$ cat e.txt
branch_d
branch_e
$ git status
rebase in progress; onto 5aacb5f
You are currently rebasing branch 'branch_e' on '5aacb5f'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   a.txt
        both modified:   e.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git add .

$ git rebase --continue
Applying: branch_e | update a.txt | update e.txt

$ git status
On branch branch_e
Your branch is ahead of 'origin/branch_a' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git log --oneline
49ec523 (HEAD -> branch_e) branch_e | update a.txt | update e.txt
5aacb5f (origin/master, origin/branch_d, origin/HEAD, master) branch_d | update a.txt | delete b.txt | update e.txt
08db352 (origin/branch_c) branch_c | update a.txt | delete e.txt
bcb903c (origin/branch_b) branch_b | update a.txt | add new.txt
467223c (origin/branch_a) branch_a | update a.txt | add new.txt
76e47a0 add f.txt
e345ccf add e.txt
ecd639c add d.txt
b735dd8 add c.txt
cc0d7bc add b.txt
fb8e408 add a.txt
# 推送到远程
$ git push origin branch_e:branch_e
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 384 bytes | 384.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'branch_e' on Gitee by visiting:
remote:     https://gitee.com/zsx242030/tm/pull/new/zsx242030:branch_e...zsx242030:master
To https://gitee.com/zsx242030/tm.git
 * [new branch]      branch_e -> branch_e
# 合并分支
# master分支操作
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

# 合并
$ git merge origin/branch_e
$ git merge branch_e
Updating 5aacb5f..49ec523
Fast-forward
 a.txt | 3 ++-
 e.txt | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

# 推送
$ 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
   5aacb5f..49ec523  master -> master

$ git log --pretty=format:"%h: %cd %s" --graph
* 49ec523: Wed Jun 7 21:10:05 2023 +0800 branch_e | update a.txt | update e.txt
* 5aacb5f: Wed Jun 7 20:58:08 2023 +0800 branch_d | update a.txt | delete b.txt | update e.txt
* 08db352: Wed Jun 7 20:03:36 2023 +0800 branch_c | update a.txt | delete e.txt
* bcb903c: Wed Jun 7 20:00:08 2023 +0800 branch_b | update a.txt | add new.txt
* 467223c: Wed Jun 7 19:54:15 2023 +0800 branch_a | update a.txt | add new.txt
* 76e47a0: Wed Jun 7 19:47:08 2023 +0800 add f.txt
* e345ccf: Wed Jun 7 19:47:08 2023 +0800 add e.txt
* ecd639c: Wed Jun 7 19:47:07 2023 +0800 add d.txt
* b735dd8: Wed Jun 7 19:47:07 2023 +0800 add c.txt
* cc0d7bc: Wed Jun 7 19:47:06 2023 +0800 add b.txt
* fb8e408: Wed Jun 7 19:47:06 2023 +0800 add a.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值