git修改已经提交到远程的commit信息

本文最后更新于:9 分钟前

git修改已经提交到远程的commit信息

1.git log/git reflog

开始之前先查看log日志

git log

git log 命令可以显示所有提交过的版本信息

git reflog

git reflog 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)

2.修改最近某个commit message

找出最近提交的commit

git rebase -i HEAD~3

git rebase -i HEAD~3
HEAD~3表示将修改当前版本的近三条commit。

git rebase -i 哈希值

或者,也可以直接用git每次commit生成的哈希值——SHA-1码来定位(git log中可找到对应的哈希值)。可以使用简码,也可以使用全码,其实只要没有歧义即可。
git rebase -i ffc21e
git rebase -i ffc21e88dbad1742ae32889461c5981ea50f1283

git rebase -i –root

以上两种命令虽然可以解决大部分的情况,但是依然会有失效的时候。
这时候我们就需要用第三条命令了。
git rebase -i --root

修改

以上命令输入成功之后输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pick 733d868 first commit
pick 2640678 test1
pick cbba117 'test'

# Rebase cbba117 onto dd90bd9 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

进入/推出编辑模式

以上结果出来后,控制台输入 i 进入编辑模式,然后我们就可以进行修改了,比如我要改第一条

1.把第一条的“pick”修改为“edit”。

1
2
3
4
5
6
edit 733d868 first commit
pick 2640678 test1
pick cbba117 'test'

# Rebase cbba117 onto dd90bd9 (3 commands)

2.键盘esc退出编辑模式,:wq保存文件并退出

git commit –amend

控制台输出

1
2
3
4
5
6
7
8
Stopped at 733d868...  first commit
You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

根据提示,控制台输入
git commit --amend
或者
git commit --amend -s
正常来说你不需要加-s,如果你加了-s,是这样的 :加不加-s都是可以的,-s表示要加入签名。
修改成功之后会多一个签名 Signed-off-by: jackxxx jackxxx@foxmail.com

进入编辑模式,修改commit信息,保存并退出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
commit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon May 9 10:29:01 2022 +0800
#
# interactive rebase in progress; onto dd90bd9
# Last command done (1 command done):
# edit 733d868 first commit
# Next commands to do (2 remaining commands):
# pick 2640678 test1
# pick cbba117 'test'
# You are currently splitting a commit while rebasing branch 'master' on 'dd90bd9'.
#
#
# Initial commit
#
# Changes to be committed:
# new file: README.md

git rebase –continue

将基变合并
git rebase --continue
控制台输出以下则成功

1
Successfully rebased and updated refs/heads/master.

3.推送到远程

git push/git push -f

git push

git push 推送到远程
如果提示失败

1
2
3
4
5
6
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:jackhoo_98/test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

如果你确定你的代码没有问题的话,你可以强提代码到远程,也就是强制push

git push -f

git push -f 强制推送到远程

4.友情提醒

如果你只是为了好奇而在本地尝试了本文的以上命令,但是尝鲜过后不想要更改了,你可以使用下面的命令 。
git pull --rebase origin master
或者
git pull --rebase origin 分支名
运行了这行代码后,会pull远程仓库的代码到本地,强行覆盖本地的所有修改。
这样就和以前一样啦~