Just remember that anyone else who has fetched your pushed changes has them, and will continue to have them and can easily get confused by their presence. You can use git push -f after doing the rewind ( git reset -hard) above. You don't have to delete the remote branch. Update: ah, you've pushed them and you want them gone from the remote repository. You'll have to find or remember that number, d97de0e in the above example, somehow.) (If you decide that despite the bleakness you want them back, you need only give them a name before they expire in roughly 3 months. ![]() The old one, that has the three extra commits, is now a "junk" branch. It's just that they no longer have any names, and if you do some work and git add and git commit it, that will be on a new branch named my_work_branch. * a1d6424 (HEAD, my_work_branch) here's where you started Will show it them to you: $ git log -graph -decorate -pretty=oneline -abbrev-commit d79de0e The commits you added are still there: $ git log d97de0e If you do this, you find that the name my_work_branch has also been moved: $ git rev-parse my_work_branch Or $ git reset -hard HEAD~3 # use at most one of these But, here's the thing: you can also move the name my_work_branch to point to a1d6424 too: $ git reset -hard a1d6424 You can move HEAD elsewhere with any old git checkout. (This is also where the special name HEAD points.) What you have now is that the branch named my_work_branch (the name you gave earlier) "points to" commit d97de0e, which in turn points to 9a3efe3, which points to 9e80936, which points to a1d6424. * 9a3efe3 attempt #2, getting a little hazier $ git commit -m "attempt #3, looking bleak now"Īt this point git log -graph -decorate -pretty=oneline -abbrev-commit might produce something like: * d97de0e (HEAD, my_work_branch) attempt #3, looking bleak now $ git commit -m "attempt #2, getting a little hazier" $ git commit -m "attempt #1, so far so good" Commits that are neither named nor referenced by some other named commit, eventually go away on their own.įor example, suppose you started with: $ git checkout my_work_branch This will keep the history the same, and it will not change the record of the branch’s history.The general idea in git is that you never delete a commit. 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 |