![]() ![]() To check the current status of the repository, run the git status command. To stash, let's have a look at the repository's current status. We can stash it to save as its current status. ![]() ![]() So I want to save it temporarily for future use. I am in a messy state, and I have not entirely edited any file yet. I have made changes to my project GitExample2 in two files from two distinct branches. Let's understand it with a real-time scenario. Many options are available with git stash. Stashing takes the messy state of your working directory, and temporarily save it for further use. Generally, the stash's meaning is " store something safely in a hidden place." The sense in Git is also the same for stash Git temporarily saves your data safely without committing. The below figure demonstrates the properties and role of stashing concerning repository and working directory. The git stash command enables you to switch branches without committing the current branch. You don't want to make a commit of half-done work. Sometimes you want to switch the branches, but you are working on an incomplete part of your current project. 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. First, a developer will initialize a Git repository, add two files to the Git worktree and issue a commit: git init. Heres a simple example of how to use the git stash command. You can save a stash on one branch, switch to another branch later, and try to reapply the changes. Call the git stash pop command at any point to apply the shelved files. 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. Earlier, we had an option to apply or pop, well this is where it. 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. Now that your code is safe and sound in your commit there might be some cleaning up to do. 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. To push a new stash onto your stack, run git stash or git stash push: git stash Saved working directory and index state \ 'WIP on master: 049d078 Create index file' HEAD is now at 049d078 Create index file (To restore them type 'git stash apply') You can now see that your working directory is clean: git status On branch master nothing to. 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 |