更新:2021 年 5 月 6 日
这是一个(相对)简单的 bash 脚本,它将更新分支名称和标签。使用风险自负!把它放在
/usr/bin/ # On Linux
或者
C:\Program Files\Git\usr\bin # On Windows
并命名
git-rebase-bti
(没有文件扩展名),并使其可执行
chmod +x path/to/git-rebase-bti
或在 Windows 上
icacls path/to/git-rebase-bti /grant your_usrnm:(rx)
虽然我相信 Windows 默认为文件提供可执行权限?(不要引用我的话)
#! /bin/sh -
#
# Git Rebase Branch Names, Tags, and Forks
#
# File:
# git-rebase-bti
#
# Installation Instructions:
# Place this file in the following folder:
# - Linux: `/usr/bin/`
# - Windows: `C:\Program Files\Git\usr\bin`
#
# Usage:
# git rebase-bti <SHA>
#
# Authors:
# Copyleft 2020 Adam Hendry. All rights reserved.
#
# Original Author:
# Copyleft 2020 Adam Hendry. All rights reserved.
#
# License:
# GNU GPL vers. 2.0
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
GIT_DIR='.git'
REBASE_DIR="${GIT_DIR}/rebase-merge"
TODO_FILE="${REBASE_DIR}/git-rebase-todo"
TODO_BACKUP="${TODO_FILE}.backup"
HEADS_FOLDER='refs/heads'
TAGS_FOLDER='refs/tags'
REWRITTEN_FOLDER='refs/rewritten'
# Initialize associative array (dictionary) variables
declare -A labels_by_sha # Rebase label names indexed by original SHA
declare -A shas_by_label # Original SHAs indexed by rebase label names
# Get heads (remove '.git/refs/heads' from beginning)
heads=($(find "${GIT_DIR}/${HEADS_FOLDER}" -type f | cut -d '/' -f 4-))
# Get tags (remove '.git/refs/tags' from beginning)
tags=($(find "${GIT_DIR}/${TAGS_FOLDER}" -type f | cut -d '/' -f 4-))
# Start the rebase operation in the background
git rebase -i --rebase-merges $1 &
# Capture the process ID
pid_main=$!
# Wait until the todo file is created
until [ -e "$TODO_FILE" ] && [ -e "$TODO_BACKUP" ]
do
continue
done
# Store rebase message
rebase_message=$(tac $TODO_FILE | sed '/^$/q' | tac)
# Store todo list
rebase_message_length=$(echo "$rebase_message" | wc -l)
todo_list=$(cat $TODO_FILE | head -n -"$rebase_message_length")
# Prompt user
printf "Calculating todo file. Please wait..." > $TODO_FILE
# Get label names
label_names=($(grep -oP '^(l|label) \K[^ ]*$' -- $TODO_BACKUP))
for label_name in "${label_names[@]}"
do
if [ $label_name = 'onto' ]
then
continue
fi
command_line=$(grep -B 1 -P '^(l|label) '"$label_name"'$' $TODO_BACKUP | head -n 1 | sed 's/\n//g')
command_name=$(echo "$command_line" | grep -oP '^(p|pick|m|merge)(?= )')
label_sha=
if [ "$command_name" = 'p' ] || [ "$command_name" = 'pick' ]
then
label_sha=$(echo $command_line | grep -oP '^(p|pick) \K[[:alnum:]]*' | cut -c1-7)
elif [ "$command_name" = 'm' ] || [ "$command_name" = 'merge' ]
then
label_sha=$(echo $command_line | grep -oP '^(m|merge) -[cC] \K[[:alnum:]]*' | cut -c1-7)
fi
shas_by_label["$label_name"]="$label_sha"
labels_by_sha["$label_sha"]="$label_name"
done
# Restore Branch Names
todo_list+="\n\n# Restore Branch Names\n"
for head in "${heads[@]}"
do
sha=$(cat "${GIT_DIR}/${HEADS_FOLDER}/${head}" | cut -c1-7)
if [ -n "${labels_by_sha[$sha]}" ]
then
todo_list+='exec git update-ref '"${HEADS_FOLDER}/${head}"' '"${REWRITTEN_FOLDER}/${labels_by_sha[$sha]}\n"
fi
# elif in `git rev-list`, pick sha and label it, then `git update-ref` here`
done
todo_list+='\n# Restore Tag Names\n'
for tag in "${tags[@]}"
do
sha=$(cat "${GIT_DIR}/${TAGS_FOLDER}/${tag}" | cut -c1-7)
if [ -n "${labels_by_sha[$sha]}" ]
then
todo_list+='exec git update-ref '"${TAGS_FOLDER}/${tag}"' '"${REWRITTEN_FOLDER}/${labels_by_sha[$sha]}\n"
fi
done
todo_list+="$rebase_message"
# Update todo file
printf "$todo_list" > $TODO_FILE
# Wait until the rebase operation is completed
wait $pid_main
# Exit the script
exit 0
回答:
交互式变基可用于实现这些更改,但存在于HEAD变基和根之间的分支和标记名称,否则会阻止 Git 的垃圾收集删除这些较旧的提交,必须首先删除,然后在变基后重新应用。不幸的是,为了正常工作,变基必须从你的历史的尖端开始(即develop分支)
变基从develop:
git checkout develop
git branch -D feature/project-setup
git branch -D master
git tag -d 0.1.0
git rebase -i --rebase-merges
将 添加edit到您希望更改的提交中,然后暂存更改 ( git add -A),修改提交 ( git commit --amend),并完成变基 ( git rebase --continue)。
之后,将分支和标签名称一一添加回来
git branch master cfa8
git branch feature/project-setup 1696
git checkout master
git tag 0.1.0
虽然这里开发的 git-rebasetags 脚本是一个好的开始,但它只适用于 Linux 机器,只对标签进行变基而不是分支名称,匹配标签提交消息(对非注释标签不起作用),并改用 python shell 脚本,它的可移植性稍差。
或者,rebase-todo可以更新如下:
label onto
# Branch feature-project-setup
reset onto
pick ff7abc83 Install initial project site packages
pick 1696181f Add `.bumpversion.cfg`
label feature-project-setup
# Branch release-0-1-0
reset 8e2d63e # Initial commit
merge -C c598c3bf feature-project-setup # Merge branch 'feature/project-setup' into develop
label branch-point
pick 0cea85a3 Bump version: 0.0.0 → 0.1.0
label release-0-1-0
# Branch 0-1-0
reset 8e2d63e # Initial commit
merge -C cfa8ed17 release-0-1-0 # Merge branch 'release/0.1.0' into master
label 0-1-0
reset branch-point # Merge branch 'feature/project-setup' into develop
merge -C a22db135 0-1-0 # Merge tag '0.1.0' into develop
label develop
# Reset branch and tag names
reset feature-project-setup
exec git branch -D feature/project-setup
exec git branch feature/project-setup
reset 0-1-0
exec git tag -d 0.1.0
exec git tag 0.1.0
exec git branch -D master
exec git branch master
reset develop
或者,由于 git rebase 将标签写入refs/rewritten,这可以通过一些管道命令在更少的行中完成:
exec git update-ref refs/heads/feature/project-setup refs/rewritten/feature-project-setup
exec git update-ref refs/heads/master refs/rewritten/0-1-0
exec git update-ref refs/tags/0.1.0 refs/rewritten/0-1-0
上面的标签在这个特定的例子中0-1-0同时适用于master和标签。0.1.0
如果这可以成为 rebase 的额外选项,就像--rebase-tags和rebase-branch-names. 不幸的是,pre-rebase钩子不起作用,因为这rebase-todo是在制作之前发生的。此外,没有post-rebase钩子。所以,一个单独的 shell 脚本似乎是谨慎的。
最后,上述内容不会重新设置分支点,如果重新设置路径修订列表中存在未合并的分支点,则也需要重新设置分支点。如果发生这种情况,您可以尝试在待办事项列表的末尾添加以下内容:
reset new_base_branch # Be sure to `label new_base_branch` before here at right spot
exec git branch temp_name # Give new base a temp name
exec git checkout branch_to_rebase
exec git rebase temp_name
exec git branch -D temp_name
这也是一个很好的附加选项(如--rebase-forks),但代码还需要检查branch_to_rebase实际上没有合并回onto变基路径之外的分支的子项。为了获得最佳安全性,我总是会rebase -i --rebase-merges从您的存储库的提示提交。