Documentation/content/git/squash-commits.md
crapStone da1b77aa6a Add woodpecker ci & many linters (and their required fixes) (#377)
closes #238

Co-authored-by: pat-s <patrick.schratz@gmail.com>
Co-authored-by: Patrick Schratz <pat-s@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/Codeberg/Documentation/pulls/377
Co-authored-by: crapStone <crapstone01@gmail.com>
Co-committed-by: crapStone <crapstone01@gmail.com>
2024-06-11 07:51:22 +00:00

56 lines
1.4 KiB
Markdown

---
eleventyNavigation:
key: SquashCommits
title: Merge multiple commits into one
parent: Git
order: 50
---
Sometimes you'll want to merge multiple commits into one. Maybe the commits are "dirty", full of non-working code or embarrassing commit messages. The solution shown here is only one of many possible solutions. See this [StackOverflow question](https://stackoverflow.com/q/2563632/4865723) for more details.
Here is an example.
```bash
$ git log --graph --decorate --oneline
* cf634bb (HEAD -> main) english
* 722a9c7 zwei
* e59e6d0 eins
* c6990ba a
* 6dfc50b (origin/main, origin/HEAD) ix
* 10074d7 (tag: test) test
* 662e04e Initial commit
$ git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
```
Here, we want to merge the last 4 commits, from `a` to `english`.
```bash
$ git reset --soft "HEAD~4"
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a
$ git commit --amend
[main 24e0e06] English
Date: Wed May 27 13:56:28 2020 +0200
2 files changed, 3 insertions(+), 1 deletion(-)
create mode 100644 a
$ git log --graph --decorate --oneline
* 24e0e06 (HEAD -> main) English
* 10074d7 (tag: test) test
* 662e04e Initial commit
```
After that, you can push it to the remote repository via `git push`.