git commit --amend

Git commit –amend

Definition and usage

Definition and usage (定义和用法)

While working with Git, you can often forget to format the commit or to stage a file. The git commit – amend command is the easiest way of correcting such mistakes. It is used to edit the latest commits. Instead of creating a completely new commit, you can run this command for combining staged changes with the previous commit. Besides, this command can modify the previous commit message without changing its snapshot. Take into account, that git commit – amend replaces the current commit entirely. We are going to see some ways of usage of git commit – amend.

Changing the most recent Git commit message

Changing the most recent Git commit message (更改最新的Git提交消息)

If you have committed and made a mistake in the commit log message, you can execute git commit – amend for modifying the log message of the previous commit without changing its snapshot. You can pass in a new message from the command line without receiving a prompt for opening an editor by using -m option.

git commit --amend -m "an updated commit message"

Changing committed files

Changing committed files (更改提交的文件)

Let’s imagine that you have modified some files that you want to commit in a singular snapshot, but you have forgotten to add one of the files the first time around. You can solve this problem, if you stage the other file and commit by using the –amend flag. Add –no-edit flag to modify your commit without changing the commit message. As a result, the wrong commit will be replaced by the right one, and it will look like the following example:

# Modify project.py and text.py git add project.py git commit 
# You forgot to add the changes from text.py git add text.py 
git commit --amend --no-edit

The inadmissibility of amending public commits

In fact, amended commits are new ones and after amending a commit, the previous commit will not be removed from the current branch. That’s why you should never amend a commit which is being developed by other team members because it will cause a lot of problems for collaboration. So you notice that the consequences are the same as in the case of resetting a public snapshot. (实际上,修改的提交是新的提交,在修改提交后,不会从当前分支中删除以前的提交。这就是为什么你永远不应该修改由其他团队成员开发的承诺,因为它会给协作带来很多问题。因此,您会注意到结果与重置公共快照的情况相同。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部