Git学习笔记(一)

135 阅读1分钟

Git学习笔记之Rebase基础操作

场景

在开发新功能时,将其他开发者提交到master分支的代码合并到自己当前开发的分支。

操作

切换到master分支并拉取最新代码

(feature1) git checkout master
(master) git pull

将master分支的代码合并到自己当前开发的功能分支

(master) git checkout feature1
(feature1) git rebase master

开发后提交代码

(feature1) git add -A
(feature1) git commit -m "My commit."
(feature1) git push
# Push之后可以在GitHub上面创建PR

心得

Rebase的流程如下

刚执行完(master) git checkout -b feature1

# 可以看到feature1的“基础”为C0
C0 -- C1 -- C2 (master分支)
 \-- C3 -- C4 -- C5 (feature1分支)

执行完(feature1) git rebase master之后

# 执行后将feature1的“基础”从C0变更为C2
C0 -- C1 -- C2 (master分支)
             \-- C3 -- C4 -- C5 (feature1分支)
             
操作后不会影响master,feature1不会被删除,代码会被加入master上最新的内容。

Rebase可以理解为将更新后master的分支重新设定为feature1的“基础(起点)”。 可以简单理解为(当然实际情况并不是)

(master) git pull master
(master) git checkout -b feature1