I'm working on a bash script for my team to enforce regular rebasing of working branches. The problem I am facing at the moment is how to determine whether a branch is behind master and/or if it needs to be rebased, rather than blindly attempting to rebase the branch.
我正在为我的团队制作一个bash脚本,以强制定期对工作分支进行重新定位。我目前面临的问题是如何确定分支是否落后于主分支和/或是否需要重新分配,而不是盲目地尝试重新分支分支。
Here is a simplified version of what I have so far:
这是我到目前为止的简化版本:
#Process each repo in the working directory.
for repo_dir in $(ls -1); do
# if working branch is clean ...
# BEGIN update of local master
git checkout master
git fetch origin
git merge remotes/origin/master
# END update of local master
for sync_branch in $(git branch | cut -c 3-); do
if [ "$sync_branch" != "master" ]; then
# BEGIN rebase working branch
git checkout $sync_branch
git rebase master
# Do NOT push working branch to remote.
# END rebase working branch
fi
done
fi
done
Any ideas would be much appreciated. Thanks!
任何想法将不胜感激。谢谢!
1 个解决方案
#1
To tell if you need to rebase your branch, you need to find out what the latest commit is and what was the last commit that your two branches share.
要告诉您是否需要重新分支您的分支,您需要找出最新提交的内容以及您的两个分支共享的最后一次提交。
To find the latest commit on the branch:
要在分支上查找最新提交:
git show-ref --heads -s <branch name>
Then to find the last commit that your branches have in common:
然后找到分支机构共有的最后一次提交:
git merge-base <branch 1> <branch 2>
Now all you need to do is find out if these two commits are the same commit. If they are, then you don't need to rebase. If they are not, a rebase is required.
现在你需要做的就是找出这两个提交是否是同一个提交。如果是,那么你不需要变基。如果不是,则需要变基。
Example:
hash1=$(git show-ref --heads -s master)
hash2=$(git merge-base master foo/bar)
[ "${hash1}" = "${hash2}" ] && echo "OK" || echo "Rebase is required"
Though as stated in the comment, if you attempt to rebase a branch that is already up to date. Git will gracefully handle the situation and exit.
尽管如评论中所述,如果您尝试重新绑定已经是最新的分支。 Git将优雅地处理这种情况并退出。
#1
To tell if you need to rebase your branch, you need to find out what the latest commit is and what was the last commit that your two branches share.
要告诉您是否需要重新分支您的分支,您需要找出最新提交的内容以及您的两个分支共享的最后一次提交。
To find the latest commit on the branch:
要在分支上查找最新提交:
git show-ref --heads -s <branch name>
Then to find the last commit that your branches have in common:
然后找到分支机构共有的最后一次提交:
git merge-base <branch 1> <branch 2>
Now all you need to do is find out if these two commits are the same commit. If they are, then you don't need to rebase. If they are not, a rebase is required.
现在你需要做的就是找出这两个提交是否是同一个提交。如果是,那么你不需要变基。如果不是,则需要变基。
Example:
hash1=$(git show-ref --heads -s master)
hash2=$(git merge-base master foo/bar)
[ "${hash1}" = "${hash2}" ] && echo "OK" || echo "Rebase is required"
Though as stated in the comment, if you attempt to rebase a branch that is already up to date. Git will gracefully handle the situation and exit.
尽管如评论中所述,如果您尝试重新绑定已经是最新的分支。 Git将优雅地处理这种情况并退出。