Skip to content

Instantly share code, notes, and snippets.

@ChunhThanhDe
Created November 12, 2024 03:54
Show Gist options
  • Save ChunhThanhDe/ed008f73dacb01641f9ce7941c0a4952 to your computer and use it in GitHub Desktop.
Save ChunhThanhDe/ed008f73dacb01641f9ce7941c0a4952 to your computer and use it in GitHub Desktop.
Git Commands that save Developers 😎

πŸš€ Merge additional commits

Here's a quick guide to squash commits from HEAD~2 to HEAD~4 (including these commits) into a single commit:

Steps:

  1. πŸ“ Start Interactive Rebase from HEAD~5:

    git rebase -i HEAD~5

    This command starts an interactive rebase from the 5th commit back from HEAD, allowing you to edit commits from HEAD~2 to HEAD~4.

  2. ✏️ Edit the Commit List:

    • Git will open a text editor showing a list of commits.
    • Change pick to squash (or s) for each commit you want to merge into the first one.
    • Keep pick on the first commit in the series, then mark subsequent commits with squash.

    Example:

    pick <commit_id_1> Initial Commit message 
    squash <commit_id_2> Update tests 
    squash <commit_id_3> Adjust styles 
    pick <commit_id_4> Refactor code 
    pick <commit_id_5> Fix small bug 
    
  3. πŸ’Ύ Save and Close:

    • Save and close the editor to start the rebase process.
  4. πŸ“ Edit the Squashed Commit Message (optional):

    • Git will open the editor again to let you edit the final message.
    • Combine messages or create a new one for the squashed commit, with emojis if desired!
  5. πŸš€ Push Changes with --force:

    git push origin main --force
    • Use --force because you've rewritten the history.

And that's it! πŸŽ‰ You’ve successfully squashed commits from HEAD~2 to HEAD~4 into one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment