![]() ![]() ![]() ![]() The stash command can be very powerful, and there are more features to it than what was covered here, like various flags that are available, which we'll save for another article. Here we've seen how to handle the use-case where you have changes in your working directory, but you want to switch branches and not commit the unfinished changes. Unsurprisingly, Git has a solution for most problems that arise in version control since it's been around for such a long time. $ git stash list : WIP on master: bbf6ef9 Initial commit You're able to view this stack using the list subcommand. Technically, when you stash changes, Git puts the changes on a stack, which can then be pulled off in a LIFO (last in, first out) order. When you’re ready to reapply your changes, you will have the option to apply or pop your stash to your currently checked out branch. Creating a stash in Git saves uncommitted changes so you can work on other things in your repository without losing your work. What if, for example, you end up needing to stash changes from your working directory multiple times? Luckily, stash allows you to do just that. Here is where the Git stash command comes in. If you wish to see the contents of your most recent stash, you can run: git stash show. This will put your working copy in a clean state and allow you to run different commands, such as Git checkout or Git pull. As we tend to find out in our day-to-day programming, real-world use-cases aren't usually that simple. To stash uncommitted local changes in Git using the terminal, you will simply run the Git stash command. 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. 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. ![]()
0 Comments
Leave a Reply. |