The commits that you will make after this will be updated in the new branch. Then, point the HEAD to the new branch using ` git checkout `. To create a new branch, simply execute the command ` git branch `. In the example, HEAD now points to the C1 commit. ` git checkout HEAD^^` or ` git checkout HEAD~2`. You can achieve this by using either of these commands: Alternatively, you can also make the HEAD point toward that commit by using the current position of the HEAD itself. One option is to point the HEAD directly to the C1 commit by specifying its SHA value. To do so, you must point the HEAD to the desired commit. Let’s say that you want to create a new branch from commit C1. You can also use ` HEAD` to create a new branch by referencing a particular commit. However, the main branch still points toward the C1 commit. This can be achieved using the ` git checkout` command.Īs you can see, when the new commit was created, the HEAD pointer changed its reference and now points toward the latest commit, C2. Here, the HEAD is not referencing or pointing to the main branch instead, it is pointing directly to the C1 commit. In the image above, the HEAD is pointing to the main branch and the main branch is pointing to the C1 commit. To better understand this, consider an example: This state is known as detached HEAD state. However, it can also point directly to a commit. ![]() Thus, the ` HEAD` pointer indirectly points to a particular commit. ![]() It is like a pointer that points to a branch, and that branch itself points to a particular commit. ` HEAD` is primarily a reference to a named branch in a Git repository. Reflogs are useful in various Git commands, to specify the old value of a reference.”īefore diving further into the `git reflog` command, let’s take a look at some basics of Git. “ Reference logs, or “reflogs,” record when the tips of branches and other references were updated in the local repository. This command is used to get a record of references to the tips of branches and commits that have been updated. In this tutorial, you will learn how to recover your deleted commits or branches using the ` git reflog` command. It could get even worse if you are working with a bunch of other developers, as it’s easy to modify or even completely remove another person’s work, whether knowingly or inadvertently. Issues ranging from merge conflicts to accidentally deleting branches are a nightmare for many developers. Voilà: you now have a new branch named "old-project-state" reflecting the old version of your project - without touching or even removing any other commits or branches.Managing branches or commits in Git can be quite cumbersome. If you don't want it to start at the current HEAD revision, you also need to provide a commit hash - the old project revision we want to restore. However, providing the -b parameter, you can also let it create a new branch (named "old-project-state" in this example). Normally, the checkout command is used to just switch branches. Since "branches" are so cheap and easy in Git, we can easily create a new branch which starts at that old revision: $ git checkout -b old-project-state 0ad5a7a6 However, there is also a "safer" way in case you'd prefer leaving your current HEAD branch untouched. If you're sure that this is what you want, everything is fine. And in case you made a mistake: simply hit CMD+Z to undo the reset and restore the removed commits! Restoring a Revision in a New Local BranchĪs said, using the reset command on your HEAD branch is a quite drastic action: it will remove any commits (on this branch) that came after the specified revision. In case you are using the Tower Git client, you can use the reset command right from a commit's contextual menu. You'll be left with a couple of changes in your working copy and can then decide what to do with them. If you use it instead of -hard, Git will keep all the changes in those "undone" commits as local modifications: $ git reset -soft 0ad5a7a6 The reset command comes with a couple of options, one of the more interesting ones being the "-soft" flag. All commits that came after this version are effectively undone your project is exactly as it was at that point in time. ![]() This will rewind your HEAD branch to the specified version. The fastest way to restore an old version is to use the "reset" command: $ git reset -hard 0ad5a7a6 Download Now for Free Returning to an Old Revision
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |