-
-
Notifications
You must be signed in to change notification settings - Fork 74.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stashing a file #6096
Stashing a file #6096
Conversation
@@ -0,0 +1,137 @@ | |||
# Stashing | |||
|
|||
What if you are working on a big code and suddenly you need to switch the branch from which you are currently working on to some other branch. Since the code you are writing is not completed, and you don't want to commit it either since the code has not been tested too. How to prevent this? This is what this tutorial covers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you are working on a big code and suddenly you need to switch the branch from which you are currently working on to some other branch. Since the code, is not complete, and without any tests, you probably don't want to commit it. But you cannot move to the other branch without committing the changes, Git won't let you break this flow. What do we do then? How do we prevent an unnecessary commit, while being able to jump branches? This is what this tutorial covers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this intro?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this sure looks great. Will update it ASAP.
|
||
## Stashing your work | ||
|
||
Let's assume you are working on some project and made some changes with some files. Now if you run ```git status``` you can see your changes in the files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's assume you are working on a project's branch where you have changed some files.
Just making the sentence simpler.
nothing to commit, working directory clean | ||
``` | ||
|
||
Now you can switch to any branch and do your work; your changes will be stored in the stack. To see which stashes you have stored in the stack you can use ```git stash list```: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your stashed changes are stored in form of a stack.
stash@{2}: WIP on master: 21d80a5 added number to log | ||
``` | ||
|
||
In case you want to reapply the one you just stashed, you can use the command ```git stash apply```. By using this command you can reapply the most recent stashed file. In order to reapply any other file, you can specify it by naming it like: ```git stash apply <stash-name>```, in place of ```<stash-name>``` write the name of the stash you need to reapply. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case you want to re-apply the changes you just stashed,
# | ||
``` | ||
|
||
You can see that git re-modifies the file that you uncommitted when you saved the stash. In this case, you had a clean working directory when you tried to apply the stash, and you tried to apply it on the same branch you saved it from; but having a clean working directory and applying it on the same branch aren’t necessary to successfully apply a stash. You can save a stash on one branch, switch to another branch later, and try to reapply the changes. You can also have modified and uncommitted files in your working directory when you apply a stash, git gives merge conflicts if anything no longer apply cleanly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
, and re-apply the changes in the new branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
longer applies cleanly.
|
||
You can see that git re-modifies the file that you uncommitted when you saved the stash. In this case, you had a clean working directory when you tried to apply the stash, and you tried to apply it on the same branch you saved it from; but having a clean working directory and applying it on the same branch aren’t necessary to successfully apply a stash. You can save a stash on one branch, switch to another branch later, and try to reapply the changes. You can also have modified and uncommitted files in your working directory when you apply a stash, git gives merge conflicts if anything no longer apply cleanly. | ||
|
||
The changes made to your files are reapplied, but the file you staged did not restaged. To do so you need to run the command ```git stash apply``` with a ```--index``` to tell the command to reapply the staged changes. If you have run that instead, you would have returned to your original position: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was not restaged.
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43) | ||
``` | ||
|
||
You can even use ```git stash pop``` to apply the stash and then immediately drop it from your stack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can even use git stash pop
to apply the stash and then immediately drop it from your stack. Git stash pop is not for that.
It is used to pop the last applied stash and apply it on the current branch. It does not have to be immediately, the only function of it stash pop is to un-stash the last set of changes, like the pop operation of the stack.
You can use ```git stash pop``` to un-stash the last changes drop it from your stash's stack.
|
||
## Un-applying a Stash | ||
|
||
In some cases you want to apply stashed changes, do some work, but up-apply the changes originally came from the stash. Git does not provide command like ```git unapply```, but it is possible to achieve this effect by simply retrieving the patch associated with a stash and applying it in reverse: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...changes that originally came...
|
||
## Creating a Branch from Stash | ||
|
||
If you stash some work, leave it there for a while, and continue on the branch from which you stashed the work, you may have a problem reapplying the work. If the apply tries to modify a file that you’ve since modified, you’ll get a merge conflict and will have to try to resolve it. If you want an easier way to test the stashed changes again, you can run ```git stash branch```, which creates a new branch for you, checks out the commit you were on when you stashed your work, reapplies your work there, and then drops the stash if it applies successfully: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will have to resolve it
. No need to add the word try
.
@akramkazmi71 Please address the review comments. |
@sara-02 modified the changes as requested. |
No description provided.