如何编辑的老蠢货提交信息编程?

问题描述 投票:7回答:3

您可以通过编程编辑只有最后提交信息:

git commit --amend -m 'xxxxxxx'

或随机提交交互:

git rebase -i HEAD~n
# Vim opens up, select the commit you want to modify, and change the word "pick" for "edit"
git commit --amend -m "Changing an old commit message!"
git rebase --continue

如何结合两者兼而有之?我想以编程方式更改的消息,但要事先承诺,而不仅仅是最后一个。

提交我要修改已经被推到一个Git服务器,但有其他人重新同步的Git项目是不是一个问题。

git git-rebase git-commit amend
3个回答
6
投票

如果你只是改变一些提交,使用git rebase -i和“改写”选项。例如...

pick 6256642 mv file1 file2
pick 20c2e82 Add another line to file2

# Rebase 8236784..20c2e82 onto 8236784 (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

切换到pickreword你就可以选择一个编辑器重写提交信息。


如果你需要做同样的事情到了很多的提交,使用git filter-branch--msg-filter。原来提交信息是标准输入,新提交的消息是在标准输出上。这里有一个在所有提交改变“颜色”,以“颜色”在当前分支。

git filter-branch --msg-filter "perl -ple 's{color}{colour}g'"

4
投票

你不能简单地“修正”任意提交的原因是提交是不可变的。当你修改提交,它实际上取代了当前提交与其他移动和你跳转到新的提交。与旧的消息,作者姓名等在历史上,直到你清理的承诺仍然存在:

Before:

        master
          |
          v
A -- B -- C

After:

        master
          |
          v
A -- B -- C'
      \
       \- C

为了模拟“修改”任意提交,你就必须不仅如此承诺,但之后的整个历史改写,因为提交包括其父母作为其不可改变的数据的一部分:

Before:

        master
          |
          v
A -- B -- C

After:

         master
           |
           v
A -- B' -- C'
 \ 
  \- B --- C

您可以通过创建提交你有兴趣,修改它,并提交重订之后原来你原来的分支的末端到新的分支范围内的分支做到这一点。这里是在你所追求的一个例子:

Start:

             master
               |
               v
A -- B -- C -- D

New Branch:

             master
               |
               v
A -- B -- C -- D
     ^
     |
    temp

Amend:

             master
               |
               v
A -- B -- C -- D
 \
  \- B'
     ^
     |
    temp

Rebase:

A -- B  -- C  -- D
 \
  \- B' -- C' -- D'
     ^           ^
     |           |
    temp       master

Cleanup:

A -- B  -- C  -- D
 \
  \- B' -- C' -- D'
                 ^
                 |
               master

这是非常正是交互式rebase才会时,你只修改单个提交,顺便说一句,除了没有明确的临时党支部。


0
投票

由于要进行编程的变化,互动变基(GIT变基-i)是不是一种选择。

编辑老犯,无论出于何种原因,将有效地重订所有提交的在此之上。如果你只改变提交信息,那么你就不需要担心合并冲突。

您创建一个新的临时党支部与目标承诺任组长,编辑提交信息,旧的分支合并到新的一个,然后删除旧的临时党支部。

在一个shell脚本:

CURBRANCH=`git rev-parse --abbrev-ref HEAD`
TMPBRANCH=tmp$$
git checkout $SHA -b $TMPBRANCH
MSG=`tempfile`
git log --format=%B -n 1 HEAD > $MSG
... edit the $MSG file
git commit --amend -F $MSG
SHA=`git rev-list -n 1 HEAD`   # Commit has change, so SHA has also changed
rm $MSG
git rebase --onto $TMPBRANCH HEAD $CURBRANCH
git branch -d $TMPBRANCH
© www.soinside.com 2019 - 2024. All rights reserved.