I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.
我在提交消息中写错了。或者,我忘了包含一些文件。
How can I change the commit message/files? The commit has not been pushed yet.
如何更改提交消息/文件?承诺还没有被推出。
27 个解决方案
#1
14661
Amending the most recent commit message
git commit --amend
will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:
将打开您的编辑器,允许您更改最近提交的提交消息。此外,您可以在命令行中直接设置提交消息:
git commit --amend -m "New commit message"
…however, this can make multi-line commit messages or small corrections more cumbersome to enter.
然而,这可能会使多行提交消息或小的修改更难以输入。
Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)
确保在进行此操作之前没有任何工作拷贝更改,否则它们也会被提交。(未分阶段的更改不会被提交。)
Changing the message of a commit that you've already pushed to your remote branch
If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with:
如果您已经将提交推到远程分支,那么需要强制执行提交:
git push <remote> <branch> --force# Orgit push <remote> <branch> -f
Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.
警告:强制推送将覆盖您本地的远程分支。如果在您的本地分支中没有您的远程分支,那么您将丢失这些提交。
Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.
警告:对于已经与他人共享的提交,要小心修改。修改提交本质上重写了它们,使它们具有不同的SHA id,如果其他人拥有您重写的旧提交的副本,则会产生问题。任何拥有旧提交副本的人都需要将他们的工作与您新重写的提交同步,这有时会很困难,所以在尝试重写共享提交历史记录时,请确保与其他人协调,或者只是避免完全重写共享提交。
Use interactive rebase
Another option is to use interactive rebase.
This allows you to edit any message you want to update even if it's not the latest message.
另一种选择是使用交互式重基。这允许您编辑任何您想要更新的消息,即使它不是最新的消息。
In order to do a git squash, follow these steps:
为了做一个git壁球,请遵循以下步骤:
// X is the number of commits to the last commit you want to be able to editgit rebase -i HEAD~X
Once you squash your commits - choose the e/r
for editing the message
一旦你压缩提交-选择e/r编辑消息
Important note about Interactive rebase
When you use the git rebase -i HEAD~X
there can be more than X
commits. Git will "collect" all the commits in the last X
commits and if there was a merge somewhere in between that range you will see all the commits as well so the outcome will be X+.
当您使用git rebase -i HEAD~X时,可能会有更多的提交。Git将“收集”上一个X提交中的所有提交,如果在这个范围内有合并,您将看到所有的提交,因此结果是X+。
Good tip:
If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere
and let git resolve those conflicts automatically for you.
如果您需要对多个分支进行此操作,并且在修改内容时可能会遇到冲突,那么可以设置git rerere,并让git自动为您解决这些冲突。
Documentation
-
git-commit(1)手册页
-
git-rebase(1)手册页
-
git-push(1)手册页
#2
2402
git commit --amend -m "your new message"
#3
2281
If the commit you want to fix isn’t the most recent one:
如果你想修复的承诺不是最近的:
-
git rebase --interactive $parent_of_flawed_commit
git变基——交互式parent_of_flawed_commit美元
If you want to fix several flawed commits, pass the parent of the oldest one of them.
如果您想修复几个有缺陷的提交,请传递其中最老的一个的父类。
-
An editor will come up, with a list of all commits since the one you gave.
将出现一个编辑器,其中包含自您提供的提交以来的所有提交。
- Change
pick
toreword
(or on old versions of Git, toedit
) in front of any commits you want to fix. - 在您希望修复的任何提交之前,将pick更改为reword(或老版本的Git)。
- Once you save, Git will replay the listed commits.
- 保存之后,Git将重播列出的提交。
- Change
-
For each commit you want to reword, Git will drop you back into your editor. For each commit you want to edit, Git drops you into the shell. If you’re in the shell:
对于您想要重述的每个提交,Git都将把您送回编辑器。对于要编辑的每个提交,Git都会将您放入shell中。如果你在壳层:
- Change the commit in any way you like.
- 以您喜欢的任何方式更改提交。
git commit --amend
- git commit -修改
git rebase --continue
- git变基——继续
Most of this sequence will be explained to you by the output of the various commands as you go. It’s very easy, you don’t need to memorise it – just remember that git rebase --interactive
lets you correct commits no matter how long ago they were.
这个序列的大部分将通过您执行的各种命令的输出进行解释。这非常简单,您不需要记住它—只需记住git rebase—interactive允许您纠正提交,无论它们在多长时间以前。
Note that you will not want to change commits that you have already pushed. Or maybe you do, but in that case you will have to take great care to communicate with everyone who may have pulled your commits and done work on top of them. How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?
请注意,您将不希望更改已提交的提交。也许你会这么做,但在这种情况下,你必须非常小心地与每个可能已经完成你的任务并在其之上完成工作的人进行沟通。当有人将重基或重设推到已发布的分支后,我如何恢复/重新同步?
#4
744
To amend the previous commit, make the changes you want and stage those changes, and then run
要修改之前的提交,请进行所需的更改并执行这些更改,然后运行
git commit --amend
This will open a file in your text editor representing your new commit message. It starts out populated with the text from your old commit message. Change the commit message as you want, then save the file and quit your editor to finish.
这将在表示新的提交消息的文本编辑器中打开一个文件。它首先填充来自旧提交消息的文本。根据需要更改提交消息,然后保存文件并退出编辑器以完成。
To amend the previous commit and keep the same log message, run
要修改之前的提交并保持相同的日志消息,请运行
git commit --amend -C HEAD
To fix the previous commit by removing it entirely, run
要完全删除之前的提交,请运行
git reset --hard HEAD^
If you want to edit more than one commit message, run
如果希望编辑多个提交消息,请运行
git rebase -i HEAD~commit_count
(Replace commit_count with number of commits that you want to edit.) This command launches your editor. Mark the first commit (the one that you want to change) as “edit” instead of “pick”, then save and exit your editor. Make the change you want to commit and then run
(用要编辑的提交数替换commit_count。)此命令启动编辑器。将第一个提交(您希望更改的提交)标记为“edit”而不是“pick”,然后保存并退出编辑器。进行您想要提交的更改,然后运行
git commit --amendgit rebase --continue
Note: You can "Make the change you want" also from the editor opened by git commit --amend
注意:您还可以从git提交—修改打开的编辑器中“进行您想要的更改”
#5
378
As already mentioned, git commit --amend
is the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be
如前所述,git提交——修改是覆盖最后一次提交的方法。注意:如果您想要覆盖文件,命令将是。
git commit -a --amend -m "My new commit message"
#6
341
You also can use git filter-branch
for that.
您还可以使用git过滤-分支。
git filter-branch -f --msg-filter "sed 's/errror/error/'" $flawed_commit..HEAD
It's not as easy as a trivial git commit --amend
, but it's especially useful, if you already have some merges after your erroneous commit message.
这并不是简单的git提交——修改,但是它特别有用,如果您在错误的提交消息之后已经有一些合并。
Note that this will try to rewrite EVERY commit between HEAD
and the flawed commit, so you should choose your msg-filter
command very wise ;-)
注意,这将尝试重写HEAD和有缺陷提交之间的每个提交,因此您应该非常明智地选择您的msg-filter命令;
#7
303
I prefer this way.
我喜欢这种方式。
git commit --amend -c <commit ID>
Otherwise, there will be a new commit with a new commit ID
否则,将会有一个带有新的提交ID的新提交
#8
300
If you are using the Git GUI tool, there is a button named amend last commit. Click on that button and then it will display your last commit files and message. Just edit that message and you can commit it with new commit message.
如果您正在使用Git GUI工具,则有一个名为amend last commit的按钮。单击该按钮,然后它将显示您的最后一个提交文件和消息。只需编辑该消息,就可以使用新的提交消息提交它。
Or use this command from a console/terminal:
或从控制台/终端使用此命令:
git commit -a --amend -m "My new commit message"
#9
276
You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run
您可以使用Git rebase。例如,如果您想修改回发bbc643cd,请运行
$ git rebase bbc643cd^ --interactive
In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with
在默认的编辑器中,将“选择”修改为要修改提交的行中的“编辑”。做出你的改变,然后和他们一起。
$ git add <filepattern>
Now you can use
现在你可以使用
$ git commit --amend
to modify the commit, and after that
修改提交,然后
$ git rebase --continue
to return back to the previous head commit.
返回到先前的head提交。
#10
267
-
If you only want to modify your last commit message, then do:
如果您只想修改您的最后提交消息,请执行以下操作:
git commit --amend
That will drop you into your text exitor and let you change the last commit message.
这会将您放入文本exitor中,并允许您更改最后的提交消息。
-
If you want to change the last 3 commit messages, or any of the commit messages up to that point, supply
HEAD~3
to thegit rebase -i
command:如果您想更改最后的3条提交消息,或任何提交消息,请向git rebase -i命令提供HEAD~3:
git rebase -i HEAD~3
#11
248
If you have to change an old commit message over multiple branches (i.e., the commit with the erroneous message is present in multiple branches) you might want to use:
如果你必须改变旧的提交消息(即多个分支机构。,提交错误信息出现在多个分支)您可能想要使用:
git filter-branch -f --msg-filter \'sed "s/<old message>/<new message>/g"' -- --all
Git will create a temporary directory for rewriting and additionally backup old references in refs/original/
.
Git将创建一个临时目录,用于重写并在refs/original/中备份旧引用。
-
-f
will enforce the execution of the operation. This is necessary if the the temporary directory is already present or if there are already references stored underrefs/original
. If that is not the case, you can drop this flag.-f将强制执行操作。如果临时目录已经存在,或者已经有引用存储在refs/original下,则需要这样做。如果不是这样,您可以删除这个标志。
-
--
separates filter-branch options from revision options.——分离filter-branch选项修改选项。
-
--all
will make sure, that all branches and tags are rewritten.——所有这些都将确保所有分支和标记都被重写。
Due to the backup of your old references, you can easily go back to the state before executing the command.
由于备份了旧的引用,您可以很容易地在执行命令之前返回到状态。
Say, you want to recover your master and access it in branch old_master
:
比方说,你想要恢复你的master并在分支old_master中访问它:
git checkout -b old_master refs/original/refs/heads/master
#12
211
Use
使用
git commit --amend
To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use git commit --amend
.
要详细了解它,最好的文章是4。Git重写历史。它还讨论了何时不使用git提交——修改。
#13
188
Amend
You have a couple of options here. You can do
这里有几个选项。你可以做
git commit --amend
as long as it's your last commit.
只要这是你最后的承诺。
Interactive rebase
Otherwise if it's not your last commit you can do an interactive rebase,
否则,如果这不是你最后一次提交你可以做一个互动的重新基础,
git rebase -i [branched_from] [hash before commit]
Then inside the interactive rebase you simply add edit to that commit. When it comes up do a git commit --amend
and modify the commit message. If you want to roll back before that commit point you could also use git reflog
and just delete that commit. Then you just do a git commit
again.
然后在交互式的rebase中,您只需添加编辑到该提交。当它出现时,执行git提交——修改和修改提交消息。如果您想在提交点之前回滚,您还可以使用git reflog并删除该提交。然后再做一次git提交。
#14
176
If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:
如果您正在使用Git GUI,您可以修改最后一次提交,但没有使用:
Commit/Amend Last Commit
#15
162
If it's your last commit, just amend the commit:
如果是你最后一次提交,只需修改提交:
git commit --amend -o -m "New commit message"
(using the -o
(--only
) flag to make sure you change only the commit message)
(使用-o (only)标志确保只更改提交消息)
If it's a buried commit, use the awesome interactive rebase:
如果是隐藏的承诺,使用令人敬畏的交互基础:
git rebase -i @~9 # Show the last 9 commits in a text editor
Find the commit you want, change pick
to r
(reword
), and save and close the file. Done!
找到您想要的提交,将选择改为r (reword),并保存并关闭文件。完成了!
Miniature vim tutorial (or, how to rebase with only 8 keystrokes 3j
cw
r
EscZZ
):
迷你vim教程(或者,如何仅用8个按键重设3jcwrEscZZ):
- Run
vimtutor
if you have time - 如果有时间,请运行vimtutor
-
h
j
k
l
correspond to movement keys ←↓↑→ - hjkl对应移动键←↓↑→
- All commands can be prefixed with a "range", e.g.
3j
moves down 3 lines - 所有命令都可以以“range”为前缀,例如3j向下移动3行
-
i
to enter insert mode — text you type will appear in the file - 我要输入插入模式-你输入的文本将会出现在文件中
-
Esc or Ctrl
c
to exit insert mode and return to "normal" mode - 退出插入模式并返回到“正常”模式。
-
u
to undo - 你取消
-
Ctrl
r
to redo - Ctrlr重做
-
dd
,dw
,dl
to delete a line, word, or letter, respectively - 分别删除行、字或字母
-
cc
,cw
,cl
to change a line, word, or letter, respectively (same asdd
i
) - cc、cw、cl分别更改一行、单词或字母(与ddi相同)
-
yy
,yw
,yl
to copy ("yank") a line, word, or letter, respectively - yy, yw, yl to copy (yank) a line, word, letter, each other
-
p
orP
to paste after, or before current position, respectively - p或p分别粘贴在当前位置之后或之前
-
:w
Enter to save (write) a file - :保存(写)文件
-
:q!
Enter to quit without saving - :问!进入退出而不保存
-
:wq
Enter orZZ
to save and quit - :wqEnter或ZZ以保存和退出
If you edit text a lot, then switch to the Dvorak keyboard layout, learn to touch-type, and learn vim. Is it worth the effort? Yes.
如果你经常编辑文本,切换到Dvorak键盘布局,学习触摸类型,学习vim。这值得付出努力吗?是的。
ProTip™: Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:
ProTip™:不要害怕尝试“危险”命令,改写历史* - Git没有删除你的默认提交了90天;你可以在reflog中找到它们:
$ git reset @~3 # go back 3 commits$ git reflogc4f708b HEAD@{0}: reset: moving to @~32c52489 HEAD@{1}: commit: more changes4a5246d HEAD@{2}: commit: make important changese8571e4 HEAD@{3}: commit: make some changes... earlier commits ...$ git reset 2c52489... and you're back where you started
* Watch out for options like --hard
and --force
though — they can discard data.
* Also, don't rewrite history on any branches you're collaborating on.
*注意一些选项,比如“硬的”和“强制的”,它们可以丢弃数据。*此外,不要重写正在协作的任何分支的历史。
#16
159
I use the Git GUI as much as I can, and that gives you the option to amend the last commit:
我尽可能多地使用Git GUI,这给了您修改最后提交的选项:
Also, git rebase -i origin/master
is a nice mantra that will always present you with the commits you have done on top of master, and give you the option to amend, delete, reorder or squash. No need to get hold of that hash first.
另外,git rebase -i origin/masteris是一个很好的咒语,它会始终显示您在master之上所做的提交,并为您提供修改、删除、重新排序或挤压的选项。不需要先得到哈希。
#17
129
Wow, so there are a lot of ways to do this.
哇,有很多方法可以做到这一点。
Yet another way to do this is to delete the last commit, but keep its changes so that you won't lose your work. You can then do another commit with the corrected message. This would look something like this:
另一种方法是删除最后一次提交,但是保留它的更改,以免丢失工作。然后,您可以使用已更正的消息进行另一次提交。它看起来是这样的:
git reset --soft HEAD~1git commit -m 'New and corrected commit message'
I always do this if I forget to add a file or do a change.
如果我忘记添加文件或做更改,我总是这样做。
Remember to specify --soft
instead of --hard
, otherwise you lose that commit entirely.
记住要明确——软而不是硬,否则你会完全失去承诺。
#18
119
If you just want to edit the latest commit use:
如果您只想编辑最新的提交使用:
git commit --amend
or
或
git commit --amend -m 'one line message'
But if you want to edit several commits in a row you should use rebasing instead:
但是如果你想在一行中编辑多个提交,你应该使用rebase:
git rebase -i <hash of one commit before the wrong commit>
In a file like the one above write edit/e or one of the other option and hit save and exit.
在一个像上面这样的文件中写入edit/e或者另一个选项,点击save和exit。
Now you'll be at the first wrong commit. Make changes in the files, and they'll be automatically staged for you. Type
现在你将第一次犯错误。在文件中进行更改,它们将自动为您设置。类型
git commit --amend
save and exit that and type
保存并退出,然后输入
git rebase --continue
to move to next selection until finished with all your selections.
切换到下一个选择,直到完成所有的选择。
Note that these things change all your SHA hashes after that particular commit.
注意,这些东西会在特定的提交之后更改所有的SHA哈希。
#19
117
If you only want to change your last message you should use the --only
flag or its shortcut -o
with commit --amend
:
如果你只想改变你的最后一条信息,你应该使用-only标志或它的快捷方式-o与提交-修改:
git commit --amend -o -m "New commit message"
This ensures that you don't accidentally enhance your commit with staged stuff. Of course it's best to have a proper $EDITOR
configuration. Then you can leave the -m
option out, and git will pre-fill the commit message with the old one. In this way it can be easily edited.
这将确保您不会意外地使用舞台设置增强提交。当然,最好有一个合适的$EDITOR配置。然后您可以将-m选项保留下来,git将使用旧的提交消息预先填充提交消息。这样就可以很容易地编辑它。
#20
116
For anyone looking for a Windows/Mac GUI to help with editing older messages (i.e. not just the latest message), I'd recommend SourceTree. The steps to follow are below.
对于任何想要寻找Windows/Mac GUI来帮助编辑旧消息(即不只是最新消息)的人,我推荐SourceTree。接下来的步骤如下。
For commits that haven't yet been pushed to a remote:
对于尚未被推送到远程的提交:
- Make sure you've committed or stashed all current changes (i.e. so there are no files listed in the "File Status" tab) - it won't work otherwise.
- 确保您已经提交或隐藏了所有当前的更改(例如,在“文件状态”选项卡中没有列出文件)——否则它将无法工作。
- In the "Log / History" tab, right click on the entry with an adjoining line in the graph one below the commit(s) you wish to edit and select "Rebase children of <commit ref> interactively..."
-
在“Log / History”选项卡中,在您希望编辑和选择“Rebase子元素
交互式地… - Select the whole row for the commit message you wish to change (i.e. click on the "Message" column).
- 选择要更改的提交消息的整个行(即单击“message”列)。
- Click the "Edit Message" button.
- 点击“编辑消息”按钮。
- Edit the message as desired in the dialog that comes up and then click OK.
- 在出现的对话框中按需要编辑消息,然后单击OK。
- Repeat steps 3-4 if there are other commit messages to change.
- 如果有其他提交消息需要更改,请重复步骤3-4。
- Click OK: Rebasing will commence. If all is well, the output will end "Completed successfully".
- 点击确定:重置将开始。如果一切顺利,输出将“成功完成”。
...Or... for commits that have already been pushed:
…或…对于已经提交的提交:
Follow the steps in this answer, which are similar to above but require a further command to be run from the command line to force-push the branch - read it all and apply the necessary caution!
按照这个答案的步骤,这类似于上面的步骤,但是需要从命令行中运行一个命令来强制执行分支—读取它,并应用必要的警告!
#21
95
Update your last wrong commit message with new commit message in one line:
在一行中使用新的提交消息更新上一次错误的提交消息:
git commit --amend -m "your new commit message"
Or, try git reset like below:
或者,尝试如下所示的git重置:
# You can reset your head to n number of commit# NOT a good idea for changing last commit message# but you can get an idea to split commit into multiple commitsgit reset --soft HEAD^# it will reset you last commit. Now, you# can re-commit it with new commit message.
Using reset to split commits into smaller commits
git reset
can help you to break one commit into multiple commits too:
git重置可以帮助您将一个提交分解为多个提交:
# reset your head. I am resetting to last commits:git reset --soft HEAD^# (you can reset multiple commit by doing HEAD~2(no. of commits)# Now, reset your head for splitting it to multiple commitsgit reset HEAD# add and commit your files seperately to make multiple commits: e.ggit add app/git commit -m "add all files in app directory"git add config/git commit -m "add all files in config directory"
Here you have successfully broke your last commit into two commits.
在这里,您已经成功地将上一次提交分解为两个提交。
#22
78
On this question there are a lot of answers but none of them explains in super detail how to change older commit messages using VIM. I was stuck trying to do this myself, so here I'll write down in detail how I did this especially for people who have no experience in VIM!
关于这个问题,有很多答案,但是没有一个非常详细地解释如何使用VIM更改旧的提交消息。我一直在尝试自己做这件事,所以在这里我将详细地写下我是如何为那些没有VIM经验的人做这件事的!
I wanted to change my five latest commits that I already pushed to the server. This is quite 'dangerous' cause if someone else already pulled from this you can mess things up by changing the commit messages. However when you’re working on your own little branch and are sure no one pulled it you can change it like this:
我想更改我已经提交到服务器的五个最新提交。这是相当“危险”的,因为如果其他人已经从这里退出,您可以通过更改提交消息来把事情弄糟。然而,当你在自己的小树枝上工作时,你肯定没有人拉它,你可以这样改变它:
Let's say you want to change your five latest commits, then you type this in the terminal:
假设您想更改您的5个最新提交,然后在终端中输入:
git rebase -i HEAD~5
*Where 5 is the number of commit messages you want to change. (so if you want to change the 10th to last commit you type in 10)
git rebase -i HEAD~5*其中5是您希望更改的提交消息的数量。(所以如果你想把第10个改为最后一个提交,请输入10)
This command will get you into VIM there you can ‘edit’ your commit history.You’ll see your last 5 commits at the top like this:
这个命令将使您进入VIM,您可以在那里“编辑”您的提交历史记录。您将看到您的最后5个提交在顶部,像这样:
pick <commit hash> commit message
选择 <提交哈希> 提交消息
Instead of pick
you need to write reword
. You can do this in VIM by typing in i
, that makes you go in to INSERT-mode. (You see that you’re in insert mode by the word INSERT at the bottom) For the commits you want to change type in reword
instead of pick
而不是选择你需要重新写。你可以在VIM中输入i,这样就进入了插入模式。(您可以看到,在插入模式中,在底部的单词insert)对于提交,您希望在reword中更改类型,而不是选择
Then you need to save and quit this screen, you do that by first going in to ‘command-mode’ by pressing the esc button. (you can check that you’re in command-mode if the word INSERT at the bottom has disappeared) Then you can type in a command by typing :
, the command to save and quit is wq
. So if you type in :wq
you’re ont he right track.
然后你需要保存并退出这个屏幕,你需要通过按esc键进入“命令模式”。(如果底部的单词INSERT消失,您可以检查自己是否处于命令模式)然后您可以输入命令:,保存和退出的命令是wq。所以,如果你输入:wq,你就进入了正确的轨道。
Then VIM wil go over every commit message you want to reword, here you can actually change the commit messages. You’ll do this by going into INSERT-mode, changing the commit message, going into the command-mode and save and quit. Do this 5 times and you’re out of VIM!
然后VIM将检查您想要重新填入的每个提交消息,在这里您实际上可以更改提交消息。您可以进入插入模式、更改提交消息、进入命令模式并保存和退出。这样做5次,你就没有活力了!
Then, if you already pushed your wrong commits, you need to git push --force
to overwrite them. Remember that git push --force
is quite a dangerous thing to do, so make sure that no-one pulled from the server since you pushed your wrong commits!
然后,如果您已经提交了错误的提交,则需要git push—force来覆盖它们。请记住,git push——force是一种非常危险的操作,所以请确保在您提交错误之后,没有人从服务器上提取!
Now you have changed your commit messages!
现在您已经更改了提交消息!
(As you see I'm not that experienced in VIM so if I used wrong 'lingo' to explain what's happening, feel free to correct me!)
(正如你所看到的,我在VIM中没有那么丰富的经验,所以如果我用了错误的‘行话’来解释发生了什么,请随时纠正我!)
#23
72
I have added the alias of reci
, recm
for recommit (amend)
it, now I can do it with git recm
or git recm -m
.
我添加了reci的别名,recm用于推荐(修改)它,现在可以用git recm或者git recm -m来做了。
$ vim ~/.gitconfig[alias] ...... cm = commit reci = commit --amend recm = commit --amend ......
#24
69
You can use git-rebase-reword
您可以使用git-rebase-reword
It is designed to edit any commit (not just last) same way as commit --amend
它被设计成以与提交—修改相同的方式编辑任何提交(而不仅仅是最后一次)
$ git rebase-reword <commit-or-refname>
It is named after the action on rebase interactive to amend a commit: "reword". See this post and man -section interactive mode-
它以rebase interactive操作的名称命名,以修改提交:“reword”。请参阅这篇文章和人物部分的互动模式-
Examples:
例子:
$ git rebase-reword b68f560$ git rebase-reword HEAD^
#25
53
I realised that I had pushed a commit with a typo in it. In order to undo, I did the following:
我意识到我推了一个带有错字的承诺。为了撤销,我做了以下事情:
git commit --amend -m "T-1000, advanced prototype"git push --force
Warning: force pushing your changes will overwrite the remote branch with your local one. Make sure that you aren't going to be overwriting anything that you want to keep. Also be cautious about force pushing an amended (rewritten) commit if anyone else shares the branch with you, because they'll need to rewrite their own history if they have the old copy of the commit that you've just rewritten.
警告:强制推进您的更改将覆盖远程分支和本地分支。确保你不会覆盖任何你想保留的东西。如果其他人与您共享分支,也要谨慎地避免强制执行修改(重写)的提交,因为如果他们有您刚刚重写的提交的旧副本,他们将需要重写自己的历史。
#26
47
I like to use the following:
我喜欢使用以下内容:
git status
- git状态
git add --all
- git添加——所有
git commit -am "message goes here about the change"
- git提交-am“关于变更的消息”
git pull <origin master>
- git拉 <起源大师>
git push <origin master>
- git push <起源大师>
#27
41
If you have not pushed the code to your remote branch (GitHub/Bitbucket) you can change the commit message on the command line as below.
如果没有将代码推到远程分支(GitHub/Bitbucket),可以在命令行中更改提交消息,如下所示。
git commit --amend -m "Your new message"
If you're working on a specific branch do this:
如果你在一个特定的分支机构工作,请这样做:
git commit --amend -m "BRANCH-NAME: new message"
If you've already pushed the code with the wrong message, and you need to be careful when changing the message. That is, after you change the commit message and try pushing it again, you end up with having issues. To make it smooth, follow these steps.
如果您已经用错误的消息推送了代码,并且在更改消息时需要小心。也就是说,在您更改提交消息并尝试再次推动它之后,您最终会遇到问题。要使它平滑,请遵循以下步骤。
Please read my entire answer before doing it.
在做之前,请阅读我的全部答案。
git commit --amend -m "BRANCH-NAME : your new message"git push -f origin BRANCH-NAME # Not a best practice. Read below why?
Important note: When you use the force push directly you might end up with code issues that other developers are working on the same branch. So to avoid those conflicts, you need to pull the code from your branch before making the force push:
重要注意:当您直接使用force push时,您可能会遇到其他开发人员正在处理同一分支的代码问题。因此,为了避免这些冲突,您需要从您的分支中提取代码,然后再进行force push:
git commit --amend -m "BRANCH-NAME : your new message" git pull origin BRANCH-NAME git push -f origin BRANCH-NAME
This is the best practice when changing the commit message, if it was already pushed.
这是更改提交消息时的最佳实践,如果它已经被推送。
#1
14661
Amending the most recent commit message
git commit --amend
will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:
将打开您的编辑器,允许您更改最近提交的提交消息。此外,您可以在命令行中直接设置提交消息:
git commit --amend -m "New commit message"
…however, this can make multi-line commit messages or small corrections more cumbersome to enter.
然而,这可能会使多行提交消息或小的修改更难以输入。
Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)
确保在进行此操作之前没有任何工作拷贝更改,否则它们也会被提交。(未分阶段的更改不会被提交。)
Changing the message of a commit that you've already pushed to your remote branch
If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with:
如果您已经将提交推到远程分支,那么需要强制执行提交:
git push <remote> <branch> --force# Orgit push <remote> <branch> -f
Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.
警告:强制推送将覆盖您本地的远程分支。如果在您的本地分支中没有您的远程分支,那么您将丢失这些提交。
Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.
警告:对于已经与他人共享的提交,要小心修改。修改提交本质上重写了它们,使它们具有不同的SHA id,如果其他人拥有您重写的旧提交的副本,则会产生问题。任何拥有旧提交副本的人都需要将他们的工作与您新重写的提交同步,这有时会很困难,所以在尝试重写共享提交历史记录时,请确保与其他人协调,或者只是避免完全重写共享提交。
Use interactive rebase
Another option is to use interactive rebase.
This allows you to edit any message you want to update even if it's not the latest message.
另一种选择是使用交互式重基。这允许您编辑任何您想要更新的消息,即使它不是最新的消息。
In order to do a git squash, follow these steps:
为了做一个git壁球,请遵循以下步骤:
// X is the number of commits to the last commit you want to be able to editgit rebase -i HEAD~X
Once you squash your commits - choose the e/r
for editing the message
一旦你压缩提交-选择e/r编辑消息
Important note about Interactive rebase
When you use the git rebase -i HEAD~X
there can be more than X
commits. Git will "collect" all the commits in the last X
commits and if there was a merge somewhere in between that range you will see all the commits as well so the outcome will be X+.
当您使用git rebase -i HEAD~X时,可能会有更多的提交。Git将“收集”上一个X提交中的所有提交,如果在这个范围内有合并,您将看到所有的提交,因此结果是X+。
Good tip:
If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere
and let git resolve those conflicts automatically for you.
如果您需要对多个分支进行此操作,并且在修改内容时可能会遇到冲突,那么可以设置git rerere,并让git自动为您解决这些冲突。
Documentation
-
git-commit(1)手册页
-
git-rebase(1)手册页
-
git-push(1)手册页
#2
2402
git commit --amend -m "your new message"
#3
2281
If the commit you want to fix isn’t the most recent one:
如果你想修复的承诺不是最近的:
-
git rebase --interactive $parent_of_flawed_commit
git变基——交互式parent_of_flawed_commit美元
If you want to fix several flawed commits, pass the parent of the oldest one of them.
如果您想修复几个有缺陷的提交,请传递其中最老的一个的父类。
-
An editor will come up, with a list of all commits since the one you gave.
将出现一个编辑器,其中包含自您提供的提交以来的所有提交。
- Change
pick
toreword
(or on old versions of Git, toedit
) in front of any commits you want to fix. - 在您希望修复的任何提交之前,将pick更改为reword(或老版本的Git)。
- Once you save, Git will replay the listed commits.
- 保存之后,Git将重播列出的提交。
- Change
-
For each commit you want to reword, Git will drop you back into your editor. For each commit you want to edit, Git drops you into the shell. If you’re in the shell:
对于您想要重述的每个提交,Git都将把您送回编辑器。对于要编辑的每个提交,Git都会将您放入shell中。如果你在壳层:
- Change the commit in any way you like.
- 以您喜欢的任何方式更改提交。
git commit --amend
- git commit -修改
git rebase --continue
- git变基——继续
Most of this sequence will be explained to you by the output of the various commands as you go. It’s very easy, you don’t need to memorise it – just remember that git rebase --interactive
lets you correct commits no matter how long ago they were.
这个序列的大部分将通过您执行的各种命令的输出进行解释。这非常简单,您不需要记住它—只需记住git rebase—interactive允许您纠正提交,无论它们在多长时间以前。
Note that you will not want to change commits that you have already pushed. Or maybe you do, but in that case you will have to take great care to communicate with everyone who may have pulled your commits and done work on top of them. How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?
请注意,您将不希望更改已提交的提交。也许你会这么做,但在这种情况下,你必须非常小心地与每个可能已经完成你的任务并在其之上完成工作的人进行沟通。当有人将重基或重设推到已发布的分支后,我如何恢复/重新同步?
#4
744
To amend the previous commit, make the changes you want and stage those changes, and then run
要修改之前的提交,请进行所需的更改并执行这些更改,然后运行
git commit --amend
This will open a file in your text editor representing your new commit message. It starts out populated with the text from your old commit message. Change the commit message as you want, then save the file and quit your editor to finish.
这将在表示新的提交消息的文本编辑器中打开一个文件。它首先填充来自旧提交消息的文本。根据需要更改提交消息,然后保存文件并退出编辑器以完成。
To amend the previous commit and keep the same log message, run
要修改之前的提交并保持相同的日志消息,请运行
git commit --amend -C HEAD
To fix the previous commit by removing it entirely, run
要完全删除之前的提交,请运行
git reset --hard HEAD^
If you want to edit more than one commit message, run
如果希望编辑多个提交消息,请运行
git rebase -i HEAD~commit_count
(Replace commit_count with number of commits that you want to edit.) This command launches your editor. Mark the first commit (the one that you want to change) as “edit” instead of “pick”, then save and exit your editor. Make the change you want to commit and then run
(用要编辑的提交数替换commit_count。)此命令启动编辑器。将第一个提交(您希望更改的提交)标记为“edit”而不是“pick”,然后保存并退出编辑器。进行您想要提交的更改,然后运行
git commit --amendgit rebase --continue
Note: You can "Make the change you want" also from the editor opened by git commit --amend
注意:您还可以从git提交—修改打开的编辑器中“进行您想要的更改”
#5
378
As already mentioned, git commit --amend
is the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be
如前所述,git提交——修改是覆盖最后一次提交的方法。注意:如果您想要覆盖文件,命令将是。
git commit -a --amend -m "My new commit message"
#6
341
You also can use git filter-branch
for that.
您还可以使用git过滤-分支。
git filter-branch -f --msg-filter "sed 's/errror/error/'" $flawed_commit..HEAD
It's not as easy as a trivial git commit --amend
, but it's especially useful, if you already have some merges after your erroneous commit message.
这并不是简单的git提交——修改,但是它特别有用,如果您在错误的提交消息之后已经有一些合并。
Note that this will try to rewrite EVERY commit between HEAD
and the flawed commit, so you should choose your msg-filter
command very wise ;-)
注意,这将尝试重写HEAD和有缺陷提交之间的每个提交,因此您应该非常明智地选择您的msg-filter命令;
#7
303
I prefer this way.
我喜欢这种方式。
git commit --amend -c <commit ID>
Otherwise, there will be a new commit with a new commit ID
否则,将会有一个带有新的提交ID的新提交
#8
300
If you are using the Git GUI tool, there is a button named amend last commit. Click on that button and then it will display your last commit files and message. Just edit that message and you can commit it with new commit message.
如果您正在使用Git GUI工具,则有一个名为amend last commit的按钮。单击该按钮,然后它将显示您的最后一个提交文件和消息。只需编辑该消息,就可以使用新的提交消息提交它。
Or use this command from a console/terminal:
或从控制台/终端使用此命令:
git commit -a --amend -m "My new commit message"
#9
276
You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run
您可以使用Git rebase。例如,如果您想修改回发bbc643cd,请运行
$ git rebase bbc643cd^ --interactive
In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with
在默认的编辑器中,将“选择”修改为要修改提交的行中的“编辑”。做出你的改变,然后和他们一起。
$ git add <filepattern>
Now you can use
现在你可以使用
$ git commit --amend
to modify the commit, and after that
修改提交,然后
$ git rebase --continue
to return back to the previous head commit.
返回到先前的head提交。
#10
267
-
If you only want to modify your last commit message, then do:
如果您只想修改您的最后提交消息,请执行以下操作:
git commit --amend
That will drop you into your text exitor and let you change the last commit message.
这会将您放入文本exitor中,并允许您更改最后的提交消息。
-
If you want to change the last 3 commit messages, or any of the commit messages up to that point, supply
HEAD~3
to thegit rebase -i
command:如果您想更改最后的3条提交消息,或任何提交消息,请向git rebase -i命令提供HEAD~3:
git rebase -i HEAD~3
#11
248
If you have to change an old commit message over multiple branches (i.e., the commit with the erroneous message is present in multiple branches) you might want to use:
如果你必须改变旧的提交消息(即多个分支机构。,提交错误信息出现在多个分支)您可能想要使用:
git filter-branch -f --msg-filter \'sed "s/<old message>/<new message>/g"' -- --all
Git will create a temporary directory for rewriting and additionally backup old references in refs/original/
.
Git将创建一个临时目录,用于重写并在refs/original/中备份旧引用。
-
-f
will enforce the execution of the operation. This is necessary if the the temporary directory is already present or if there are already references stored underrefs/original
. If that is not the case, you can drop this flag.-f将强制执行操作。如果临时目录已经存在,或者已经有引用存储在refs/original下,则需要这样做。如果不是这样,您可以删除这个标志。
-
--
separates filter-branch options from revision options.——分离filter-branch选项修改选项。
-
--all
will make sure, that all branches and tags are rewritten.——所有这些都将确保所有分支和标记都被重写。
Due to the backup of your old references, you can easily go back to the state before executing the command.
由于备份了旧的引用,您可以很容易地在执行命令之前返回到状态。
Say, you want to recover your master and access it in branch old_master
:
比方说,你想要恢复你的master并在分支old_master中访问它:
git checkout -b old_master refs/original/refs/heads/master
#12
211
Use
使用
git commit --amend
To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use git commit --amend
.
要详细了解它,最好的文章是4。Git重写历史。它还讨论了何时不使用git提交——修改。
#13
188
Amend
You have a couple of options here. You can do
这里有几个选项。你可以做
git commit --amend
as long as it's your last commit.
只要这是你最后的承诺。
Interactive rebase
Otherwise if it's not your last commit you can do an interactive rebase,
否则,如果这不是你最后一次提交你可以做一个互动的重新基础,
git rebase -i [branched_from] [hash before commit]
Then inside the interactive rebase you simply add edit to that commit. When it comes up do a git commit --amend
and modify the commit message. If you want to roll back before that commit point you could also use git reflog
and just delete that commit. Then you just do a git commit
again.
然后在交互式的rebase中,您只需添加编辑到该提交。当它出现时,执行git提交——修改和修改提交消息。如果您想在提交点之前回滚,您还可以使用git reflog并删除该提交。然后再做一次git提交。
#14
176
If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:
如果您正在使用Git GUI,您可以修改最后一次提交,但没有使用:
Commit/Amend Last Commit
#15
162
If it's your last commit, just amend the commit:
如果是你最后一次提交,只需修改提交:
git commit --amend -o -m "New commit message"
(using the -o
(--only
) flag to make sure you change only the commit message)
(使用-o (only)标志确保只更改提交消息)
If it's a buried commit, use the awesome interactive rebase:
如果是隐藏的承诺,使用令人敬畏的交互基础:
git rebase -i @~9 # Show the last 9 commits in a text editor
Find the commit you want, change pick
to r
(reword
), and save and close the file. Done!
找到您想要的提交,将选择改为r (reword),并保存并关闭文件。完成了!
Miniature vim tutorial (or, how to rebase with only 8 keystrokes 3j
cw
r
EscZZ
):
迷你vim教程(或者,如何仅用8个按键重设3jcwrEscZZ):
- Run
vimtutor
if you have time - 如果有时间,请运行vimtutor
-
h
j
k
l
correspond to movement keys ←↓↑→ - hjkl对应移动键←↓↑→
- All commands can be prefixed with a "range", e.g.
3j
moves down 3 lines - 所有命令都可以以“range”为前缀,例如3j向下移动3行
-
i
to enter insert mode — text you type will appear in the file - 我要输入插入模式-你输入的文本将会出现在文件中
-
Esc or Ctrl
c
to exit insert mode and return to "normal" mode - 退出插入模式并返回到“正常”模式。
-
u
to undo - 你取消
-
Ctrl
r
to redo - Ctrlr重做
-
dd
,dw
,dl
to delete a line, word, or letter, respectively - 分别删除行、字或字母
-
cc
,cw
,cl
to change a line, word, or letter, respectively (same asdd
i
) - cc、cw、cl分别更改一行、单词或字母(与ddi相同)
-
yy
,yw
,yl
to copy ("yank") a line, word, or letter, respectively - yy, yw, yl to copy (yank) a line, word, letter, each other
-
p
orP
to paste after, or before current position, respectively - p或p分别粘贴在当前位置之后或之前
-
:w
Enter to save (write) a file - :保存(写)文件
-
:q!
Enter to quit without saving - :问!进入退出而不保存
-
:wq
Enter orZZ
to save and quit - :wqEnter或ZZ以保存和退出
If you edit text a lot, then switch to the Dvorak keyboard layout, learn to touch-type, and learn vim. Is it worth the effort? Yes.
如果你经常编辑文本,切换到Dvorak键盘布局,学习触摸类型,学习vim。这值得付出努力吗?是的。
ProTip™: Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:
ProTip™:不要害怕尝试“危险”命令,改写历史* - Git没有删除你的默认提交了90天;你可以在reflog中找到它们:
$ git reset @~3 # go back 3 commits$ git reflogc4f708b HEAD@{0}: reset: moving to @~32c52489 HEAD@{1}: commit: more changes4a5246d HEAD@{2}: commit: make important changese8571e4 HEAD@{3}: commit: make some changes... earlier commits ...$ git reset 2c52489... and you're back where you started
* Watch out for options like --hard
and --force
though — they can discard data.
* Also, don't rewrite history on any branches you're collaborating on.
*注意一些选项,比如“硬的”和“强制的”,它们可以丢弃数据。*此外,不要重写正在协作的任何分支的历史。
#16
159
I use the Git GUI as much as I can, and that gives you the option to amend the last commit:
我尽可能多地使用Git GUI,这给了您修改最后提交的选项:
Also, git rebase -i origin/master
is a nice mantra that will always present you with the commits you have done on top of master, and give you the option to amend, delete, reorder or squash. No need to get hold of that hash first.
另外,git rebase -i origin/masteris是一个很好的咒语,它会始终显示您在master之上所做的提交,并为您提供修改、删除、重新排序或挤压的选项。不需要先得到哈希。
#17
129
Wow, so there are a lot of ways to do this.
哇,有很多方法可以做到这一点。
Yet another way to do this is to delete the last commit, but keep its changes so that you won't lose your work. You can then do another commit with the corrected message. This would look something like this:
另一种方法是删除最后一次提交,但是保留它的更改,以免丢失工作。然后,您可以使用已更正的消息进行另一次提交。它看起来是这样的:
git reset --soft HEAD~1git commit -m 'New and corrected commit message'
I always do this if I forget to add a file or do a change.
如果我忘记添加文件或做更改,我总是这样做。
Remember to specify --soft
instead of --hard
, otherwise you lose that commit entirely.
记住要明确——软而不是硬,否则你会完全失去承诺。
#18
119
If you just want to edit the latest commit use:
如果您只想编辑最新的提交使用:
git commit --amend
or
或
git commit --amend -m 'one line message'
But if you want to edit several commits in a row you should use rebasing instead:
但是如果你想在一行中编辑多个提交,你应该使用rebase:
git rebase -i <hash of one commit before the wrong commit>
In a file like the one above write edit/e or one of the other option and hit save and exit.
在一个像上面这样的文件中写入edit/e或者另一个选项,点击save和exit。
Now you'll be at the first wrong commit. Make changes in the files, and they'll be automatically staged for you. Type
现在你将第一次犯错误。在文件中进行更改,它们将自动为您设置。类型
git commit --amend
save and exit that and type
保存并退出,然后输入
git rebase --continue
to move to next selection until finished with all your selections.
切换到下一个选择,直到完成所有的选择。
Note that these things change all your SHA hashes after that particular commit.
注意,这些东西会在特定的提交之后更改所有的SHA哈希。
#19
117
If you only want to change your last message you should use the --only
flag or its shortcut -o
with commit --amend
:
如果你只想改变你的最后一条信息,你应该使用-only标志或它的快捷方式-o与提交-修改:
git commit --amend -o -m "New commit message"
This ensures that you don't accidentally enhance your commit with staged stuff. Of course it's best to have a proper $EDITOR
configuration. Then you can leave the -m
option out, and git will pre-fill the commit message with the old one. In this way it can be easily edited.
这将确保您不会意外地使用舞台设置增强提交。当然,最好有一个合适的$EDITOR配置。然后您可以将-m选项保留下来,git将使用旧的提交消息预先填充提交消息。这样就可以很容易地编辑它。
#20
116
For anyone looking for a Windows/Mac GUI to help with editing older messages (i.e. not just the latest message), I'd recommend SourceTree. The steps to follow are below.
对于任何想要寻找Windows/Mac GUI来帮助编辑旧消息(即不只是最新消息)的人,我推荐SourceTree。接下来的步骤如下。
For commits that haven't yet been pushed to a remote:
对于尚未被推送到远程的提交:
- Make sure you've committed or stashed all current changes (i.e. so there are no files listed in the "File Status" tab) - it won't work otherwise.
- 确保您已经提交或隐藏了所有当前的更改(例如,在“文件状态”选项卡中没有列出文件)——否则它将无法工作。
- In the "Log / History" tab, right click on the entry with an adjoining line in the graph one below the commit(s) you wish to edit and select "Rebase children of <commit ref> interactively..."
-
在“Log / History”选项卡中,在您希望编辑和选择“Rebase子元素
交互式地… - Select the whole row for the commit message you wish to change (i.e. click on the "Message" column).
- 选择要更改的提交消息的整个行(即单击“message”列)。
- Click the "Edit Message" button.
- 点击“编辑消息”按钮。
- Edit the message as desired in the dialog that comes up and then click OK.
- 在出现的对话框中按需要编辑消息,然后单击OK。
- Repeat steps 3-4 if there are other commit messages to change.
- 如果有其他提交消息需要更改,请重复步骤3-4。
- Click OK: Rebasing will commence. If all is well, the output will end "Completed successfully".
- 点击确定:重置将开始。如果一切顺利,输出将“成功完成”。
...Or... for commits that have already been pushed:
…或…对于已经提交的提交:
Follow the steps in this answer, which are similar to above but require a further command to be run from the command line to force-push the branch - read it all and apply the necessary caution!
按照这个答案的步骤,这类似于上面的步骤,但是需要从命令行中运行一个命令来强制执行分支—读取它,并应用必要的警告!
#21
95
Update your last wrong commit message with new commit message in one line:
在一行中使用新的提交消息更新上一次错误的提交消息:
git commit --amend -m "your new commit message"
Or, try git reset like below:
或者,尝试如下所示的git重置:
# You can reset your head to n number of commit# NOT a good idea for changing last commit message# but you can get an idea to split commit into multiple commitsgit reset --soft HEAD^# it will reset you last commit. Now, you# can re-commit it with new commit message.
Using reset to split commits into smaller commits
git reset
can help you to break one commit into multiple commits too:
git重置可以帮助您将一个提交分解为多个提交:
# reset your head. I am resetting to last commits:git reset --soft HEAD^# (you can reset multiple commit by doing HEAD~2(no. of commits)# Now, reset your head for splitting it to multiple commitsgit reset HEAD# add and commit your files seperately to make multiple commits: e.ggit add app/git commit -m "add all files in app directory"git add config/git commit -m "add all files in config directory"
Here you have successfully broke your last commit into two commits.
在这里,您已经成功地将上一次提交分解为两个提交。
#22
78
On this question there are a lot of answers but none of them explains in super detail how to change older commit messages using VIM. I was stuck trying to do this myself, so here I'll write down in detail how I did this especially for people who have no experience in VIM!
关于这个问题,有很多答案,但是没有一个非常详细地解释如何使用VIM更改旧的提交消息。我一直在尝试自己做这件事,所以在这里我将详细地写下我是如何为那些没有VIM经验的人做这件事的!
I wanted to change my five latest commits that I already pushed to the server. This is quite 'dangerous' cause if someone else already pulled from this you can mess things up by changing the commit messages. However when you’re working on your own little branch and are sure no one pulled it you can change it like this:
我想更改我已经提交到服务器的五个最新提交。这是相当“危险”的,因为如果其他人已经从这里退出,您可以通过更改提交消息来把事情弄糟。然而,当你在自己的小树枝上工作时,你肯定没有人拉它,你可以这样改变它:
Let's say you want to change your five latest commits, then you type this in the terminal:
假设您想更改您的5个最新提交,然后在终端中输入:
git rebase -i HEAD~5
*Where 5 is the number of commit messages you want to change. (so if you want to change the 10th to last commit you type in 10)
git rebase -i HEAD~5*其中5是您希望更改的提交消息的数量。(所以如果你想把第10个改为最后一个提交,请输入10)
This command will get you into VIM there you can ‘edit’ your commit history.You’ll see your last 5 commits at the top like this:
这个命令将使您进入VIM,您可以在那里“编辑”您的提交历史记录。您将看到您的最后5个提交在顶部,像这样:
pick <commit hash> commit message
选择 <提交哈希> 提交消息
Instead of pick
you need to write reword
. You can do this in VIM by typing in i
, that makes you go in to INSERT-mode. (You see that you’re in insert mode by the word INSERT at the bottom) For the commits you want to change type in reword
instead of pick
而不是选择你需要重新写。你可以在VIM中输入i,这样就进入了插入模式。(您可以看到,在插入模式中,在底部的单词insert)对于提交,您希望在reword中更改类型,而不是选择
Then you need to save and quit this screen, you do that by first going in to ‘command-mode’ by pressing the esc button. (you can check that you’re in command-mode if the word INSERT at the bottom has disappeared) Then you can type in a command by typing :
, the command to save and quit is wq
. So if you type in :wq
you’re ont he right track.
然后你需要保存并退出这个屏幕,你需要通过按esc键进入“命令模式”。(如果底部的单词INSERT消失,您可以检查自己是否处于命令模式)然后您可以输入命令:,保存和退出的命令是wq。所以,如果你输入:wq,你就进入了正确的轨道。
Then VIM wil go over every commit message you want to reword, here you can actually change the commit messages. You’ll do this by going into INSERT-mode, changing the commit message, going into the command-mode and save and quit. Do this 5 times and you’re out of VIM!
然后VIM将检查您想要重新填入的每个提交消息,在这里您实际上可以更改提交消息。您可以进入插入模式、更改提交消息、进入命令模式并保存和退出。这样做5次,你就没有活力了!
Then, if you already pushed your wrong commits, you need to git push --force
to overwrite them. Remember that git push --force
is quite a dangerous thing to do, so make sure that no-one pulled from the server since you pushed your wrong commits!
然后,如果您已经提交了错误的提交,则需要git push—force来覆盖它们。请记住,git push——force是一种非常危险的操作,所以请确保在您提交错误之后,没有人从服务器上提取!
Now you have changed your commit messages!
现在您已经更改了提交消息!
(As you see I'm not that experienced in VIM so if I used wrong 'lingo' to explain what's happening, feel free to correct me!)
(正如你所看到的,我在VIM中没有那么丰富的经验,所以如果我用了错误的‘行话’来解释发生了什么,请随时纠正我!)
#23
72
I have added the alias of reci
, recm
for recommit (amend)
it, now I can do it with git recm
or git recm -m
.
我添加了reci的别名,recm用于推荐(修改)它,现在可以用git recm或者git recm -m来做了。
$ vim ~/.gitconfig[alias] ...... cm = commit reci = commit --amend recm = commit --amend ......
#24
69
You can use git-rebase-reword
您可以使用git-rebase-reword
It is designed to edit any commit (not just last) same way as commit --amend
它被设计成以与提交—修改相同的方式编辑任何提交(而不仅仅是最后一次)
$ git rebase-reword <commit-or-refname>
It is named after the action on rebase interactive to amend a commit: "reword". See this post and man -section interactive mode-
它以rebase interactive操作的名称命名,以修改提交:“reword”。请参阅这篇文章和人物部分的互动模式-
Examples:
例子:
$ git rebase-reword b68f560$ git rebase-reword HEAD^
#25
53
I realised that I had pushed a commit with a typo in it. In order to undo, I did the following:
我意识到我推了一个带有错字的承诺。为了撤销,我做了以下事情:
git commit --amend -m "T-1000, advanced prototype"git push --force
Warning: force pushing your changes will overwrite the remote branch with your local one. Make sure that you aren't going to be overwriting anything that you want to keep. Also be cautious about force pushing an amended (rewritten) commit if anyone else shares the branch with you, because they'll need to rewrite their own history if they have the old copy of the commit that you've just rewritten.
警告:强制推进您的更改将覆盖远程分支和本地分支。确保你不会覆盖任何你想保留的东西。如果其他人与您共享分支,也要谨慎地避免强制执行修改(重写)的提交,因为如果他们有您刚刚重写的提交的旧副本,他们将需要重写自己的历史。
#26
47
I like to use the following:
我喜欢使用以下内容:
git status
- git状态
git add --all
- git添加——所有
git commit -am "message goes here about the change"
- git提交-am“关于变更的消息”
git pull <origin master>
- git拉 <起源大师>
git push <origin master>
- git push <起源大师>
#27
41
If you have not pushed the code to your remote branch (GitHub/Bitbucket) you can change the commit message on the command line as below.
如果没有将代码推到远程分支(GitHub/Bitbucket),可以在命令行中更改提交消息,如下所示。
git commit --amend -m "Your new message"
If you're working on a specific branch do this:
如果你在一个特定的分支机构工作,请这样做:
git commit --amend -m "BRANCH-NAME: new message"
If you've already pushed the code with the wrong message, and you need to be careful when changing the message. That is, after you change the commit message and try pushing it again, you end up with having issues. To make it smooth, follow these steps.
如果您已经用错误的消息推送了代码,并且在更改消息时需要小心。也就是说,在您更改提交消息并尝试再次推动它之后,您最终会遇到问题。要使它平滑,请遵循以下步骤。
Please read my entire answer before doing it.
在做之前,请阅读我的全部答案。
git commit --amend -m "BRANCH-NAME : your new message"git push -f origin BRANCH-NAME # Not a best practice. Read below why?
Important note: When you use the force push directly you might end up with code issues that other developers are working on the same branch. So to avoid those conflicts, you need to pull the code from your branch before making the force push:
重要注意:当您直接使用force push时,您可能会遇到其他开发人员正在处理同一分支的代码问题。因此,为了避免这些冲突,您需要从您的分支中提取代码,然后再进行force push:
git commit --amend -m "BRANCH-NAME : your new message" git pull origin BRANCH-NAME git push -f origin BRANCH-NAME
This is the best practice when changing the commit message, if it was already pushed.
这是更改提交消息时的最佳实践,如果它已经被推送。