这可能是如何保持 git 分支与 master 同步的重复(我假设我可以用另一个分支替换 Master),但重要的是它可以工作并且不会以错误的方式合并,所以我需要确定一下。
场景
有一个名为
v1
的分支,我创建了一个名为 v1_adminui
的分支。我已经对我的分支 v1_adminui
进行了大约 10 次提交,但是 v1
中对项目的另一部分进行了重大改进,因此我想将更改与我当前的分支同步。
我相信以下方法可以做到:
git checkout v1
git pull
git checkout v1_adminui
git merge v1
请您确认这是否正确?如果没有,任何关于如何实现这一目标的帮助将不胜感激。
由于您是该分支上唯一的工作人员,因此您应该使用
rebase
而不是 merge
。
# Get the base branch
git checkout v1
# Pull in any changes to make sure you have the latest version
git pull
# Check out your branch
git checkout v1_adminui
# Rebase your changes on top of the v1 changes
git rebase v1
# Optionally push your rebased branch
git push origin v1_adminui
推入最后一步时,您可能必须使用
--force
选项。由于您正在使用变基重写分支的历史记录,因此远程分支将具有不同的历史记录。仅当没有其他人使用此分支时才执行此操作!
有时,除了主分支之外,还意味着诸如功能等...我们部署在生产或任何其他环境中,但有时我们忘记将分支与主分支同步,因此我们错过了主分支的一些更改,以克服我们可以向分支添加验证的情况。管道
在 bitbucket-pipelines.yml 文件中添加以下步骤,并将此步骤应用于构建之前意味着我们将在其中生成 JAR。
branchsyncwith-master-validation.sh 在下面添加验证步骤
bitbucket-pipelines.yml
-----------------------------------
-step: &syncbranch-with-master-validation
name: validate sync feature branch with master
clone:
depth: 50
script:
-source ./bin/branchsyncwith-master-validation.sh
branchsyncwith-master-validation.sh:
------------------------------------
#!/usr/bin/env bash
set -ex
apt-get update && apt-get install -y git
if [ "$BITBUCKET_BRNACH" == "master" ]; then
echo "sync not required for master branch";
exit 0;
fi
git fetch origin --depth 50
git rev-list origin/master ^origin/$BITBUCKET_BRANCH
COUNT=$(git rev-list origin/master ^origin/$BITBUCKET_BRANCH -
-count)
if [ "$COUNT" -eq 0 ]; then
echo "already sync with master";
else
echo "behind the master need sync";
exit 1;
fi