Saving your time with git --autosquash
In this post a simple but quite common scenario. You have just committed something to your current branch and then you realised that you forgot something. For this example let's assume you've just created a fresh git init and you have just made your Initial Commit. The thing you forgot is README.md file (just as an example). Now you want to fix the thing up: you want adding the README.md to belong to the Initial Commit.
$ git init $ echo "FileA" > FileA.txt $ git add --all . $ git commit -am 'Intial commit.' [master (root-commit) 1c1a88d] Intial commit. 1 file changed, 1 insertion(+) create mode 100644 FileA.txt $ git log --oneline 1c1a88d Intial commit. $ echo "## git --autosquash is great" > README.md $ git add README.md $ git commit --fixup HEAD [master deaf9ef] fixup! Intial commit. 1 file changed, 1 insertion(+) create mode 100644 README.md $ git log --oneline deaf9ef fixup! Intial commit. 1c1a88d Intial commit. $ git rebase -i --autosquash --root master
This will open the file looking like this:
pick 1c1a88d Intial commit. fixup deaf9ef fixup! Intial commit.
Now you just hit cmd+W (Sublime Text2) to save the file and you are done! This should be the output:
[detached HEAD cb87174] Intial commit. 2 files changed, 2 insertions(+) create mode 100644 FileA.txt create mode 100644 README.md Successfully rebased and updated refs/heads/master. $ git log --oneline cb87174 Intial commit.
(Note this is now a completely new commit - the history has been rewritten. This is why you have to pay special attention if you do this on a branch that you already share with other folks.)
I own you one extra explanation here: I am using --root master just because I am on the master branch and there is nothing before Initial commit. Normally I could use something like HEAD~2 to point to the commit before the commit I want to fix the next commit up to. Finally, it is worth noticing that there is a --squash equivalent of the --fixup commit option.












