Skip to content

Instantly share code, notes, and snippets.

@bekirbakar
Created April 20, 2023 11:15
Show Gist options
  • Save bekirbakar/cfbb59ae55c7c7412fd2031f0574bf02 to your computer and use it in GitHub Desktop.
Save bekirbakar/cfbb59ae55c7c7412fd2031f0574bf02 to your computer and use it in GitHub Desktop.
Git: Stash and Squash

Git: Stash and Squash

This tutorial demonstrates how to use Git's stash and squash features to manage changes across different branches and combine multiple commits into one when merging.

By following these steps, you can efficiently manage changes across branches and keep your commit history clean by squashing multiple commits into one when merging.

Prerequisites

  • Git installed on your system.
  • Basic understanding of Git commands and concepts.

Steps

1. Create an Empty Repository

mkdir sample_repository
cd sample_repository
git init

2. Make Some Changes and Commit Them

echo "Initial Content" > file.txt
git add file.txt
git commit -m "Initial Commit"

3. Make More Changes

echo "More Changes" >> file.txt

4. Move These Changes to a New Branch

git stash save "New Changes"
git checkout -b new-feature-branch
git stash apply

5. Commit the Changes on the New Branch

git add file.txt
git commit -m "First change in new-feature-branch."

6. Make More Changes and Commit Them

echo "Even more changes." >> file.txt
git add file.txt
git commit -m "Second change in new-feature-branch."

7. Return to the Main Branch

git checkout main

8. Merge the New Branch with Squash Option

git merge --squash new-feature-branch

9. Commit the Squashed Changes

git commit -m "Merged new-feature-branch with squashed commits."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment