AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Git restore head8/6/2023 ![]() ![]() There is an audio recording of his talk available and a much more detailed article about git refs and the reflog on Atlassian’s tutorial site. I’d like to thank Steve Smith for the talk he gave at Codemotion Berlin where I learned this trick. Knowing I have this trick up my sleeve has made me more confident using git. As with most things in git once you learn about the feature you can use it to your advantage. The git reflog keeps a track of everything we get up to with git. Now we have reset the branch to the state we were in before, no work has been lost, breathe a sigh of relief. The reflog now shows the four actions that we took two commits and then two resets. You can always reset using hashes, in fact HEAD~1 is really just a reference to a hash. This time we reset using the hash of our lost commit. So, how do we get back to the last state before we reset? We can reset again. Note how the reset points to the same hash as the original commit. Git log shows only one commit, but git reflog shows three actions two commits and one reset. What happens if we git reset -hard HEAD~1? Now we make a commit, something important of course. Running git log -oneline and git reflog both show the commit has the hash 2daf3ba. Here’s an example, I have a repo with one commit. Be warned! How to use the reflog to recover lost commits The following will save only work that you have committed. ![]() If you have uncommitted changes in the working directory and you use git reset -hard no amount of fancy git knowledge about the reflog is going to get that back. Warning!īefore I show you how this works, please note the following. It has a log of all commits made in the repo, as well as other actions, and we can use it to recover these lost commits. This is where the reflog comes into play. The key is that the commits still exist, there just aren’t any branches that currently point to them. Even if we were to remove a whole bunch of work by resetting with the -hard flag, git still knows about the commits we had made and we can recover them. When we make changes to any branch, git stores those changes in the reflog. It turns out that git is watching us closer than we may think. ![]() I know I’ve mixed the two up and lost work. The difference between the -soft and -hard flag is one of those ways you can shoot yourself in the foot. Git reset -hard HEAD~1 removes the commit and all the work. Git reset -soft HEAD~1 undoes one commit and leaves the work from that commit still present in the working directory. You can turn back the clock and undo commits. Git reset is useful when you’ve done something wrong. Recently, however, I learned of one way that you can unshoot yourself and potentially save yourself hours of lost work. Then finally commit using $ git commit, thus, you can undo published commits.Git may be the best version control software I’ve used but it is a complex beast and makes it easy to shoot yourself in the foot. You can get your index and work tree into the desired state, without changing HEAD You can give the reverted commits by giving ranges. Undoing published commits can be done by using revert command. If you already published the commits then you need to undo the published commits with new commits. This saves the modification, then re-applies that patch after resetting. Also, you can stash, pop if there is a work to save If you need to reset all mess that you have created during revert useĭo only if you have committed the work that you need further. You could reset to throw them away, you could commit them to a branch there if you want a branch there.) (If you've made changes, when switching branches, you'll have to deal with them as appropriate. To go back, you just check out the branch that you were on again. ![]() $ git checkout -b (ie - $git checkout -b master ) If you need to make commits, go ahead and make a new branch If you need to switch temporarily to a committed branchĪnd it should be the id of the commit that you need to revert. ![]()
0 Comments
Read More
Leave a Reply. |