{"id":54680,"date":"2022-02-02T10:16:39","date_gmt":"2022-02-02T04:46:39","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=54680"},"modified":"2022-02-02T10:16:39","modified_gmt":"2022-02-02T04:46:39","slug":"a-classic-mistake-faced-by-every-git-user","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/a-classic-mistake-faced-by-every-git-user\/","title":{"rendered":"A classic mistake faced by every git user"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Learning from mistakes will teach us a lifelong lesson, similarly here we are discussing a classic mistake that every git user made at least once i.e. committing code in the wrong branch.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If you identify the mistake at the right time this can be easily managed, but it gets more tricky if you start committing code in the wrong branch. Let\u2019s take a problem statement to understand the issue more clearly.<\/span><\/p>\n<p><b>Problem Statement<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You are working on a new feature and you forgot to change a new branch for it. You\u2019ve already done lots of work and committed in the master branch, now you\u2019ve realized you need to switch to a new feature branch keeping all work secure that you already committed in the master branch.<\/span><\/p>\n<p><b>Solution<\/b><\/p>\n<p><span style=\"font-weight: 400;\">git stash is the solution provided by git to prevent us from this mess. Follow the commands below to switch to the correct branch without losing your committed code.<\/span><\/p>\n<pre>$ git checkout new-feature<\/pre>\n<p><span style=\"font-weight: 400;\">If this succeeds, no need to worry, just commit your changes and push the changes in the new branch. In case the above command raises an error like the one below<\/span><\/p>\n<pre>error: Your local changes to the following files would be overwritten....<\/pre>\n<p><span style=\"font-weight: 400;\">Then execute the command below<\/span><\/p>\n<pre>$ git stash<\/pre>\n<p><span style=\"font-weight: 400;\">This command safely stores your changes in a local repository, and now you can switch your<\/span><span style=\"font-weight: 400;\"> branch without worries.<\/span><\/p>\n<pre>$ git checkout new-feature\r\n$ git stash apply<\/pre>\n<p><span style=\"font-weight: 400;\">Now you can see all your changes in the new feature branch. If everything goes well make sure to execute <\/span><em><span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\"> git stash drop<\/span><\/em><span style=\"font-weight: 400;\">, and it\u2019ll drop the reference of the branch created in your local repository.<\/span><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Learning from mistakes will teach us a lifelong lesson, similarly here we are discussing a classic mistake that every git user made at least once i.e. committing code in the wrong branch.\u00a0 If you identify the mistake at the right time this can be easily managed, but it gets more tricky if you start committing [&hellip;]<\/p>\n","protected":false},"author":1436,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1],"tags":[375],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54680"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1436"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=54680"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54680\/revisions"}],"predecessor-version":[{"id":54682,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54680\/revisions\/54682"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=54680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=54680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=54680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}