AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Undo git stash9/23/2023 How will you revert the commit made by git cherry-pick command now? However, it is possible that you realise you have used the cherry-pick command, but suddenly want to undo it. On the other hand, if you first collapse all the commits made in that separate branch into one single commit, you can use the git cherry-pick command afterwards to move that commit to your master branch. Why is it preferable to git merge, you may ask? As it appears to be, git merge clutters your master branch’s history by pushing in all the commits made in the separate branch. For instance, instead of using the git merge command to migrate commits from a different branch to the master branch, you can use the git cherry-pick command instead. This command gives rise to a different approach to git merge, and at times is used as an alternative for it. 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 cherry-pick command in git allows you to move a group of commits from a given branch to another branch. 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. 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 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
Read More
Leave a Reply. |