I often do this:
git add -u
git commit
...discover mistake, fix it...
git add -u
git commit -m "Fix typo"
Is there an easy way to squash the second commit back into the first? Sometimes I can do it with git rebase -i
, but sometimes I just get a message about being up to date.
Note I'm only really talking about situations where I haven't pushed the first commit. Basically I want to use the first log message, but with the combined effect of the two commits. Can I use git commit --amend
for such things?
This related question kind of dealt with this: git: squash/fixup earlier commit
The best answer is a year old though, and I wonder if things have improved?
This is exactly what git commit --amend
is for. Given your example, all you need to do is add the --amend
flag to your second commit:
git add -u
git commit
...discover mistake, fix it...
git add -u
git commit --amend
You can do this visually in git gui
also (there is a radio-box to toggle between "new commit" and "amend last commit")
The best I've found is this article that talks about the autosquash feature introduced in git 1.7.
Yes, you can just amend the previous commit.
git add typointhisfile
git commit -vm
OR if you've already created 2 commits - you can make them one with:
git rebase -i HEAD~2
Just squash one of them into the other.
You're better off (IMO) though, preventing the problem in the first place. And for that I suggest always doing verbose commits:
git commit -v
Before writing the commit message - skim the diff of the changes you are going to commit, and just close your editor if you see something you need to correct. Closing without entering a commit message will abort the commit.
Make it a habit and the need to commit typo-corrections will all but disappear.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.