在保留提交时间戳的同时执行git rebase是否有意义?

我相信结果将是新的分支不一定有按时间顺序提交的日期。这在理论上可行吗?(例如使用管道命令;只是好奇)

如果这在理论上是可能的,那么在实践中是否可能使用rebase而不更改时间戳?

例如,假设我有以下树:

master <jun 2010>
  |
  :
  :
  :     oldbranch <feb 1984>
  :     /
oldcommit <jan 1984>

现在,如果我在master上重新创建oldbranch,提交的日期从1984年2月更改为2010年6月。是否有可能改变这种行为,从而不改变提交时间戳?最后,我将得到:

      oldbranch <feb 1984>
      /
 master <jun 2010>
    |
    :

这有意义吗?git中是否允许有历史记录,即旧的提交有最近的提交作为父?


当前回答

post-rewrite钩

这个钩子适用于所有的git rebase, git pull -rebase和git commit -amend。

. / / post-rewrite hook

set -eu
echo post-rewrite
if [ ! "${CIROSANTILLI_GITHOOKS_DISABLE:-0}" = 1 ]; then
  declare -a olds
  declare -A oldnew
  while IFS= read -r line; do
    echo "$line"
    old="$(echo "$line" | cut -d ' ' -f1)"
    new="$(echo "$line" | cut -d ' ' -f2)"
    oldnew[$old]="$new"
    olds+=("$old")
    news+=("$new")
  done
  git reset --hard "${news[0]}~"
  for old in "${olds[@]}"; do
    new="${oldnew[$old]}"
    git cherry-pick "$new" &>/dev/null
    olddate="$(git log --format='%cd' -n 1 "$old")"
    CIROSANTILLI_GITHOOKS_DISABLE=1 \
      GIT_COMMITTER_DATE="$olddate" \
      git commit \
      --amend \
      --no-edit \
      --no-verify \
      &>/dev/null \
    ;
  done
  echo
fi

GitHub上游。

别忘了:

chmod +x .git/hooks/post-rewrite

这是一种很好的方法——在选定的回购上默认设置committer-date-is-author-date,直到有人最终修补配置以默认设置它。

它还可以用。committer-date-is-author-date,这似乎不会在git pull时暴露。

参见:

什么git挂钩适用于'git rebase -continue'? 男人githooks

在git 2.19, Ubuntu 18.04上测试。

其他回答

这是我在我的案例中使用的命令:

GIT_AUTHOR_EMAIL=xaionaro@dx.center git rebase --root -x "bash -c 'git commit --amend --reset-author -CHEAD --date=\"\$(git show --format=%ad -s)\"'"

这里git show——format=%ad -s提取当前日期,——date重新执行它。然后rebase——root -x执行bash -c 'git commit——modify——reset-author -CHEAD——date="$(git show——format=%ad -s)"’。

如果你已经搞砸了提交日期(可能是用了一个rebase),想要将它们重置为对应的作者日期,你可以运行:

git filter-branch——env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE;出口GIT_COMMITTER_DATE”

真正的解决方案似乎来自Reddit。稍微放大一下,是这样的:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit --allow-empty --allow-empty-message' rebase -i

冯·C的一个关键问题帮助我理解了发生了什么:当你重基时,提交者的时间戳改变了,但作者的时间戳没有改变,这突然之间就说得通了。所以我的问题其实不够精确。

答案是,rebase实际上不会改变作者的时间戳(您不需要为此做任何事情),这非常适合我。

post-rewrite钩

这个钩子适用于所有的git rebase, git pull -rebase和git commit -amend。

. / / post-rewrite hook

set -eu
echo post-rewrite
if [ ! "${CIROSANTILLI_GITHOOKS_DISABLE:-0}" = 1 ]; then
  declare -a olds
  declare -A oldnew
  while IFS= read -r line; do
    echo "$line"
    old="$(echo "$line" | cut -d ' ' -f1)"
    new="$(echo "$line" | cut -d ' ' -f2)"
    oldnew[$old]="$new"
    olds+=("$old")
    news+=("$new")
  done
  git reset --hard "${news[0]}~"
  for old in "${olds[@]}"; do
    new="${oldnew[$old]}"
    git cherry-pick "$new" &>/dev/null
    olddate="$(git log --format='%cd' -n 1 "$old")"
    CIROSANTILLI_GITHOOKS_DISABLE=1 \
      GIT_COMMITTER_DATE="$olddate" \
      git commit \
      --amend \
      --no-edit \
      --no-verify \
      &>/dev/null \
    ;
  done
  echo
fi

GitHub上游。

别忘了:

chmod +x .git/hooks/post-rewrite

这是一种很好的方法——在选定的回购上默认设置committer-date-is-author-date,直到有人最终修补配置以默认设置它。

它还可以用。committer-date-is-author-date,这似乎不会在git pull时暴露。

参见:

什么git挂钩适用于'git rebase -continue'? 男人githooks

在git 2.19, Ubuntu 18.04上测试。