Skip to content
Created: November 18, 2016

rebase branch merge ​

Description ​

The following v2 is the development branch of a certain requirement, dev is the general development branch, and v2 is checked out based on the dev branch.

After completing the development of v2, the code needs to be merged into dev. We can use rebase to merge:

sh
# 首先将 v2 push到远程仓库
git add .
git commit -m 'xxx'
git push origin v2

# 切换到 dev 拉取最新代码
git checkout dev
git pull origin dev

# 切换到 v2
git checkout v2
git rebase dev # 将 v2 的所有[commit] 变基到(应用到) dev

# 切换到 dev
git checkout dev
git merge v2  # 将 dev分支 快进合并 (此时 (HEAD -> dev, v2) [commit] 两个分支指向同一个提交)

# 查看 原v2的[commit]记录 是否在dev分支的最前面(变基成功会把v2的提交记录应用到dev分支的最前面)
git log

	# 如果到这一步发现有问题,尝试使用 git --abort中止变基,如果还是有问题的可以在dev分支上使用《后悔药》操作, 再到v2分支上使用《后悔药》操作,即可使两个分支都回退到 rebase变基 之前的状态

# 试运行项目是否有问题
yarn start

git status # 查看状态是否有问题
git push origin dev # 推送到远程仓库的 dev

Guidelines to follow when rebasing ​

When several people are developing and submitting on a branch at the same time, please do not perform rebase privately during development. Rebase can only be performed after everyone has completed their work.

The essence of rebasing ​

The essence of the rebase operation is to discard some existing commits and then create new commits that have the same content but are actually different. Therefore, The branch after the rebase operation will no longer be used. 。

regret medicine ​

sh
# 查看HEAD指针变动记录
git reflog
# 记录示例(当前分支是v2):
07c398f (HEAD -> v2, master) HEAD@{0}: checkout: moving from master to v2
07c398f (HEAD -> v2, master) HEAD@{1}: rebase (finish): returning to refs/heads/master
07c398f (HEAD -> v2, master) HEAD@{2}: rebase (start): checkout v2
15a97d8 HEAD@{3}: reset: moving to 15a97d8
07c398f (HEAD -> v2, master) HEAD@{4}: merge v2: Fast-forward
15a97d8 HEAD@{5}: checkout: moving from v2 to master
07c398f (HEAD -> v2, master) HEAD@{6}: rebase (finish): returning to refs/heads/v2
07c398f (HEAD -> v2, master) HEAD@{7}: rebase (pick): C
15a97d8 HEAD@{8}: rebase (start): checkout master # 首次rebase
d278ecd HEAD@{9}: checkout: moving from master to v2 # rebase前的状态
15a97d8 HEAD@{10}: commit: D

# 可见,示例中最初的 rebase 操作是 HEAD@{8},想回退到变基前的状态需让指针指向 HEAD@{9}
git reset --hard d278ecd  # 重置当前分支的HEAD为指定[commit],同时重置暂存区和工作区,与指定[commit]一致

# 此时打印 log 查看是否回到之前的状态
git log

Note: This operation can only roll back the current branch. If you want to roll back other branches, you need to switch to that branch and perform the above operation.

Rebase operations during development ​

background ​

There are two branches:

sh
dev
*v2

v2 is cut out based on dev.

The submission record is as follows:

		dev
a - b - c
		v2

During development, both branches had new commits at the same time:

				dev
a - b - c - d - e
		\ - f - g
				v2

You are currently developing on v2, and dev is also developing at the same time, and there are major changes. You need to synchronize the commits of dev to v2.

Requirement: Synchronize new commits in dev to v2 without affecting the dev branch.

Operation steps ​

  1. Cut a new branch dev-copy based on the latest dev

    The commit IDs of dev-copy and dev are the same.

  2. Execute rebase in dev-copy to rebase the commit of dev-copy to v2

    sh
    git rebase v2 # 将 dev-copy 的提交[commit] 变基到(应用到) v2
  3. Delete the original v2 branch and change the dev-copy branch name to v2

    sh
    # 当前在 dev-copy 分支
    git branch -d v2 # 删除分支
    git branch -m dev-copy v2 # 重命名

git cherry-pick ​

Source: "git cherry-pick tutorial"

Used to copy a single or several [commit] to another branch.

Basic application ​

sh
git cherry-pick <commitHash> # 将commitHash应用于当前分支

The above command will submit the specified commitHash , applied to the current branch.This will generate a new commit on the current branch, of course their The hash value will be different 。

The parameters of the git cherry-pick command are not necessarily the hash value of the commit. The branch name is also acceptable, indicating that the latest commit of the branch is transferred.

Transfer multiple commits ​

Cherry pick supports transferring multiple commits at once.

sh
git cherry-pick <HashA> <HashB> # A和B提交

The above command applies two commits A and B to the current branch.This will generate two corresponding new commits on the current branch.

If you want to transfer a series of consecutive commits, you can use the following simple syntax.

sh
git cherry-pick A..B # A到B提交,不包含A

The above command can transfer all commits from A to B.They must be placed in the correct order: commit A must be earlier than commit B, otherwise the command will fail without an error.

Note that using the above command, commit A will not be included in Cherry pick.If you want to include commit A, you can use the following syntax.

sh
git cherry-pick A^..B # A到B提交,包含A