{"id":997,"date":"2014-04-11T11:12:10","date_gmt":"2014-04-11T18:12:10","guid":{"rendered":"http:\/\/10kdev.net\/?p=997"},"modified":"2014-04-11T11:22:06","modified_gmt":"2014-04-11T18:22:06","slug":"backing-out-git-commits-with-reset-and-stash","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=997","title":{"rendered":"Backing Out Git Commits With <br\/>Reset And Stash"},"content":{"rendered":"<p>Our team had some of us working on master in our git repository, and a refactor was done in a branch so the team lead decided we should all move to from the master to the branch because the merge was painful for this particular feature. \u00a0If you have this situation and a few commits in the wrong branch here&#8217;s a method that might help out using git reset and git stash. \u00a0<em>Reset<\/em> can undo your last commit, and <em>stash<\/em> can save it off-stream to retrieve whenever you wish.<\/p>\n<p>For this example I have several commits, not pushed, \u00a0in the local repository already. \u00a0Looking in Tortoise Git log you can see this state \u00a0is three commits ahead of origin\/HEAD.<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" alt=\"\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2014\/04\/4f491335b5837637b9e4985ef53f3ca0.png\" width=\"427\" height=\"116\" \/><\/p>\n<p>The task here is to \u00a0back out the commits from master, save them, then recommit them into the branch. \u00a0There are several ways to accomplish this in git but I am showing the revert-stash methodology.<\/p>\n<p>Before starting the process, my recommonedation (that you do not need to follow) is to make sure your stash was clear &#8212; \u00a0I do so that I can just issue a series of pops to get back to my starting point easily on branch without worrying about mistakes.<\/p>\n<p><code>git stash clear<\/code><\/p>\n<p>Then clean up any other code\/work in progress so that <em>git status<\/em>\u00a0shows clean.<\/p>\n<p>Next start backing out the commits back to the original HEAD. \u00a0Each commit should be backed and saved atomically as a separate entry to your stash. \u00a0For instance in this example, start at Commit 3 and work back to Commit 1.<\/p>\n<p><code> git reset HEAD~1 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #339966;\">\/\/back out current top commit; 1 every time<\/span><br \/>\ngit status \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span style=\"color: #339966;\">\/\/check and see what files you have to stash<\/span><br \/>\ngit add -A :\/ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #339966;\">\/\/stage all the files listed<\/span><br \/>\ngit stash save Commit# <span style=\"color: #339966;\">\/\/save the commit to whatever name you choose<\/span><br \/>\ngit status \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<span style=\"color: #339966;\">\/\/a good place to check status and Tortoise log<\/span><\/code><\/p>\n<p><em><strong>&#8230;.repeat.<\/strong><\/em><\/p>\n<p>Now when you do\u00a0<em>git stash list<\/em> you can see all the commits ready for moving to the branch, and Tortoise shows master at \u00a0origin\/HEAD so you know that all changes were removed.<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" alt=\"\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2014\/04\/7c4164dcf3039fd89d95e868a7316c0d.png\" width=\"240\" height=\"44\" \/><\/p>\n<p>Time to switch branches.<\/p>\n<p>Issue a <em>git branch -a<\/em> to list all the remotes as well. Switch to your branch:<\/p>\n<p><code>git checkout remotebranch<\/code><\/p>\n<p>Now you can just pop your first stash right off the stack, confirm it works, check it in and move onto the next stash item:<\/p>\n<p><code>git stash list \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #339966;\">\/\/just checking<\/span><br \/>\ngit stash pop \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<span style=\"color: #339966;\">\/\/takes first one off list and deletes from stash list<\/span><br \/>\ngit add -A :\/ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<span style=\"color: #339966;\">\/\/stage everything<\/span><br \/>\n<span style=\"color: #339966;\">\/\/--&gt; do some merging\/work as needed<\/span><br \/>\nmvn clean install \u00a0 \u00a0 \u00a0 <span style=\"color: #339966;\">\/\/whatever build process you do to test and check<\/span><br \/>\ngit commit -c \"Commit#\" <span style=\"color: #339966;\">\/\/commit your code<\/span><br \/>\ngit status \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #339966;\">\/\/always check your state!!<\/span><\/code><\/p>\n<p><em><strong>&#8230;.repeat.<\/strong><\/em><\/p>\n<p>Now sync it up with remotebranch HEAD:<\/p>\n<p><code>git pull --rebase <span style=\"color: #339966;\">\/\/just checking that I'm up to date<\/span><br \/>\ngit push \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #339966;\">\/\/send those commits to the homeworld<\/span><\/code><\/p>\n<p>All done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our team had some of us working on master in our git repository, and a refactor was done in a branch so the team lead decided we should all move to from the master to the branch because the merge was painful for this particular feature. \u00a0If you have this situation and a few commits [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/997"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=997"}],"version-history":[{"count":13,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/997\/revisions"}],"predecessor-version":[{"id":1010,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/997\/revisions\/1010"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}