{"id":220,"date":"2016-08-18T22:21:04","date_gmt":"2016-08-19T02:21:04","guid":{"rendered":"https:\/\/mberlove.com\/blog\/?p=220"},"modified":"2016-08-18T22:21:04","modified_gmt":"2016-08-19T02:21:04","slug":"todays-little-git-adventure","status":"publish","type":"post","link":"https:\/\/mberlove.com\/blog\/todays-little-git-adventure\/","title":{"rendered":"Today&#8217;s Little Git Adventure"},"content":{"rendered":"<p><strong>Today&#8217;s lesson: pull before you make changes, and what do you when you forget that.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The task started off well enough &#8212; branched off of master, made my changes, committed them. Ready to send my content upstream as a new branch to share. But wait &#8212; my master branch wasn&#8217;t up to date when I branched. I can pull from origin now, after the fact &#8212; it&#8217;s no big deal in this case, no conflicts, very clean, but now the merge is the latest commit, which is not how I want to push. Much better for my committed changes to be the first in the log.<\/p>\n<p>&nbsp;<\/p>\n<p>The first steps in turning that around were fairly normal:<\/p>\n<p>&nbsp;<\/p>\n<pre>git reset --hard &lt;my original commit&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Ok, that&#8217;s just to make sure I&#8217;m back with my original changes as the most recent thing. But I still have to get the up-to-date content. Turns out there&#8217;s a simple way <a href=\"http:\/\/stackoverflow.com\/questions\/15772134\/can-i-delete-a-git-commit-but-keep-the-changes\">to undo a commit but keep the changes<\/a>. So now I run this:<\/p>\n<p>&nbsp;<\/p>\n<pre>git reset HEAD^<\/pre>\n<p>&nbsp;<\/p>\n<p>Awesome. Back where I started, just as if I hadn&#8217;t made any commits in the first place.<\/p>\n<p>Alright, <em>now <\/em>I can pull&#8230;.<\/p>\n<p>&nbsp;<\/p>\n<pre>git pull origin master<\/pre>\n<p>&nbsp;<\/p>\n<p>Ahhhh. Much bet&#8211;wait, still not quite right. It&#8217;s much better to update master and then branch off of that. Ok, here we go.<\/p>\n<p>&nbsp;<\/p>\n<pre>git checkout master\r\n\r\ngit pull origin master<\/pre>\n<p>&nbsp;<\/p>\n<p>Now we&#8217;re on track here; what&#8217;s next?<\/p>\n<p>&nbsp;<\/p>\n<pre>git branch -d &lt;my new branch&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Ew. Maybe technically unnecessary, but so very clean.<\/p>\n<p>&nbsp;<\/p>\n<pre>git checkout -b &lt;that same branch&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Hah, bring the branch back, but the right way this time. And my pending changes are still hovering around my branch as I switch around, waiting to be committed! So now I can do that very thing.<\/p>\n<p>&nbsp;<\/p>\n<pre>git commit -am \"All better now, etc, etc.\"<\/pre>\n<p>&nbsp;<\/p>\n<p>And <em>now<\/em> I push.<\/p>\n<p>&nbsp;<\/p>\n<pre>git push origin &lt;that branch again&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>\ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p><em>Note: As I&#8217;ve mentioned before, I am far from an expert on Git. I just use my experiences every day, and a healthy dollop of googling and reading, to learn about how things work and how to get stuff done. I share what I learn for fun and to make everyone&#8217;s day a bit easier, but I&#8217;m sure on this topic in particular there&#8217;s likely a better way to solve the same problem. So feel free to come up with a better solution! I&#8217;d love to hear about it if you do.<\/em><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s lesson: pull before you make changes, and what do you when you forget that. &nbsp; The task started off well enough &#8212; branched off of master, made my changes, committed them. Ready to send my content upstream as a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109,125,108,1],"tags":[113,148,37,70],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-learning","category-programming","category-tools","category-uncategorized","tag-development","tag-git","tag-learning","tag-programming"],"_links":{"self":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":5,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions\/225"}],"wp:attachment":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}