To Nha Notes | Dec. 22, 2023, 10:48 a.m.
To squash multiple commits into a single commit, you can use `git rebase` in interactive mode (`-i`). Here's a step-by-step guide:
1. Determine how many of the latest commits you want to squash. For example, if you want to squash the last 3 commits, you would use the command `git rebase -i HEAD~3`.
git rebase -i HEAD~3
2. This will open a text editor with a list of the last 3 commits at the top. Each commit is prefixed with the word `pick`.
3. To squash the commits, you need to replace `pick` with `squash` or `s` next to each commit you want to squash into the previous commit. The first commit should be a `pick`, and the following ones should be `squash`.
4. Save and close the editor.
5. An editor window will open for you to change the commit message. This will be the commit message for the new squashed commit.
6. Save and close the editor.
7. If everything was successful, you should have squashed your last 3 commits into one. You can confirm this with `git log`.
git log --since='1 hour ago' | grep commit
Remember, this will rewrite your Git history. You should avoid doing this if you've already pushed your commits to a public repository.