Modify the last commit, with Git

A common operation of maintaining a Git repository is to modify the last commit with the –amend option.

In Git’s day-to-day there is a situation that occurs quite often and that we can solve in an elegant way with a certain option of the commit action. Basically it is about modifying the last commit, instead of creating a new one.

Imagine that you are editing your project, you make a series of changes and then you make the corresponding commit to commit them to the repository. Almost immediately you realize you made a mistake. Then you can fix your code to fix it and launch another commit to confirm it. Everything is fine, what happens is that the functionality that you have added is only one, what happens is that two commits were generated when the most elegant thing is that we would have created only one, with the functionality well done.

The way to solve this situation is very simple and it is worth looking at it, because it does not involve any effort and allows us to manage our repository a little better. As we are surely starting with Git, reading the one from .com, we are going to do a complete practice that at the same time will serve as a review of everything we have learned.

Status of our repository

Let’s start with an empty repository:

git init

Now we create a file called “README”. In which we can already place some text. You can also do it with the console with this command (although you can also do it, of course, with your preferred editor).

See also  TLDs - Top Level Domains

echo “This is the README” > README

Then I add the files to the staging area, so that they are tracked by Git.

git add .

Now I make a commit committing the changes.

git commit -m ‘created readme’

Make changes to the file

Now you realize that you didn’t put something in the README and you edit it again to put any new content, like your name as the author of the project and your email. You open the file with your editor and modify it. We can see the changes just made with the status command:

git status

You will get an output like this:

Now you could add that file:

git add .

And then do the commit with:

git commit -m ‘Specify the name of the author”

Note: Since the file is already in the repository, and is being tracked by Git, you could save yourself a step by adding the file and doing the one-time commit, with git commit -am ‘provided author name’.

But it is precisely what we want to avoid. I don’t need two commits for this operation, since I just created the README and forgot one simple thing that is not worth having in a separate commit.

Modify the commit with –amend

So, we’re going to alter the previous commit, since that’s the logical thing to do, instead of producing a new commit. We simply indicate the –amend option.

git commit –amend

Note: Although we did it in the previous step, don’t forget to do the “git add .”, because if not in the commit there will be nothing to send.

In this case we do not put a message, since we had already indicated the message in the previous commit. However, Git will open an editor for us so that we can edit the message and change it to the one we like best. You can save yourself the (for some annoying) command line editor by indicating the message in the commit with “-m”. Even if you are making an “amend” and you already have a message, you can directly overwrite it without taking into account the above: git commit –amend -m ‘Created the readme edited the commit first’

See also  Create a Layout with Tailwind CSS

The editor may vary depending on the system you are on. But basically in all cases you will be able to edit the text of the commit message and then activate the option to exit editing the text.

Note: Vim opens it for me, so I have to, from command mode (press ESC to make sure you’re in command mode), type :wq and press enter.

Done, with this we have edited the previous commit instead of creating a new one.

Lastly, if you do a git log you can see that there is indeed only one commit in the repository, since the one we did to fix the problem in the README really only changed the previous commit.

Done, that’s all. You now know how to modify the last commit without having to make a new and unnecessary commit.

Loading Facebook Comments ...
Loading Disqus Comments ...