git 合并commit(合并提交) 合并分支
参考博客:https://blog.csdn.net/weixin_64314555/article/details/121567879
可以使用下面命令合并commit,
假如从master分支新建的分支,有了5个commit,1, 2, 3, 4, 5
那么要想将这5次commit合并到一次,那么你需要用git lg找到commit1之前的那次commit的commit id,即master最后一次的commit id。
git rebase -i <commit_id>
commit id 那里也能用HEAD~n 啥的,不太明确,建议用git log 查看想要合并的commit然后复制commit id,大概率就不会出错。
git merge是合并分支独有的。
git rebase可用用来合并分支,也可以用来合并commit。
git cherry-pick 也可以用来合并分支,用来合并commit。
1 git merge 合并分支
我暂时了解到的,这个就是用来合并分支的。
你先站到你想要合并到的分支,然后merge你想要合并过来的分支。
例如你现在有两个分支,feature和master。你想要把feature写好的代码合并到master。
那么你先checkout到master,然后merge feature。
git checkout master
git merge feature
测试样例
1 创建一个文件夹test_merge
mkdir test_merge
2 初始化文件夹,并添加README,并commit
cd test_merge
git init .
echo "hello this is master" > README
cat README
git add .
git commit -m "master"
3 创建feature分支,修改README,添加1.cpp,并commit
git checkout -b feature
echo "add 1.cpp" > README
cat README
echo "this is 1.cpp" > 1.cpp
cat 1.cpp
git add .
git commit -m "feature"
4 合并feature 到master
git checkout master
git merge feature
git br
cat README
cat 1.cpp
结果:
[user@server c]$ mkdir test_merge
[user@server c]$ cd test_merge
[user@server test_merge]$ git init .
Initialized empty Git repository in /user/c/test_merge/.git/
[user@server test_merge]$ echo "hello this is master" > README
[user@server test_merge]$ cat README
hello this is master
[user@server test_merge]$ git add .
[user@server test_merge]$ git commit -m "master"
[master (root-commit) 67f71ab] master
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
[user@server test_merge]$ git checkout -b feature
Switched to a new branch 'feature'
[user@server test_merge]$ echo "add 1.cpp" > README
[user@server test_merge]$ cat README
add 1.cpp
[user@server test_merge]$ echo "this is 1.cpp" > 1.cpp
[user@server test_merge]$ cat 1.cpp
this is 1.cpp
[user@server test_merge]$ git add .
[user@server test_merge]$ git commit -m "feature"
[feature 80be8a2] feature
2 files changed, 2 insertions(+), 1 deletions(-)
create mode 100644 1.cpp
[user@server test_merge]$ git checkout master
Switched to branch 'master'
[user@server test_merge]$ git merge feature
Updating 67f71ab..80be8a2
Fast-forward
1.cpp | 1 +
README | 2 +-
2 files changed, 2 insertions(+), 1 deletions(-)
create mode 100644 1.cpp
[user@server test_merge]$ git br
feature
* master
[user@server test_merge]$ cat README
add 1.cpp
[user@server test_merge]$ cat 1.cpp
this is 1.cpp
[user@server test_merge]$
2 git rebase 合并分支 合并commit
git rebase 我的理解是找到两个分支共同的祖先,让把祖先之后的修改应用到另一个分支上。
2.1 合并分支
用它来合并分支可能有些奇怪。
例如现在有一个master分支,有一个feature分支,现在想要将feature分支的操作合并到master上。用rebase的话就像下面的写法
git checkout master
git rebase feature
意思就好像上master是从feature上开始的意思一样,但是效果是合并了feature
实际测试:
[user@server ~/c]$ mkdir study_git8
[user@server ~/c]$ cd study_git8
[user@server study_git8]$ git init .
Initialized empty Git repository in ~/user/c/study_git8/.git/
[user@server study_git8]$ echo "master" >> README
[user@server study_git8]$ git add .
[user@server study_git8]$ git commit -m "master"
[master (root-commit) b864189] master
1 file changed, 1 insertion(+)
create mode 100644 README
[user@server study_git8]$ git checkout -b feature
Switched to a new branch 'feature'
[user@server study_git8]$ echo "add by feature" >> README
[user@server study_git8]$ git add .
[user@server study_git8]$ git commit -m "feature"
[feature c880f57] feature
1 file changed, 1 insertion(+)
[user@server study_git8]$ cat README
master
add by feature
[user@server study_git8]$ git checkout master
Switched to branch 'master'
[user@server study_git8]$ cat README
master
[user@server study_git8]$ git rebase feature
First, rewinding head to replay your work on top of it...
Fast-forwarded master to feature.
[user@server study_git8]$ cat README
master
add by feature
[user@server study_git8]$ git st
On branch master
nothing to commit, working directory clean
[user@server study_git8]$ git log
commit c880f5735dd7c7d979a6ff5c38e51e5530d091cf
Author: user <[email protected]>
Date: Tue Oct 24 15:04:44 2023 +0800
feature
commit b8641899615fc6d987cec90133f2d7e0e5f75e88
Author: user <[email protected]>
Date: Tue Oct 24 15:03:50 2023 +0800
master
[user@server study_git8]$ git br
feature
* master
2.2 合并commit
这个其实也挺奇怪的
假如我现在有一个master 分支有3次commmit , 1,2,3
如果使用rebase进行commit合并,那么写法如下
commit id为commit1的前一次的id
commit id也可以写成HEAD~3,这个有时候不太好记,我一般是给commit id
git rebase -i <commit_id before commit 1>
实例:
[user@server ~/c]$ mkdir study_git9
[user@server ~/c]$ cd study_git9
[user@server study_git9]$ git init .
Initialized empty Git repository in ~/user/c/study_git9/.git/
[user@server study_git9]$ echo "master" > README
[user@server study_git9]$ git commit -m "master"
On branch master
Initial commit
Untracked files:
README
nothing added to commit but untracked files present
[user@server study_git9]$ git add .
[user@server study_git9]$ git commit -m "master"
[master (root-commit) 613c70a] master
1 file changed, 1 insertion(+)
create mode 100644 README
[user@server study_git9]$ git lg
* commit 613c70a37e6261486b0e19246ea3faca74086e05 (HEAD -> master)
Author: user <[email protected]>
Date: Tue Oct 24 15:29:31 2023 +0800
master
[user@server study_git9]$ echo "1" >> README
[user@server study_git9]$ git add .
[user@server study_git9]$ git commit -m "1"
[master d7010ab] 1
1 file changed, 1 insertion(+)
[user@server study_git9]$ echo "2" >> README
[user@server study_git9]$ git add .
[user@server study_git9]$ git commit -m "2"
[master 44ecd32] 2
1 file changed, 1 insertion(+)
[user@server study_git9]$ echo "3" >> README
[user@server study_git9]$ git add .
[user@server study_git9]$ git commit -m "3"
[master 88d9ac1] 3
1 file changed, 1 insertion(+)
[user@server study_git9]$ git lg
* commit 88d9ac1f3b2280d478215d7e29c38150126699dd (HEAD -> master)
| Author: user <[email protected]>
| Date: Tue Oct 24 15:30:22 2023 +0800
|
| 3
|
* commit 44ecd3217d13753b8f01cc3f43ea098e8e35ca7e
| Author: user <[email protected]>
| Date: Tue Oct 24 15:30:14 2023 +0800
|
| 2
|
* commit d7010ab8e316ae799e3283d95cbeb634d1758d6c
| Author: user <[email protected]>
| Date: Tue Oct 24 15:30:04 2023 +0800
|
| 1
|
* commit 613c70a37e6261486b0e19246ea3faca74086e05
Author: user <[email protected]>
Date: Tue Oct 24 15:29:31 2023 +0800
master
[user@server study_git9]$ git rebase -i 613c70a37e6261486b0e19246ea3faca74086e05
[detached HEAD d59e4b6] 1
Date: Tue Oct 24 15:30:04 2023 +0800
1 file changed, 3 insertions(+)
Successfully rebased and updated refs/heads/master.
[user@server study_git9]$ git lg
* commit d59e4b652d7229930da60563b83827a99ac2fc62 (HEAD -> master)
| Author: user <[email protected]>
| Date: Tue Oct 24 15:30:04 2023 +0800
|
| 1
|
| 2
|
| 3
|
* commit 613c70a37e6261486b0e19246ea3faca74086e05
Author: user <[email protected]>
Date: Tue Oct 24 15:29:31 2023 +0800
master
用HEAD~3
[user@server study_git10]$ git rebase -i HEAD~3
[detached HEAD 7c5cf1f] 1
Date: Tue Oct 24 15:34:47 2023 +0800
1 file changed, 3 insertions(+)
Successfully rebased and updated refs/heads/master.
[user@server study_git10]$ git lg
* commit 7c5cf1f5ee3ba98b57d58265c6d4f72ace6f36e8 (HEAD -> master)
| Author: user <[email protected]>
| Date: Tue Oct 24 15:34:47 2023 +0800
|
| 1
|
| 2
|
| 3
|
* commit 7e0207bddbbefded4754b4241d5ce08d1cd81da0
Author: user <[email protected]>
Date: Tue Oct 24 15:34:26 2023 +0800
master