# Git: amending commits

How to amend commits. Tip: don’t amend remote commits.

## Amending commits

If you forgot a small change in a commit, you probably don’t want to give that
small change its own commit. Amend it into the previous commit where it belongs:

```shell
$ git commit --amend
```

After that, Git pops out the last commit message into a text editor, for me that
editor was Vim, for you it could be something different. The message can be
edited there if needed, then just save and exit.

**About Vim:** Press `Esc` to enter command mode, then type `:wq` (write and
write) and hit enter. More on exiting Vim
[here](https://stackoverflow.com/questions/11828270/how-to-exit-the-vim-editor).

## Do not amend remote commits

If an amend was made after pushing to the remote, Git gives an error "remote is
behind", when pushing (or something along those lines). Then **you need to force
push the local changes**, note that all the changes done to the remote origin by
another dev will be lost. But if it’s your branch and you know who you work
with, it’s most likely not a problem.

```shell
$ git push -f origin master
```

ps. the default git commit message editor can be changed, for instance to nano:

```shell
$ git config --global core.editor nano
```
