5

How git stash Lets You Reset Your Project and Save the Changes

 1 year ago
source link: https://www.makeuseof.com/how-git-stash-lets-you-reset-your-project-and-save-the-changes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How git stash Lets You Reset Your Project and Save the Changes

Published 7 hours ago

Stashing is a little-known, yet powerful git technique for keeping code safe even when you’re not ready to commit it.

A set of metal, pastel-colored lockers.

Stashing lets you keep a copy of changes you’ve made to a repository, without having to create a commit.

It’s useful if you’re switching contexts, especially if you’re going back and forth between different bugs or tasks on the same project.

Basic Operation of git stash

You can use git stash to ease your workflow when dealing with parallel lines of work. Imagine you’re working on a long-running task, with changes in your local working copy. Then, something urgent comes up that you have to work on right away.

The standard workflow for stashing changes is:

  1. Make local changes
  2. Stash local changes
  3. <other work>
  4. Reapply stashed changes

When you stash changes using the git stash [push] command, git resets to HEAD. You can then carry on working on whatever you need to, committing to the repository as if you never made the original changes.

Once you’re done with whatever side-tracked you in the first place, use git stash pop to apply your changes and remove them from the stash. You can also apply your changes and keep them in the stash with git stash apply. This might be useful if you want to quickly apply the changes to multiple branches.

Working With More Than One Stash

If you’re really busy, you might find yourself working on several tasks at once, and you might need to stash them all. Don’t worry, git stash is built for this.

Every time you use git stash push, you’re saving another set of changes. Use git stash list to show everything you’ve stashed. You’ll see something a bit like this:

stash@{0}: WIP on main: 2fba62e first commit
stash@{1}: WIP on main: 2fba62e first commit

These messages aren’t very useful, but you can leave some clues for yourself by adding a custom message when you stash:

git stash push -m "third"

When you list now, you’ll see your custom message:

stash@{0}: On main: third
stash@{1}: WIP on main: 2fba62e first commit

Showing the Differences Between Diffs

To find out what’s changed in a stash, use git stash show. Without further arguments, it will show a diff summary for the latest stash, looking like this:

$ git stash show
README.md | 3 +++
1 file changed, 3 insertions(+)

You can also pass a stash id to query a specific entry:

git stash show stash@{0}

Creating a Branch From a Stash

You might decide that the changes in a stash are so significant that they deserve to be in a branch of their own. If so, create a new branch from the stash using the branch command:

git stash branch

Again, this will work on the most recent stash by default, but you can supply a stash id if necessary. Git creates your new branch from the same point in the repository as the stash. It then applies the changes from the stash to your working copy.

Cleaning the Stash Up

There is no “git unstash” command. If you want to remove a stash entry, use drop:

git stash drop

Again, this defaults to the latest but you can supply a stash id instead. If you decide you want to get rid of everything you’ve stashed, use this command:

git stash clear

Use git stash for Temporary Lightweight Commits

Git stashes are nowhere near as powerful as full repositories. But they still offer a lot of useful functionality in their own right. Use stashes if you often find yourself needing to switch branches in the middle of work.

Stashing is just one small part of git, which is a program with an awful lot to offer.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK