{"id":2123,"date":"2015-02-27T23:48:54","date_gmt":"2015-02-27T23:48:54","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2123"},"modified":"2015-03-06T19:24:51","modified_gmt":"2015-03-06T19:24:51","slug":"common-git-screwupsquestions-solutions","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/02\/common-git-screwupsquestions-solutions\/","title":{"rendered":"Most common git screwups\/questions and solutions"},"content":{"rendered":"<p>I was looking to learn a bit more about the parts of git I&#8217;ve not ventured into yet. What better way that looking the most common ways people screw them up and how to fix the resulting problems! Here&#8217;s a short list, compiled from my own experience and issues I&#8217;ve come across on the Internet.<\/p>\n<h2 id=\"wrongcommitmessage\">I wrote the wrong thing in a commit message<\/h2>\n<p>If the commit hasn&#8217;t been push you can do the following, which will allow you to edit the message on the most recent commit:<\/p>\n<pre>\r\ngit commit --amend\r\n<\/pre>\n<h2 id=\"undolastcommit\">How can I undo the last commit?<\/h2>\n<p>You can use git reset e.g.:<\/p>\n<pre>\r\ngit reset --hard HEAD~1\r\n<\/pre>\n<p>HEAD~1 means HEAD-1 commit. It should be noted that this is the nuclear option, and any changes you made will be discarded. If you want to keep your changes in the working tree use:<\/p>\n<pre>\r\ngit reset --soft HEAD~1\r\n<\/pre>\n<p>If you&#8217;ve already published your commits, you should use revert. This is create new commits undoing the last change:<\/p>\n<pre>\r\ngit revert HEAD~1..HEAD\r\ngit revert commitid\r\n<\/pre>\n<h2 id=\"deletebranch\">Delete a Git branch remotely<\/h2>\n<pre>\r\ngit push origin --delete branchname\r\n<\/pre>\n<h2 id=\"gitpullvfetch\">What are the differences between &#8216;git pull&#8217; and &#8216;git fetch&#8217;?<\/h2>\n<p>git pull, is git fetch followed by git merge. git fetch gets the remote changes, they get kept under refs\/remotes\/<remote>\/. However it doesn&#8217;t affect your local branches, and won&#8217;t change your working copy. git pull then merges these changes with the local copy.<\/p>\n<h2 id=\"undoadd\">How do I undo a &#8216;git add&#8217; before committing<\/h2>\n<p>You did a &#8220;git add filename&#8221; accidentally and want to undo it before committing your change. You can simply do:<\/p>\n<pre>\r\ngit reset filename\r\n<\/pre>\n<p>To unstage your changes to that file.<\/p>\n<h2 id=\"mergeconflicts\">How do I deal with merge conflicts<\/h2>\n<p>Use &#8220;git mergetool&#8221; which gives a handy interface for solving merge conflicts.<\/p>\n<h2 id=\"removelocaluntracked\">Remove all local untracked files (and directories) from your local clone<\/h2>\n<p>Careful! You might want to take a backup before doing this:<\/p>\n<pre>\r\ngit clean -f -d\r\n<\/pre>\n<h2 id=\"cloneallremote\">Clone all remote branches<\/h2>\n<p>You probably already have, they&#8217;re just hiding! Use the following to see all the branches:<\/p>\n<pre>\r\ngit branch -a\r\n<\/pre>\n<p>You can then use &#8220;git checkout origin\/branchname&#8221; to take a look at the branch. Or &#8220;git checkout -b branchname origin\/branchname&#8221;. To create a local tracking branch.<\/p>\n<h2 id=\"renamelocalbranch\">Rename local branch?<\/h2>\n<pre>git branch -m oldname newname<\/pre>\n<h2 id=\"reverting\">Revert to a previous Git commit<\/h2>\n<p>You can use reset as above to revert to a previous commit, this assumes you mean go back to where you were before permanently rather than just take a look (for that you want to checkout an old version). The commit ID, should be as shown in the output of &#8220;git log&#8221;.  <\/p>\n<pre>git reset --hard commitid<\/pre>\n<p>Again this will discard all changes in your working tree, so make sure this is really what you want to do! Or look at using &#8211;soft rather than &#8211;hard.<\/p>\n<h2 id=\"removesubmodule\">Remove a git submodule<\/h2>\n<p>Creating a submodule is pretty straight-forward, but deleting them less so the commands you need are:<\/p>\n<pre>\r\ngit submodule deinit submodulename   \r\ngit rm submodulename\r\ngit rm --cached submodulename\r\nrm -rf .git\/modules\/submodulename\r\n<\/pre>\n<h2 id=\"overwritelocalfiles\">Over-write local files when doing a git pull<\/h2>\n<p>Git reset is your friend again:<\/p>\n<pre>\r\ngit fetch --all\r\ngit reset --hard origin\/master\r\n<\/pre>\n<h2 id=\"addemptydirectory\">How can I add an empty directory to my repository?<\/h2>\n<p>You can&#8217;t! git doesn&#8217;t support this, but there&#8217;s a hack. You can create a .gitignore file in the directory with the following contents:<\/p>\n<pre>\r\n# Ignore everything in this directory\r\n*\r\n# Except this file\r\n!.gitignore\r\n<\/pre>\n<p>I don&#8217;t believe it actually matters what&#8217;s in the .gitignore (and this .gitignore will ignore all files).<\/p>\n<h2 id=\"exportsource\">git export, exporting the source code as with &#8220;svn export&#8221;<\/h2>\n<p>Use git archive e.g:<\/p>\n<pre>\r\ngit archive --format zip --output \/full\/path\/to\/zipfile.zip master \r\n<\/pre>\n<h2 id=\"discardchanges\">Discard all my unchecked in changes<\/h2>\n<pre>\r\ngit checkout -- .\r\n<\/pre>\n<h2 id=\"createnewremotebranch\">Create a new remote branch from the current local one<\/h2>\n<pre>\r\ngit config --global push.default current\r\ngit push -u\r\n<\/pre>\n<h2 id=\"restoredeletedfile\">Restore a deleted file<\/h2>\n<p>First find the commit when the file last existed:<\/p>\n<pre>\r\ngit rev-list -n 1 HEAD -- filename\r\n<\/pre>\n<p>Then checkout that file<\/p>\n<pre>\r\ngit checkout deletingcommitid^ -- filename\r\n<\/pre>\n<h2 id=\"revertsinglefile\">Revert a single file to a specific previous commit<\/h2>\n<p>Similar to the above, but a bit simpler:<\/p>\n<pre>\r\ngit checkout commitid filename\r\n<\/pre>\n<h2 id=\"ignorefilepermissions\">Make git ignore permissions\/filemode changes<\/h2>\n<pre>\r\ngit config core.fileMode false\r\n<\/pre>\n<h2 id=\"rememberpasswdhttps\">Get git to remember my password when checking out with https<\/h2>\n<p>It&#8217;ll only remember your password for 15mins:<\/p>\n<pre>\r\ngit config --global credential.helper cache\r\n<\/pre>\n<p>You can make it remember your password longer with:<\/p>\n<pre>\r\ngit config --global credential.helper \"cache --timeout=XXXX\"\r\n<\/pre>\n<h2 id=\"peekatoldrev\">Take a quick look at an old revision of a single file<\/h2>\n<pre>\r\ngit show commitid:filename\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was looking to learn a bit more about the parts of git I&#8217;ve not ventured into yet. What better way that looking the most common ways people screw them up and how to fix the resulting problems! Here&#8217;s a short list, compiled from my own experience and issues I&#8217;ve come across on the Internet. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2123","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-yf","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2123"}],"version-history":[{"count":11,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2123\/revisions"}],"predecessor-version":[{"id":2214,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2123\/revisions\/2214"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}