git rebase 使用

参考链接:https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%8F%98%E5%9F%BA

git rebase 是站到你最新的分支,然后rebase master,就是把自己对master分支的修改应用到最新的master分支上,就好像是直接从最新的master开始修改一样。

1 创建一个文件夹study_git6,初始化,添加README并commit

mkdir study_git6
cd study_git6
git init
echo "HELLO" > README
git add .
git commit -m "master add README"

2 创建一个新的分支b1,向README中添加一句话,并commit

git checkout -b b1
echo "hello add by b1" >> README
git add .
git commit -m "b1"
cat README

3 切回master分支向README中添加内容并commit

git checkout master
cat README
echo "master add second line" >> README
git add .
git commit -m "master add second line"

4 切回b1分支,将分支rebase到最新的master分支

git checkout b1
cat README
git rebase master
vim README
git add .
git rebase --continue

结果:

[user_name@id c]$ mkdir study_git6
[user_name@id c]$ cd study_git6
[user_name@id study_git6]$ echo "HELLO" > README
[user_name@id study_git6]$ git init
Initialized empty Git repository in /study_git6/.git/
[user_name@id study_git6]$ git add .
[user_name@id study_git6]$ git commit -m "master add README"
[master (root-commit) 2ce3ffd] master add README
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README
[user_name@id study_git6]$ git checkout -b b1
Switched to a new branch 'b1'
[user_name@id study_git6]$ echo "hello add by b1" >> README
[user_name@id study_git6]$ git add .
[user_name@id study_git6]$ git commit -m "b1"
[b1 63ee85d] b1
 1 files changed, 1 insertions(+), 0 deletions(-)
[user_name@id study_git6]$ cat README 
HELLO
hello add by b1
[user_name@id study_git6]$ git checkout master
Switched to branch 'master'
[user_name@id study_git6]$ cat README
HELLO
[user_name@id study_git6]$ echo "master add second line" >> README
[user_name@id study_git6]$ git add .
[user_name@id study_git6]$ git commit -m "master add second line"
[master 3759624] master add second line
 1 files changed, 1 insertions(+), 0 deletions(-)
[user_name@id study_git6]$ cat README 
HELLO
master add second line
[user_name@id study_git6]$ git checkout b1
Switched to branch 'b1'
[user_name@id study_git6]$ cat README
HELLO
hello add by b1
[user_name@id study_git6]$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: b1
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging README
CONFLICT (content): Merge conflict in README
Failed to merge in the changes.
Patch failed at 0001 b1

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

[user_name@id study_git6]$ vim README 
[user_name@id study_git6]$ git rebase --continue
You must edit all merge conflicts and then
mark them as resolved using git add
[user_name@id study_git6]$ git add .
[user_name@id study_git6]$ git rebase --continue
Applying: b1
[user_name@id study_git6]$ git br
* b1
  master
[user_name@id study_git6]$ git st
# On branch b1
nothing to commit (working directory clean)
文章目录