Changes that happened after the specified commit will remain in the staging area, ready to be committed again. It doesn’t touch the staging area or the working directory. The ‘ git reset -soft‘ option moves the HEAD pointer to a specific commit. ‘git reset’ comes in three main flavors: 1. The ‘git reset’ command can modify the staging area, working directory, and the commit history, depending on the options provided. It moves or alters the “HEAD” pointer in Git, which essentially denotes the current commit your repository is on. In simple terms, ‘git reset’ is a command used to undo changes in a Git repository. This article provides a comprehensive guide on the ‘git reset’ command with practical examples. Despite its utility, it can be a little confusing to understand, especially for beginners. Among its various commands, ‘git reset’ is a powerful one that aids in navigating the project’s history, altering commits, and even discarding changes. Git logo by Jason Long.Git is an indispensable tool for any software developer, providing an efficient way to manage and track changes in source code. She calls the reflog a magic time machine, I certainly agree and hopefully now you do too. If you need more tips for escaping from nightmare git scenarios, check out Oh shit, git! a collection of git tips from Katie Sylor-Miller. 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. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |