Git: Oops. I Forgot To Add Those New Files Before Committing

I do this pretty regularly… working away, adding files, finish something up and commit. Only after I commit do I realize that I forgot to add the new files to git.

image

This always frustrated me with subversion because it meant I would have to make yet another commit and put in some silly commit message like “forgot to add these files to the previous commit” and I would often repeat the same commit message as the previous commit along with the note about forgotten files. Anyone that needed to merge my changes anywhere would have to some how know that they need to read the commit messages and figure out that this second commit was supposed to be part of the first commit, so they have to bring it along for the merge.

 

Git Commit –amend To The Rescue!

With git, you can solve this problem easily without having to create another commit in the repository and without having to re-type any of your commit messages. Stage the file that you forgot then when you do your commit, provide the “–amend” and “-C” options.

   1: git commit --amend –C HEAD

The –amend option tells git that you want to add these changes to the previous commit… that you want to amend the previous commit with these changes.

The –C option (or –c option… slightly different semantics, but generally the same) requires you to specify what previous commit’s message you want to reuse. If you don’t provide the –C or –c option, your commit message editor will come up and will contain the commit message from the HEAD commit allowing you to modify the message if you want. Alternatively, you can still provide a –m “message” and this new message will be used.

image

and you can see in gitk that there is only one commit with that message, but the menu.html file that I previously forgot is there

image

Note that –amend will change the SHA1 ID of the commit… you can see in the original screen shot at the top that the commit was ID 8bbad12 and in the second screenshot and the gitk screen shot, the ID is now 35af25e. If you need to preserve the SHA ID of the previous commit, this is probably not the best option for you. But if you don’t need to preserve it, this can save your coworkers the headache of having to look through a bunch of “forgot to add the files” commits.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Command Line, Git, Source Control. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://colinjack.lostechies.com Colin Jack

    Great stuff, and love the “Git: Oops” approach.

  • Jeremy Hicks

    Your example at the top shows you using the flags -am when doing your commit which should have added the menu.html file.

    • http://mutedsolutions.com Derick Bailey

      the “-a” flag is a shortcut to stage and commit files that are already tracked by git. it won’t add untracked files.

      “git add -A” will add all untracked files / changes, though. perhaps you were thinking of that command?

  • Leandro Ribeiro

    Nice job ! Thanks.

  • http://twitter.com/mrPaumgarten Mathias Paumgarten

    Nice – exactly what I was looking for. 

  • Guest

    Nice.

  • Matthew Leingang

    Thanks! Most of my git searches are about how to fix mistakes I’ve made, so I like your series of articles on the subject.