How to amend changes to your code in your most recent commit – 074

Yesterday’s tip was about making changes to your commit message. Today’s tip is making changes to your code as well.

Hey my Git experts friends reading this! I’m not sure what the command line equivalent is, so can someone look at my section below on using the CLI and tell me why I have to use –allow-empty?

We’ll start with Team Explorer in Visual Studio and then rinse and repeat with what I think is the equivalent git command.

Amend using Visual Studio

It looks like Team Explorer will let you amend based on whatever changes are in the working directory, but just in case you only want to change one file, I’d suggest staging the file(s )you want to amend first. In a command line scenario, whatever files are in staging are the ones that will be amended. (See previous tips)

Actions button in Team Explorer

Click on the Actions button and select Amend Previous Commit.

Amend Previous Commit

And you’ll get a new commit ID.

new commit ID shown in TE info bar

Amend using Command Line

Full disclosure: I don’t know when to use the –allow-empty option. I also don’t know what the ramifications of using it are. It seems I need to use this option even though 1. the staged changes are different than those in the commit history and 2. I’ve specified a commit message. TBH I’m not sure what is going on here.

But this is my best guess at the command line equivalent of what Team Explorer is doing.

  1. make changes to your file
  2. use git add to add those changes to staging
  3. git commit –amend -m “new message” –allow-empty or git commit –amend –allow-empty (but I’m not sure)

The below screenshot shows what happens when I don’t use the –allow-empty option.

git commit --amend -m "new message" wants the --allow-empty even though changes are staged

and here’s what happens when I use the –allow-empty, which turns out to be the behavior I want (or as far as I know!)

git commit -m "update readme" --amend --allow-empty shows success

So yeah, if you know what is going on wrt to the –allow-empty option, please let me know!!

2 thoughts on “How to amend changes to your code in your most recent commit – 074

  1. The message you got when not doing –allow-empty says “doing so would make it empty”. I believe it’s saying that result of the amend would have a commit where nothing changed from its parent.
    So like if you have a text file with the content “test”, you commit (c1), then you change it to “test test”, commit (c2), then you change it back to “test” and tried to amend w/o allow empty, it would fail because you’re trying to do a commit of nothing – the diff between c1 (“test”) and the proposed new c2 (“test”) is nothing.
    If you then do the amend with allow empty, then look at the commit, it wouldn’t have your file because there is no change. The commit has no files and is “empty”.

    Like

Leave a comment