Inspect the results carefully (with git diff) to see if you like them, and if you do, use git stash drop to drop the stash. This gets Git to merge in your earlier changes, using Git's rather powerful merge mechanism. Check out the other branch and use git stash apply. Run git stash save (or plain git stash, same thing). You just want to take the changes you have now and "move" them to another branch. You started with a clean branch, were working on some changes, and then realized you were doing them in the wrong branch. The above is for "way 1", the "easy way": There are at least three or four different "ways to use git stash", as it were. What if you're doing more-advanced or more-complicated stuff? It's all pretty minor one way or the other though, and for a newbie to Git, it should be about the same. If you apply, you get to choose when to drop. If pop is able to extract the stash, it will immediately also drop it, and if you subsequently realize that you wanted to extract it somewhere else (in a different branch), or with -index, or some such, that's not so easy. The difference is that apply leaves the stash around for easy re-try of the apply, or for looking at, etc. I always suggest using git stash apply rather than git stash pop. Then use git diff to see the result.Īfter you're all done with your changes-the apply looks good and you're sure you don't need the stash any more- then use git stash drop to get rid of it. Just check out the branch you want your changes on, and then git stash apply. The easy answer to the easy question is git stash apply If you had run that instead, you’d have gottenīack to your original position: $ git stash apply -index To do that, you must run the git stash applyĬommand with a -index option to tell the command to try to reapply The changes to your files were reapplied, but the file you stagedīefore wasn’t restaged. You can also have modified and uncommitted files in your working directory when you apply a stash - Git gives you merge conflicts if anything no longer applies cleanly. You can save a stash on one branch, switch to another branch later, and try to reapply the changes. 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. Git re-modifies the files you uncommitted when you saved the stash. Occasionally I've accidentally done git stash apply -keep-index instead of git stash apply -index, having mixed up the two options. Instead, -keep-index affects what stash does after making its special stash commits. Without these flags, such files don't go in to either part of the stash, though.ĢConfusingly, the stash script also has a -keep-index flag, which it allows you to specify for apply operations but has no meaning there. The new branch branches-out from the commit you were on at the time you made the stash.)ġWith -u or -a, which save not just staged and unstaged files abut also ignored and/or all files, the stash script makes three commits. You can do this with the raw hash ID of an already-popped stash, though you will generally need a "clean" state. But if you've used it and now regret it, consider git stash branch, which turns a saved stash into a branch. It has too many traps for the unwary, plus it historically has had a number of bugs. (Side note: I actually recommend avoiding git stash as much as possible. I generally recommend using separate apply and drop just for this reason. If you've used pop, though, and git thinks the apply worked, it then drops the stash. When you use apply, the stash script keeps the stash commits around as well, so if the apply does not go the way you wanted-including if you forgot -index or misspelled it (see footnote 2)-you can git reset -hard (assuming you had everything in a clean state when you started, anyway) and re-do the apply. Later, when you use git stash apply, git smushes the changes together so that nothing is staged, unless you add -index 2 to the apply operation, in which case it restores (if it can) your previous arrangement of staged vs unstaged. The second commit holds state of the work tree, i.e., everything else. One commit holds the state of the index, i.e., whatever you've staged. When you run git stash push (the new verb) or git stash save (the old verb) (or plain git stash which does a push/ save), git makes two 1 commits that are not on any branch. But let's back up a bit and look at the underlying mechanism, since it matters here. If you've applied with git stash apply (vs git stash pop), you're in great shape because the stash is still present. The answer to the question as asked ("does stash convert staged files to unstaged") is both yes and no.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |