10/5/2023 0 Comments Git stash apply vs popAs we tend to find out in our day-to-day programming, real-world use-cases aren't usually that simple. In the examples above, we used stash in the simplest context. $ git stash applyĪnd just like that, you have your changes back. We're back to where we started as if we never made the changes at all! Now you can go off and fix that bug.īut what about restoring your changes? To get them back, we can simply use the apply sub-command, which takes the last-stashed changes and puts them back into your working directory. To verify, look for changes using git status: $ git status Saved working directory and index state WIP on master: bbf6ef9 Initial commitĪs you can see, HEAD is now back to our last commit, which in this case is the initial commit. To avoid losing the current updates you've made, you can just stash the changes instead and get them back later without messing up your commit history. However, the changes aren't finished, and you need to switch to a different branch to quickly fix a bug before continuing on with the current feature. No changes added to commit (use "git add" and/or "git commit -a") " to discard changes in working directory) Let's say you're working on a new feature and you made some modifications to your code, and you now have one or more files with uncommitted modifications: $ git status The stash command takes the uncommitted changes in your working directory, both the updated tracked files and staged changes, and saves them. Luckily, Git provides a mechanism to handle cases like this through the command git stash. However, you don't want to lose the changes you've made already, but they're not yet ready to commit the updates since they're not finished. Stashing just the index (staged changes) in Git is more difficult than it should be.If you've been working with Git long enough, you've probably had times where you made changes to your codebase, but needed to switch branches or work with the latest working version of your code. This answer is therefore obsolete for Git 2.35+. UPDATE January 2022: Git 2.35 has been released, and stash now supports a -staged parameter. $ git stash pop # to finish work on the saved changes $ git switch fixup-branch # switch to another branch $ git commit -m 'Massive' # commit fully tested changes $ git stash push -staged # save these changes to the stash $ git add -patch foo # add unrelated changes to the index Only the commit ends-up being in the stash and not on the current branch. This is similar to committing the staged changes, Unrelated issue that you don't want to forget to fix, you can do theĬhange(s), stage them, and use git stash push -staged to stash them When you are in the middle of massive changes and you find some Git stash now includes in its man page: Saving unrelated changes for future use The -patch option has priority over this one. This is similar toīasic git commit except the state is committed to the stash instead Stash only the changes that are currently staged. This option is only valid for push and save commands. Git stash now includes in its man page: -S -staged Unlike ' stash push -patch', -staged supports use of any tool to select the changes to stash-out, including, but not limited to ' git add -interactive' ( man). This mode allows to easily stash-out for later reuse some changes unrelated to the current work in progress. (Merged by Junio C Hamano - gitster - in commit 44ac8fd, ) stash: implement '-staged' option for 'push' and 'save' See commit a8a6e06 (), and commit 41a28eb () by Sergey Organov ( sorganov). So this is now officially supported (8 years later). With Git 2.35 (Q1 2022), " git stash" ( man) learned the -staged option to stash away what has been added to the index (and nothing else). Then you can add another alias and run git move-staged: git config -global alias.move-staged '!bash -c "git stash-staged git commit -m "temp" git stash git reset -hard HEAD^ git stash pop"' If you do not want to keep staged files and want move them into stash. Git stash save -patch # for older git versionsĪnd git will ask you for each change in your files to add or not into stash.Īlias for DOUBLE STASH: git config -global alias.stash-staged '!bash -c "git stash -keep-index git stash push -m "staged" -keep-index git stash pop you can stage your files and then run git stash-staged.Īs result your staged files will be saved into stash. With latest git you may use -patch option git stash push -patch # since 2.14.6 This is similar to basic git commit except the state is committed to the stash instead of current branch. Here you can find how to stash only unstaged changes. Create the alias for this command: git config -global alias.stashs 'stash push -S'.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |