Squashing Git commits

Squashing Git commits is a technique used to combine multiple commits into a single, more meaningful commit. This can help keep your Git history clean and concise. Here are the steps to squash Git commits:

  1. Identify the Commits to Squash:

    • First, you need to identify the range of commits you want to squash. Typically, you'll want to squash the most recent commits.
  2. Open Your Terminal:

    • Open a terminal or command prompt.
  3. Checkout the Branch:

    • Ensure you are on the branch where you want to squash the commits. You can switch to the branch using the git checkout branch-name command.
  4. Start an Interactive Rebase:

    • Use the following command to start an interactive rebase for the last n commits (replace n with the number of commits you want to squash):
    shellCopy code
    git rebase -i HEAD~n

    For example, if you want to squash the last three commits, you would run:

    shellCopy code
    git rebase -i HEAD~3

    This will open an editor with a list of commits in your text editor.

  5. Squash Commits:

    • In the text editor, you'll see a list of commits with the word "pick" in front of each commit message. To squash commits, change the word "pick" to "squash" (or just "s" for short) for the commits you want to squash. Leave the word "pick" for the commit you want to retain as the final commit.

    Example:

    shellCopy code
    pick abc123 First commit squash def456 Second commit squash 789xyz Third commit
  6. Save and Close the Editor:

    • Save the changes and close the text editor.
  7. Edit the Combined Commit Message:

    • Git will open another text editor for you to edit the commit message of the combined commit. Here, you can modify the commit message if needed. Once you're satisfied with the message, save and close the editor.
  8. Finish the Rebase:

    • After you've saved the combined commit message, the rebase is complete. Git will apply the changes, and you'll have a single squashed commit with a new commit message.
  9. Push the Changes:

    • If you've pushed the branch to a remote repository previously, you'll need to force-push the changes to the remote branch since you've rewritten the commit history. Use the following command:
    shellCopy code
    git push origin branch-name --force

Please be cautious when using --force because it can overwrite the remote branch's history, potentially causing issues for collaborators. Make sure to communicate with your team if you're working in a shared repository.

Did you find this article useful?

  • DISK ANALYSIS

    Analyzing storage on a Linux server is important to understand how disk space is being used and to i...
  • git update commit message

    git commit --amend -m "[prometheus] changes for remote config service" o amend a specific commit's ...
  • INSTALL LOKI AND PROMTAIL

    Installation of Loki and Promtail on RHEL 8.3 Overview & Architecture I’ve been looking fo...
  • INSTALL NODE EXPORTER

    Node Exporter Setup on Linux Nodes https://docs.vmware.com/en/VMware-vRealize-Operations-Manageme...
  • HEXAGONAL ARCHITECTURE

    https://miro.com/app/board/uXjVMcx1pU4=/ https://herbertograca.com/2017/11/16/explicit-architecture...