Let's say that after looking through this commit history you wanted to reset to the "second commit". To reset your local repository, first you need to know which commit you want to reset to, and what the hash is for that commit. So, after four commits I ended up with a README that looked like this: To simplify the example, I created a repository with a README in it, and for each commit, I added a line that matches the commit message. This means that any uncommitted work in the Staging Index or Work Directory will be lost. Just make sure you want drop the commits in question before you do the reset.Īlso, just as a quick reminder, git reset -hard will move the HEAD pointer back to the commit hash that was given, the Stage Index and the Working Directory will be reset back to the way they were at that commit, and all commits after the commit that was given will be dropped from the commit history. In this case, you're the only one affected by the hard reset, so it's relatively safe to do. If another developer pulls and starts basing their work on the commits that the reset removes from the commit history, you're going to create huge problems.Ī hard reset can be done if you're the only one using the local and remote repository, or if the reset removes commits that have not yet been pushed to a shared remote. This will keep the history the same, and it will not change the record of the branch’s history.Before we go over how to do this, let's go over when you should and should not do this.ĭon't do a hard reset if you've already pushed the commits in question to a shared remote repository. You could also make a new commit that signifies exactly the same state of the venture as f414f31.Also, the commits you did after f414f31 will no longer be included in the history of your master branch. Yet, this is rewriting the history of your branch, so if you share your branch with anyone it is best to not use this option. This can be done with git reset –hard f414f31. You could make your current branch to point to the older commit instead.(You can find that via git log or by viewing any history browser.) You then have a few different choices dependent on what you would like to do: Here is another example, presume that the good commit that you want to go back to is f414f31. HEAD shows your current branch, or current commit, meaning that all git reset –hard HEAD will do is to throw away all of the uncommitted changes you have. Then it will make the files in the working tree and the index (“staging area”) the same as the versions committed in.Make your present branch (typically master) back to point at. When you want to revert to a past commit using git reset – – hard, add. For example, if you do git add to stage a newer version of the file, this will override the past staged version of the file in the staging area. But be sure to note that Git does not take track of changes to your files. When you have created a commit, which will have your project files stored in a particular state, they ae safe. Git records the state of the files when you stage them with git add or when you make a commit. Be sure to always double check that the output of git status is empty (clean) before you begin using it. Luckily there is an easy solution to follow to correct this.įirst of all git reset - hard is a very dangerous command because it eliminates all of your noncommitted changes. A question that comes up with developers is how to use git reset– hard HEAD to revert to previous commit.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |