Crazy Developer in Git commit – -amend

Are you new on git? And you know these commands: git add , git commit , git pull , git push , git status. But are you starting to feel limited with those commands? This post is for you! After reading it you will have learned the command: git commit –amend , or at least will have some notion of how it works.

So lets imagine a guy called Crazy Developer and he’s working in a crazy project and he only knows those basic commands I mentioned. So he decides it’s time to add and commit some changes, and to do that he makes use of the commands he’s comfortable with.

So he checks the status:

    $ git status

    Untracked files:
      (use "git add <file>..." to include in what will be committed)

     file2
     file3
nothing added to commit but untracked files present (use "git add" to track)

Add and commit file2 and file3 .

    $ git add file2 file3

    $ git commit -m “finishing crazy stuff”

Cool, work done!

If we list Crazy Developer commits we will have this.

    74e673c finishing crazy stuff
    29sds55 working on crazy stuff

But for some unknown reason Crazy Developer decides to check the files again, and guess what? He found a problem within file3😱😱😱😱😱. He quickly fixes the problem, nice! Now he can add and commit the fix, so lets do this using the basic commands we and Crazy Developer know.

    $ git add file3

    $ git commit -m “fix problem with crazy stuff”

By listing the commits we got this:

    297035e fix problem with crazy stuff
    74e673c finishing crazy stuff
    29sds55 working on crazy stuff

Nice, the problem is fixed and Crazy Developer finishes his crazy stuff.

But wait, I told you guys that at the end of this post you should have learned or at least have gotten some notion of the command: git commit --amend , right?

So let’s get back in time, at the point where Crazy Developer found the problem within file3 . And this time he fixes the problem, but before add and commit the fix, he did a quick search and found git commit --amend , so lets see how this works:

    $ git add file3

    $ git commit --amend

After Crazy Developer executes the above command, this file will open.

    finishing crazy stuff

    # Please enter the commit message for your changes. Lines starting
    # with ‘#’ will be ignored, and an empty message aborts the commit.
    #
    # Author: Crazy Developer <crazy@developer.com>
    # Date: Sat Feb 4 14:14:49 2017 -0200
    #
    # On branch crazy_branch
    # Changes to be committed:
    # new file: file2.txt
    # new file: file3.txt

What is happening here is the added file ( git add file3 ) is getting inserted on Crazy Developer last commit ( finishing crazy stuff), and then commit is opened so he can edit or not the message. To finish the ammend proccess just save and close the file.

Now if we list Crazy Developer commits we will have.

    32f311c finishing crazy stuff
    29sds55 working on crazy stuff

The problem is fixed and Crazy Developer didn’t need a new commit for it.

So now you guys know how to use git commit --amend and the basic idea about it.

Here are a few posts related to git, that are worth a shot if you are interested.
The last one is about git commit --amend , but with a few more details.
Git Workflow Basics
This post is intended for beginners, but I assume you already know the basics of Git usage (commit, push, pull, etc).blog.codeminer42.com

Git Basics: Combining two (or more) commits into just one
This post is an extraction of a subsection of this git workflow tutorial (with a few additions). If you are interested…blog.codeminer42.com

Git Basics: Adding more changes to your last commit
With this post you’ll learn how to modify your last commit, adding (or removing) some changes. You can also follow this…blog.codeminer42.com

Thanks to Dmitry Rocha and Will Soares.

We want to work with you. Check out our "What We Do" section!