![]() ![]() It has an almost Sith-like appeal to it…Īnd what if you do accidentally run reset -hard, losing not only your current changes but also removing commits from your master branch? Well, unless you’ve gotten into the habit of using stash to take snapshots (see next section), there’s nothing you can do to recover your lost working tree. The moral of this story is: although you can do major surgery on your current branch using reset -soft and reset -hard (which changes the working tree too), why would you want to? Git makes working with branches so easy and cheap, it’s almost always worth it to do your destructive modifications on a branch, and then move that branch over to take the place of your old master. Now, Id like to back out those changes by reverse applying the patch (kind of like what git revert would do but against the stash). Ive applied it to my working copy using git stash apply. If you do make changes to new-branch and then decide you want it to become your new master branch, run the following commands: $ git branch -D master # goodbye old master (still in reflog) $ git branch -m new-branch master # the new-branch is now my master 317 I have a small patch saved away in my git stash. It reverts your working tree back to a past state, but on a new branch, so if you decide to commit your changes against the past state, you won’t have altered your original branch.Note that the stash is not branch specific, so you could potentially stash the state of your tree while on one branch, and later apply the differences to another. It saves your work in the stash, which you can come back to at any time.This approach has two distinct advantages if you’re not sure whether you really want to modify the current branch just now: $ git checkout -b new-branch HEAD~3 # head back in time! Fortunately, there is a safer way to achieve the same effect, using the Git stash (see the next section): $ git stash ![]() Thus, the following commands are equivalent: $ git reset -hard HEAD~3 # Go back in time, throwing away changes $ git reset -soft HEAD~3 # Set HEAD to point to an earlier commit $ git reset -hard # Wipe out differences in the working treeĪs you can see, doing a hard reset can be very destructive. Now, if you do a hard reset against an earlier commit, it’s the same as first doing a soft reset and then using reset -hard to reset your working tree. Otherwise, it forces your working tree to match the index. There is also another command, checkout, which operates just like reset -hard if the index is empty. A hard reset (the -hard option) has the potential of being very dangerous, as it’s able to do two different things at once: First, if you do a hard reset against your current HEAD, it will erase all changes in your working tree, so that your current files match the contents of HEAD.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |