1

我有以下 Git 历史:

之前的 Git 历史

我想从提交1f63(之前的 2 次提交)到 HEAD 的交互式变基feature/project-setup如下:

git rebase -i HEAD~2

然后该git-rebase-todo文件具有以下几行:

pick ff7abc8 Install initial project site packages
pick 1696181 Add `.bumpversion.cfg`

如果我将第一行更改为edit,应用我的更改,然后执行git commit --amendand git rebase --continue,我的提交历史现在看起来像这样:

之后的 Git 历史

我了解交互式 git rebase 正在挑选两个提交到 rebase 的根目录(在本例中为 commit 1f63)。我的问题是,我怎样才能覆盖1696e16f让分支保持不变?(我希望我的最终历史看起来像原来的一样,但1696会被e16f包含我更改的新提交所取代)

我最初的想法是我可能首先需要挑选那些提交,然后删除它们,添加中断,结帐feature/project-setup,然后进行快进合并提交。有什么想法吗?

编辑:如果有一种更简单的方法可以做到这一点,不需要重置开发、掌握,0cea8然后重新合并,请告诉我。

4

2 回答 2

0

如果有一种更简单的方法可以做到这一点,不需要重置开发,掌握,0cea8然后重新合并,请告诉我。

没有,但重置的概念0cea8也是无稽之谈。只能“重置”分支名称(好吧,还有 Git 的索引和您的工作树,因为 Git 在git reset命令中塞入了太多东西)。请参阅下面的一件大事以了解有关分支名称的信息。

关于提交,有几件事需要了解:

  • 它们是完全、完全只读的。
  • 它们的真实名称是它们的哈希 ID,它是根据提交的内容计算的(包括元数据——从技术上讲,它仅根据元数据计算,因为保存的快照是元数据)。
  • 任何一个给定提交的元数据都会给出其前身提交(如果它是常规单父提交)或其所有父提交(如果它是合并提交)的原始哈希 ID。

这些放在一起意味着您始终可以将提交复制到新的改进版本,但是这样做之后,您现在必须复制每个“下游”(后续)提交。那是因为,给定以下内容:

... <-F <-G <-H

H序列中的“最后一个”提交在哪里,如果我们已经复制到F一个新的和改进的F',我们将需要一个新的和改进G的地方,其中改进,或者至少其中一个,是的父G'F'。然后我们还需要复制H到一个新的和改进的H',以便它的父级可以G'

关于分支名称,实际上只有一件大事要知道,比如master和——几乎develop所有其他事情都来自这一大事——那就是每个人只拥有一个提交哈希 ID。无论哈希 ID 存储在分支名称中,该提交都是该分支的最后一次提交:

...--F--G--H   <-- branch

分支的最后一次branch提交是 commit H。是 commitH本身导致 commitG成为 branch 的一部分branch,然后是 commitG本身导致 commitF成为分支的一部分,依此类推,追溯历史。历史一组提交:不多也不少。

所以,如果你有一些你不喜欢的历史,你可以建立新的历史——新的提交——你喜欢,但是即使对过去的提交做了一点改变,新的和改进的提交也会有一个不同的哈希 ID,并且此更改会在其余提交中产生涟漪效应。

Git 2.18 学习了一个新--rebase-merges选项,让您不仅可以通过自动复制单个提交,还可以重复合并操作来重建某些历史记录(不能复制合并提交,因此必须重新执行合并)。这可以帮助您完成大部分工作,但仍需要手动调整分支名称,以便指向新的和改进的提交。

于 2021-04-30T02:29:14.127 回答
0

更新: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-tagsrebase-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从您的存储库的提示提交。

于 2021-04-30T04:32:35.197 回答