Post

Advanced Git Techniques: Stash, Cherry-Pick and Squash



Introduction

Git is a powerful tool for version control, and while many developers are familiar with basic operations like commit, branch, and merge, Git also offers more advanced techniques that can greatly enhance your workflow. In this article, we’ll explore some of these advanced Git techniques, including how to manage temporary changes with git stash, retrieve and delete stashed changes, apply specific commits with git cherry-pick and combine multiple commits using git squash.

Using Git Stash for Temporary Change Management

When working on a feature, it’s common to encounter situations where you need to switch branches or pull the latest changes from a remote repository without committing your current changes. In these cases, git stash is an invaluable tool. It allows you to temporarily save (“stash”) your uncommitted changes and revert your working directory to a clean state. This enables you to work on something else without losing your progress.

To stash your changes, simply run:

1
git stash

This command will save your current changes (both tracked and untracked files, if you use the -u option) and leave you with a clean working directory. You can later apply these changes back to your working directory with:

1
git stash apply

Retrieving and Deleting Changes from Stash

The stashed changes are stored in a stack-like structure, meaning you can have multiple stashes saved. To view all stashes, use:

1
git stash list

This will show a list of stashed changes, each identified by a unique name like stash@{0}, stash@{1}, and so on.

To apply a specific stash, you can run:

1
git stash apply stash@{1}

If you want to remove a stash after applying it, use the pop command:

1
git stash pop stash@{1}

This applies the stash and immediately deletes it from the list. To delete a stash without applying it, use:

1
git stash drop stash@{1}

Or to clear all stashes:

1
git stash clear

Introduction to Git Cherry-Pick

git cherry-pick is a command that allows you to apply the changes introduced by an existing commit onto your current branch. This is especially useful when you need to apply a specific bug fix or feature from another branch without merging the entire branch.

To cherry-pick a commit, simply run:

1
git cherry-pick <commit-hash>

This command will create a new commit on your current branch with the same changes as the specified commit.

Combining Multiple Commits (git squash)

Sometimes, you might end up with several small commits that should logically be a single commit. This is where squashing commits comes in. To squash commits, you can use an interactive rebase:

1
git rebase -i HEAD~n

Replace n with the number of commits you want to squash. An editor will open with a list of commits; for the commits you want to squash, replace pick with squash or s. Once you save and close the editor, Git will combine the selected commits.

This is a great way to clean up your commit history before pushing your changes to a remote repository.

Conclusion

Mastering these advanced Git techniques such as git stash, git cherry-pick and commit squashing can significantly improve your workflow, making you more efficient and helping you maintain a clean and manageable Git history. As you become more comfortable with these commands, you’ll find that Git offers even more powerful tools for version control.

© 2024 Java Tutorial Online. All rights reserved.