This is a post for those who start working with Git. Everything that is written here in parts can be found in numerous sheets about Git on Habré. But I thought that it would be nice to have a separate extremely clear topic, which would google on request “git change commits”.
Change last commit
If you overlooked something in the last commit, then edit it is not difficult. All you need is to add changes in the usual way:
git add .
Then commit the changes with the --amend parameter (amend / əˈmɛnd / - to make amendments, correct, improve):
git commit --amend
Change the name of the last commit
The same, with the only difference that there is no need to add files to the commit. Just enter the new name:
')
git commit --amend -m " "
Change NOT last commit
It is a bit more complicated here, make two commits to begin with, in my example they will be called
1
and
2
:

To begin with we carry out:
git rebase --interactive
An editor opens in which you can specify what you want to do:

As you can see,
git rebase -i
can serve when needed
r reword
rename commite edit
edit commits squash
glue two or more commits (squash / skwɒʃ / - squeeze, squeeze, pumpkin :))
Next to commit
1
instead of
pick
type
e
to edit the commit or
r
to rename. After saving the file, you will see a hint from Git:

We will understand in more detail what happened. We moved to commit
1
, "jumping" from the master branch. You can check this by running:
git branch
In response, we get:
* (no branch, rebasing master) master
Further, as the first part of the post where we changed the last commit, make changes and add them:
git add .
and commit with the
--amend
parameter:
git commit --amend
After a successful commit, following the hint (just above in the screenshot), we perform:
git rebase --continue
Thus, we will return to the
master
branch with the changed commit, as required.
The post was short and hopefully clear. Commit early, commit often.