info: "To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments"
If you want to keep the default branch commit history clean, you don't need to
manually squash all your commits before merging every merge request;
with [Squash and Merge](../../user/project/merge_requests/squash_and_merge.md)
GitLab does it automatically.
When you want to change anything in recent commits, use interactive
rebase by passing the flag `--interactive` (or `-i`) to the rebase command.
For example, if you want to edit the last three commits in your branch
(`HEAD~3`), run:
```shell
git rebase -i HEAD~3
```
Git opens the last three commits in your terminal text editor and describes
all the interactive rebase options you can use. The default option is `pick`,
which maintains the commit unchanged. Replace the keyword `pick` according to
the operation you want to perform in each commit. To do so, you need to edit
the commits in your terminal's text editor.
For example, if you're using [Vim](https://www.vim.org/) as the text editor in
a macOS's `ZSH` shell, and you want to **squash** all the three commits
(join them into one):
1. Press <kbd>i</kbd> on your keyboard to switch to Vim's editing mode.
1. Navigate with your keyboard arrows to edit the **second** commit keyword
from `pick` to `squash` (or `s`). Do the same to the **third** commit.
The first commit should be left **unchanged** (`pick`) as we want to squash
the second and third into the first.
1. Press <kbd>Esc</kbd> to leave the editing mode.
1. Type `:wq` to "write" (save) and "quit".
1. Git outputs the commit message so you have a chance to edit it:
- All lines starting with `#` will be ignored and not included in the commit
message. Everything else will be included.
- To leave it as it is, type `:wq`. To edit the commit message: switch to the
editing mode, edit the commit message, and save it as you just did.
1. If you haven't pushed your commits to the remote branch before rebasing,
push your changes normally. If you had pushed these commits already,
[force-push](#force-push) instead.
Note that the steps for editing through the command line can be slightly
different depending on your operating system and the shell you're using.
See [Numerous undo possibilities in Git](numerous_undo_possibilities_in_git/index.md#with-history-modification)
for a deeper look into interactive rebase.
## Force-push
When you perform more complex operations, for example, squash commits, reset or
rebase your branch, you'll have to _force_ an update to the remote branch,
since these operations imply rewriting the commit history of the branch.
To force an update, pass the flag `--force` or `-f` to the `push` command. For
example:
```shell
git push --force origin my-feature-branch
```
Forcing an update is **not** recommended when you're working on shared
branches.
Alternatively, you can pass the flag [`--force-with-lease`](https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-with-leaseltrefnamegt)
instead. It is safer, as it does not overwrite any work on the remote
branch if more commits were added to the remote branch by someone else: