b2977053
3/19/2020 - 9:15 AM

willwill_git_day22

# 建立資料夾 與 版本庫
mkdir git-rebase-demo
cd git-rebase-demo
git init

# 新增檔案 a.txt、b.txt 並分別提交。
echo 'a.txt first commit' > a.txt
git add .
git commit -m 'a.txt first commit'
echo 'b.txt first commit' > b.txt
git add .
git commit -m 'b.txt first commit'

# 新增分支 branch_1
git checkout -b branch_1

# 新增檔案 C.txt、D.txt 並分別提交。
echo 'C.txt first commit' > C.txt
git add .
git commit -m 'C.txt first commit'
echo 'D.txt first commit' > D.txt
git add .
git commit -m 'D.txt first commit'

# 回 master 繼續新增 E.txt
git checkout master
echo 'E.txt first commit' > E.txt
git add .
git commit -m 'E.txt first commit'


#
# git rebase
#

# 將 branch_1 分支的基礎版本換成 master 分支
git checkout branch_1
git rebase master

#
# 快轉機制 (Fast-forward)
#

git checkout master
git merge branch_1
git log --oneline

# (HEAD -> master, branch_1) D.txt first commit
# C.txt first commit
# E.txt first commit
# b.txt first commit
# a.txt first commit

#
# 重新 merge,關閉 快轉機制 (Fast-forward)
#

git reset --hard ORIG_HEAD
git merge branch_1 --no-ff
git log --oneline

# (HEAD -> master) Merge branch 'branch_1'
# (branch_1) D.txt first commit
# C.txt first commit
# E.txt first commit
# b.txt first commit
# a.txt first commit