0% found this document useful (0 votes)
2 views

Stashing commands

The document explains the Git stash command, which allows developers to temporarily save changes without committing them, enabling branch switching for urgent tasks. It contrasts Git stash with Git commit and outlines best practices for using stash effectively. Additionally, it provides practical scenarios and commands for managing stashed changes and emphasizes the importance of using stash moderately and descriptively.

Uploaded by

jafoha4863
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Stashing commands

The document explains the Git stash command, which allows developers to temporarily save changes without committing them, enabling branch switching for urgent tasks. It contrasts Git stash with Git commit and outlines best practices for using stash effectively. Additionally, it provides practical scenarios and commands for managing stashed changes and emphasizes the importance of using stash moderately and descriptively.

Uploaded by

jafoha4863
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Stashing

• Suppose a developer is working on a feature in a branch and


he needs to pull changes from some other developer’s branch
or if he has to work urgently on some other feature, but the
feature he is currently working on is incomplete. In this case,
you cannot commit the partial code of the currently working
feature. To add this new feature, you have to remove your
current changes and store them somewhere else. For this type
of situation, Git offers a very useful command known as ‘git
stash‘. git stash command saves the previously written code
and then returns to the last commit for a fresh start.
Git stash vs Git Commit

• Git stash: The Git stash command can be used to accomplish


this if a developer is working on a project and wants to
preserve the changes without committing them. This will
allow him to switch branches and work on other projects
without affecting the existing modifications.
• roll back modifications whenever necessary,
• Git commit: Developers wish to permanently store their
changes to the source code in the repository history. Thegit
commit command can be used to accomplish this. Git creates a
new commit that documents your modifications and adds
them to the repository history.
Git Stash vs Git Reset
Git Stash vs Git Stage
Which Folder Stores Git Stash History?

• When we use git stash the data will be stored in the form of
stashes in our local repository. By default, the data will be
stored in the git/refs/stash file. In this file, all the references are
stored and the actual data is stored in the .git/objects directory,
just like other Git objects.
• The below command is used to list all the stashes that have
been created.
• Git stash list
How Do You Stash Changes In Git?

• To save the uncommitted changes for later usage, you can use
the ‘git stash’ command. This command saves your local
modifications away and reverts the working directory to match
the HEAD commit so that it will give you a clean working
directory.
• Here are some important options that are most widely used:
• Git stash
• Git stash save
• Git stash list
• Git stash apply
• Git stash changes
• Git stash pop
• Git stash drop
• Git stash clear
• Git stash branch
Git Stash Best Practices

• Here are some best practices for using the “Git stash” command.
1. Don’t overuse git stash: Git stash must be used moderately
whenever it is very important to work on another task then only we
should use git stash it will affect the changes that have to be made
in committing.
2. Avoid Unnecessary messages: Using git stash, you can add a
message to describe the changes you are stashing. Make the
message meaningful. When you need to remember what’s in the
stash later, this will come in handy. Employ statements that are
informative and evocative and that accurately reflect the changes
being concealed.
1. Employ branches: Stashing is a helpful tool when working
with Git branches. Before stashing changes, make a new
branch so you can switch to different tasks or branches
without affecting your current changes. Revert back to the
branch and apply the stash when you’re ready to work on the
modifications once more.
2. Changes should be made as quickly as feasible: storing
should only be used as a temporary solution. Once you have
finished making a set of changes, you should commit them to
the repository to preserve a record of the changes.
• You're working on a new feature in a branch, and you've made
several changes. Suddenly, a critical bug is reported in the
main branch that needs immediate attention. You need to
switch branches to fix the bug, but you haven't committed your
current changes. How do you use stashing to handle this
situation?
• After pulling the latest changes from the remote repository, you
realize that your local changes conflict with the updates. You
want to keep your local changes but need to start fresh with the
latest version from the remote. Describe how you would use
stashing to resolve this.
• You've stashed some changes to switch branches, but now you
want to review what you stashed before applying it back. What
commands would you use to view your stashed changes, and
how would you proceed after reviewing them?
• You've made multiple stashes while working on different tasks.
How can you list all your stashed changes, and what steps
would you take to apply a specific stash without losing the
others?
• You have a mix of tracked and untracked files in your working
directory. You want to stash your changes, but you only want to
include the tracked files. How can you accomplish this using
Git stash commands?
• You’re on a feature branch and have stashed some changes.
You decide to create a new branch from the current one. How
would you carry over your stashed changes to this new branch
after it’s created?
• You’re working on a new feature for your project in a Git repository. You’ve made a few
local changes to your files, but the feature isn’t complete yet. Just as you’re about to
continue working, you receive an urgent request from your team lead. They need you to
quickly switch branches to fix a critical bug in the main branch. The code in main is stable,
and you don’t want to commit your unfinished work just yet.

• Question:How can you use Git stash to save your current changes without committing
them, so you can safely switch to the main branch to fix the bug? After you fix the bug
and return to your original branch, how would you retrieve your stashed changes and
continue working?

You might also like