How to modify existing, unpushed commits?

2018-12-31 01:59发布

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条回答
心情的温度
2楼-- · 2018-12-31 02:09

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/.

  • -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 under refs/original. If that is not the case, you can drop this flag.

  • -- separates filter-branch options from revision options.

  • --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:

git checkout -b old_master refs/original/refs/heads/master
查看更多
余生无你
3楼-- · 2018-12-31 02:10

You also can use git filter-branch for that.

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.

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 ;-)

查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 02:12
git commit --amend -m "your new message"
查看更多
笑指拈花
5楼-- · 2018-12-31 02:12

If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:

Commit/Amend Last Commit
查看更多
公子世无双
6楼-- · 2018-12-31 02:13
  1. 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.

  2. If you want to change the last 3 commit messages, or any of the commit messages up to that point, supply HEAD~3 to the git rebase -i command:

    git rebase -i HEAD~3
    
查看更多
妖精总统
7楼-- · 2018-12-31 02:16

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:

git rebase -i <hash of one commit before the wrong commit>

git rebase editing

In a file like the one above write edit/e or one of the other option and hit save and 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.

查看更多
登录 后发表回答