(window.webpackJsonp=window.webpackJsonp||[]).push([[1158],{1566:function(e,t,a){"use strict";a.r(t);var o=a(31),r=Object(o.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"undoing"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#undoing"}},[e._v("#")]),e._v(" Undoing")]),e._v(" "),a("h2",{attrs:{id:"return-to-a-previous-commit"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#return-to-a-previous-commit"}},[e._v("#")]),e._v(" Return to a previous commit")]),e._v(" "),a("p",[e._v("To jump back to a previous commit, first find the commit's hash using "),a("a",{attrs:{href:"http://stackoverflow.com/documentation/git/240/browsing-the-history#t=201607251730514686446",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("git log")]),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("p",[e._v("To temporarily jump back to that commit, detach your head with:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git checkout 789abcd\n\n")])])]),a("p",[e._v("This places you at commit "),a("code",[e._v("789abcd")]),e._v(". You can now make new commits on top of this old commit without affecting the branch your head is on. Any changes can be made into a proper branch using "),a("a",{attrs:{href:"http://stackoverflow.com/documentation/git/415/branching/1633/creating-and-checking-out-new-branches#t=201610192219280544581",target:"_blank",rel:"noopener noreferrer"}},[e._v("either "),a("code",[e._v("branch")]),e._v(" or "),a("code",[e._v("checkout -b")]),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("p",[e._v("To roll back to a previous commit while keeping the changes:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git reset --soft 789abcd\n\n")])])]),a("p",[e._v("To roll back the "),a("strong",[a("strong",[e._v("last")])]),e._v(" commit:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git reset --soft HEAD~\n\n")])])]),a("p",[e._v("To permanently discard any changes made after a specific commit, use:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git reset --hard 789abcd\n\n")])])]),a("p",[e._v("To permanently discard any changes made after the "),a("strong",[a("strong",[e._v("last")])]),e._v(" commit:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git reset --hard HEAD~\n\n")])])]),a("p",[a("strong",[e._v("Beware:")]),e._v(" While you can "),a("a",{attrs:{href:"http://stackoverflow.com/documentation/git/725/recovering/4135/recovering-from-a-reset-or-rebase#t=201607221006140603638",target:"_blank",rel:"noopener noreferrer"}},[e._v("recover the discarded commits using "),a("code",[e._v("reflog")]),e._v(" and "),a("code",[e._v("reset")]),a("OutboundLink")],1),e._v(", uncommitted changes cannot be recovered. Use "),a("a",{attrs:{href:"http://stackoverflow.com/documentation/git/1440/stashing#t=201610192222204220179",target:"_blank",rel:"noopener noreferrer"}},[a("code",[e._v("git stash; git reset")]),a("OutboundLink")],1),e._v(" instead of "),a("code",[e._v("git reset --hard")]),e._v(" to be safe.")]),e._v(" "),a("h2",{attrs:{id:"undoing-changes"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#undoing-changes"}},[e._v("#")]),e._v(" Undoing changes")]),e._v(" "),a("p",[e._v("Undo changes to a file or directory in the "),a("strong",[e._v("working copy")]),e._v(".")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git checkout -- file.txt\n\n")])])]),a("p",[e._v("Used over all file paths, recursively from the current directory, it will undo all changes in the working copy.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git checkout -- .\n\n")])])]),a("p",[e._v("To only undo parts of the changes use "),a("code",[e._v("--patch")]),e._v(". You will be asked, for each change, if it should be undone or not.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git checkout --patch -- dir\n\n")])])]),a("p",[e._v("To undo changes added to the "),a("strong",[e._v("index")]),e._v(".")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git reset --hard\n\n")])])]),a("p",[e._v("Without the "),a("code",[e._v("--hard")]),e._v(" flag this will do a soft reset.")]),e._v(" "),a("p",[e._v("With local commits that you have yet to push to a remote you can also do a soft reset. You can thus rework the files and then the commits.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git reset HEAD~2\n\n")])])]),a("p",[e._v("The above example would unwind your last two commits and return the files to your working copy. You could then make further changes and new commits.")]),e._v(" "),a("p",[a("strong",[e._v("Beware:")]),e._v(" All of these operations, apart from soft resets, will permanently delete your changes. For a safer option, use "),a("code",[e._v("git stash -p")]),e._v(" or "),a("code",[e._v("git stash")]),e._v(", respectively. You can later undo with "),a("code",[e._v("stash pop")]),e._v(" or delete forever with "),a("code",[e._v("stash drop")]),e._v(".")]),e._v(" "),a("h2",{attrs:{id:"undoing-merges"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#undoing-merges"}},[e._v("#")]),e._v(" Undoing merges")]),e._v(" "),a("p",[a("strong",[e._v("Undoing a merge not yet pushed to a remote")])]),e._v(" "),a("p",[e._v("If you haven't yet pushed your merge to the remote repository then you can follow the same procedure as in "),a("a",{attrs:{href:"http://stackoverflow.com/documentation/git/285/undoing/1023/undoing-commits#t=201604121857583862381",target:"_blank",rel:"noopener noreferrer"}},[e._v("undo the commit"),a("OutboundLink")],1),e._v(" although there are some subtle differences.")]),e._v(" "),a("p",[e._v("A reset is the simplest option as it will undo both the merge commit and any commits added from the branch. However, you will need to know what SHA to reset back to, this can be tricky as your "),a("code",[e._v("git log")]),e._v(" will now show commits from both branches. If you reset to the wrong commit (e.g. one on the other branch) "),a("strong",[e._v("it can destroy committed work.")])]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("> git reset --hard \n\n")])])]),a("p",[e._v("Or, assuming the merge was your most recent commit.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("> git reset HEAD~\n\n")])])]),a("p",[e._v("A revert is safer, in that it won't destroy committed work, but involves more work as you have to revert the revert before you can merge the branch back in again (see the next section).")]),e._v(" "),a("p",[a("strong",[e._v("Undoing a merge pushed to a remote")])]),e._v(" "),a("p",[e._v("Assume you merge in a new feature (add-gremlins)")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("> git merge feature/add-gremlins\n...\n #Resolve any merge conflicts\n> git commit #commit the merge\n...\n> git push\n...\n 501b75d..17a51fd master -> master\n\n")])])]),a("p",[e._v("Afterwards you discover that the feature you just merged in broke the system for other developers, it must be undone right away, and fixing the feature itself will take too long so you simply want to undo the merge.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("> git revert -m 1 17a51fd\n...\n> git push\n...\n 17a51fd..e443799 master -> master\n\n")])])]),a("p",[e._v("At this point the gremlins are out of the system and your fellow developers have stopped yelling at you. However, we are not finished just yet. Once you fix the problem with the add-gremlins feature you will need to undo this revert before you can merge back in.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("> git checkout feature/add-gremlins\n...\n #Various commits to fix the bug.\n> git checkout master\n...\n> git revert e443799\n...\n> git merge feature/add-gremlins\n...\n #Fix any merge conflicts introduced by the bug fix\n> git commit #commit the merge\n...\n> git push\n\n")])])]),a("p",[e._v("At this point your feature is now successfully added. However, given that bugs of this type are often introduced by merge conflicts a slightly different workflow is sometimes more helpful as it lets you fix the merge conflict on your branch.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("> git checkout feature/add-gremlins\n...\n #Merge in master and revert the revert right away. This puts your branch in\n #the same broken state that master was in before.\n> git merge master\n...\n> git revert e443799\n...\n #Now go ahead and fix the bug (various commits go here)\n> git checkout master\n...\n #Don't need to revert the revert at this point since it was done earlier\n> git merge feature/add-gremlins\n...\n #Fix any merge conflicts introduced by the bug fix\n> git commit #commit the merge\n...\n> git push\n\n")])])]),a("h2",{attrs:{id:"using-reflog"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#using-reflog"}},[e._v("#")]),e._v(" Using reflog")]),e._v(" "),a("p",[e._v("If you screw up a rebase, one option to start again is to go back to the commit (pre rebase). You can do this using "),a("code",[e._v("reflog")]),e._v(" (which has the history of everything you've done for the last 90 days - this can be configured):")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[a("span",{pre:!0,attrs:{class:"token command"}},[e._v("$ git reflog")]),e._v("\n4a5cbb3 HEAD@{0}: rebase finished: returning to refs/heads/foo\n4a5cbb3 HEAD@{1}: rebase: fixed such and such\n904f7f0 HEAD@{2}: rebase: checkout upstream/master\n3cbe20a HEAD@{3}: commit: fixed such and such\n...\n\n")])])]),a("p",[e._v("You can see the commit before the rebase was "),a("code",[e._v("HEAD@{3}")]),e._v(" (you can also checkout the hash):")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git checkout HEAD@{3}\n\n")])])]),a("p",[a("strong",[e._v("Now you create a new branch / delete the old one / try the rebase again.")])]),e._v(" "),a("p",[e._v("You can also reset directly back to a point in your "),a("code",[e._v("reflog")]),e._v(", but only do this if you're 100% sure it's what you want to do:")]),e._v(" "),a("p",[a("code",[e._v("git reset --hard HEAD@{3}")])]),e._v(" "),a("p",[e._v("This will set your current git tree to match how it was at that point (See Undoing Changes).")]),e._v(" "),a("p",[e._v("This can be used if you're temporarily seeing how well a branch works when rebased on another branch, but you don't want to keep the results.")]),e._v(" "),a("h2",{attrs:{id:"revert-some-existing-commits"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#revert-some-existing-commits"}},[e._v("#")]),e._v(" Revert some existing commits")]),e._v(" "),a("p",[e._v("Use git revert to revert existing commits, especially when those commits have been pushed to a remote repository. It records some new commits to reverse the effect of some earlier commits, which you can push safely without rewriting history.")]),e._v(" "),a("p",[a("strong",[e._v("Don't")]),e._v(" use "),a("code",[e._v("git push --force")]),e._v(" unless you wish to bring down the opprobrium of all other users of that repository. Never rewrite public history.")]),e._v(" "),a("p",[e._v("If, for example, you've just pushed up a commit that contains a bug and you need to back it out, do the following:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git revert HEAD~1\ngit push\n\n")])])]),a("p",[e._v("Now you are free to revert the revert commit locally, fix your code, and push the good code:")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git revert HEAD~1\nwork .. work .. work ..\ngit add -A .\ngit commit -m "),a("span",{pre:!0,attrs:{class:"token string"}},[e._v('"Update error code"')]),e._v("\ngit push\n\n")])])]),a("p",[e._v("If the commit you want to revert is already further back in the history, you can simply pass the commit hash. Git will create a counter-commit undoing your original commit, which you can push to your remote safely.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git revert 912aaf0228338d0c8fb8cca0a064b0161a451fdc\ngit push\n\n")])])]),a("h2",{attrs:{id:"undo-redo-a-series-of-commits"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#undo-redo-a-series-of-commits"}},[e._v("#")]),e._v(" Undo / Redo a series of commits")]),e._v(" "),a("p",[e._v("Assume you want to undo a dozen of commits and you want only some of them.")]),e._v(" "),a("div",{staticClass:"language-git extra-class"},[a("pre",{pre:!0,attrs:{class:"language-git"}},[a("code",[e._v("git rebase -i \n\n")])])]),a("p",[e._v('-i puts rebase in "interactive mode". It starts off like the rebase discussed above, but before replaying any commits, it pauses and allows you to gently modify each commit as it\'s replayed.'),a("strong",[a("code",[e._v("rebase -i")])]),e._v(" will open in your default text editor, with a list of commits being applied, like this:"),a("a",{attrs:{href:"http://i.stack.imgur.com/VHTqM.png",target:"_blank",rel:"noopener noreferrer"}},[a("img",{attrs:{src:"http://i.stack.imgur.com/VHTqM.png",alt:"enter image description here"}}),a("OutboundLink")],1)]),e._v(" "),a("p",[e._v("To drop a commit, just delete that line in your editor. If you no longer want the bad commits in your project, you can delete lines 1 and 3-4 above.If you want to combine two commits together, you can use the "),a("code",[e._v("squash")]),e._v(" or "),a("code",[e._v("fixup")]),e._v(" commands"),a("a",{attrs:{href:"http://i.stack.imgur.com/MV9Xd.png",target:"_blank",rel:"noopener noreferrer"}},[a("img",{attrs:{src:"http://i.stack.imgur.com/MV9Xd.png",alt:"enter image description here"}}),a("OutboundLink")],1)])])}),[],!1,null,null,null);t.default=r.exports}}]);