Imagine that you"re working on a feature in a Git-controlled software project. You"re right in the middle of making some changes when you get a request to fix a critical bug. To start resolving the issue, you need a new branch và a clean working directory. When it comes to basic Git commands, you have sầu two options:Run git remix --hard khổng lồ remove your uncommitted changes.Record your incomplete work as a new commit.
Bạn đang xem: Nhàn hơn cùng git stash!
The former option loses all of your work while the latter results in a partial commit that isn’t meaningful. Neither of these scenarios is all that desirable.
This is where the git stash command comes inkhổng lồ play. Like git rephối --hard, it gives you a clean working directory, but it also records your incomplete changes internally. After fixing the critical bug, you can re-apply these changes và pichồng up where you left off. You can think of git stash as a "pause button" for your in-progress work.
This tutorial assumes that you have sầu installed Git & that you"re familiar with its basic workflow. You should be comfortable staging changes, creating commits, & working with branches. You"ll also need a Git repository to experiment on.
1. Stashing Changes
Before you can run git stash, you need to lớn have sầu some uncommitted changes in your Git repository. For example, if you edited a tệp tin called foo.py, your git status output would look lượt thích this:
On branch masterChanges not staged for commit: (use "git add ..." lớn update what will be committed) (use "git checkout -- ..." khổng lồ disthẻ changes in working directory) modified: foo.pyTo stash these changes, simply execute git stash without any arguments.
git stashThis will take both your staged and unstaged changes, record them internally, then clear the working directory. This gives you the opportunity to lớn switch lớn a new branch & develop other features without worrying about your partial commit messing anything up.
2. Re-Applying Stashed Changes
When you"re ready lớn come baông xã to your incomplete work, run the following command lớn re-apply the stashed changes:
git stash popThe most recently stashed changeset will re-appear in your working directory and you can continue exactly where you left off. That"s all there is to lớn it.
3. Resolving Conflicts
Much like the git merge command, git stash pop can result in conflicts if the same sections of source onfire-bg.com have sầu changed since you executed git stash. When this happens, you"ll see the following message after running git stash pop:
Auto-merging foo.pyCONFLICT (content): Merge conflict in foo.pyYou"ll also find the affected tệp tin listed under the Unmerged paths section in the git status output, as well as the affected lines in the source tệp tin.
4. The Stash Stack
For most scenargame ios, the above commands are all you need when it comes to a "pause button". But, understanding how stashed changes are represented opens the door for more advanced usage.
So far, we"ve sầu only been talking about stashing a single changephối. However, each time you run git stash, uncommitted changes are stored on a stack. This means that you can stash multiple changesets at the same time.
This is useful in the early stages of development when you"re not sure which direction you want to take. Instead of losing your changes with git reset --hard, you can keep your work-in-progress snapshots on the stash staông xã in case you want lớn re-apply one of them later.
You can inspect the stash stachồng with the list parameter.
git stash listIf you had previously executed git stash three times, this would output something lượt thích the following:
2: WIPhường on new-feature: 5acd291 Begin new featureThe git stash pop comm& always re-applies the most recent snapshot, the one at the top of the stash stack. But, it"s also possible khổng lồ pick và choose which stashed snapshot you want to lớn re-apply with the apply command. For example, if you wanted khổng lồ re-apply the second set of changes, you would use the following command:
git stash apply stash
1Just like git stash pop, the changes will re-appear in your working directory & you can continue working on the incomplete feature. chú ý that this will not automatically remove sầu the snapshot from the stash staông xã. Instead, you"ll need khổng lồ manually delete it with the drop comm&.
git stash drop stash
1Again, working with the stash stack is more of an edge case for most Git users. The git stash and git stash pop commands should suffice for most of your needs, although git stash list can also prove sầu useful if you forgot where your last stash operation took place.
Committing meaningful snapshots is at the heart of any Git workflow. Purposeful, encapsulated commits make it much easier to lớn navigate your project history, figure out where bugs were introduced, và revert changes.
While not exactly an everyday comm&, git stash can be a very convenient tool for creating meaningful commits. It allows you khổng lồ store incomplete work while avoiding the need to commit partial snapshots to your permanent project history. Keep this in mind the next time you wish you could pause whatever you were working on & come bachồng khổng lồ it later.