{"id":3646,"date":"2017-01-03T21:38:34","date_gmt":"2017-01-03T21:38:34","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3646"},"modified":"2017-10-19T03:25:17","modified_gmt":"2017-10-19T03:25:17","slug":"git-how-to-replace-your-master-branch-with-another-branch","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/git-how-to-replace-your-master-branch-with-another-branch\/","title":{"rendered":"[GIT] How to replace the master branch with another branch."},"content":{"rendered":"<p>TL;DR;<br \/>\n<code>git checkout master<br \/>\ngit reset --hard fixed-master-branch<br \/>\ngit push origin master -f<\/code><\/p>\n<p>Sometimes someone may pollute the &#8216;master&#8217; branch and you may have noticed this only after you&#8217;ve fetched, rebased and pushed your commits.<br \/>\nSo your history may end up like this (I&#8217;ll use decimal numbers on the commit IDs for illustrative purposes only):<\/p>\n<p><code>commit: 87<br \/>\nAuthor: me<br \/>\nA good commit<\/p>\n<p>commit: 86<br \/>\nAuthor: other guy<br \/>\nMerge: that brought back a lot of crap because they probably used some GUI based git client and they did a git pull without your permission<\/p>\n<p>commit: 85<br \/>\nAuthor: other guy<br \/>\nAnother commit that shouldn't be here<\/p>\n<p>commit: 84<br \/>\nAuthor: me<br \/>\nAnother the last good commit<\/code><\/p>\n<p>Now, your master is all effed up and you&#8217;re about to have an anxiety attack. What you want is to have this in your git log.<\/p>\n<p><code>commit: 87<br \/>\nAuthor: me<br \/>\nA good commit<\/p>\n<p>commit: 84<br \/>\nAuthor: me<br \/>\nAnother the last good commit<\/code><\/p>\n<p>To get back to this state is pretty easy:<\/p>\n<ol>\n<li>Check out the last good commit<\/li>\n<\/ol>\n<p><code>git checkout 84<\/code><\/p>\n<p>Now you will be detached from master, let&#8217;s name this temporary branch &#8216;fixed-master-branch&#8217;<\/p>\n<p><code>git checkout -b fixed-master-branch<\/code><\/p>\n<p>Now the last commit you should see should be commit 84.<\/p>\n<p>Let&#8217;s now bring our commit, and skip the 2 sucky commits<\/p>\n<p><code>git cherry-pick 87<\/code><\/p>\n<p>Now your &#8216;fixed-master-branch&#8217; branch looks exactly how you want the master to look like.<\/p>\n<p>But now, you want to replace the old master with the new one.<\/p>\n<p>Go back to the broked master<\/p>\n<p><code>git checkout master<\/code><\/p>\n<p>And fix it, and fuck all that merge &#8211;strategy ours BS advice you will find on StackOverflow as it will leave the git log history really weird.<\/p>\n<p><code>git reset --hard fixed-master-branch<\/code><\/p>\n<p>Now when you look at your history, master will be the same as &#8216;fixed-master&#8217;, and to fix your remote repo, you will have to make a push &#8211;force and let everyone know that you did that because you needed to clean your repository&#8217;s history<\/p>\n<p><code>git push origin master -f<\/code><\/p>\n<p>You&#8217;re done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR; git checkout master git reset &#8211;hard fixed-master-branch git push origin master -f Sometimes someone may pollute the &#8216;master&#8217; branch and you may have noticed this only after you&#8217;ve fetched, rebased and pushed your commits. So your history may end up like this (I&#8217;ll use decimal numbers on the commit IDs for illustrative purposes only): [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_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":[15],"tags":[484,533,1563],"class_list":["post-3646","post","type-post","status-publish","format-standard","hentry","category-code","tag-git","tag-howto","tag-replace-branch"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-WO","jetpack-related-posts":[{"id":3055,"url":"https:\/\/www.gubatron.com\/blog\/my-git-cheat-sheet\/","url_meta":{"origin":3646,"position":0},"title":"My Git Cheat-sheet","author":"gubatron","date":"May 28, 2013","format":false,"excerpt":"I don't know if it's the crazy syntax, but for the life of me, I always need to come back to this cheat sheet, maybe you will too: GIT CHEATSHEET fetch remote branch. git fetch origin nameofbranch \"fetch\" downloads the changes of the remote branch but doesn't automatically merge them.\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2016-01-02-at-4.40.11-PM.png?fit=1200%2C696&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2016-01-02-at-4.40.11-PM.png?fit=1200%2C696&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2016-01-02-at-4.40.11-PM.png?fit=1200%2C696&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2016-01-02-at-4.40.11-PM.png?fit=1200%2C696&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2016-01-02-at-4.40.11-PM.png?fit=1200%2C696&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3150,"url":"https:\/\/www.gubatron.com\/blog\/how-to-add-an-existing-git-repository-to-github\/","url_meta":{"origin":3646,"position":1},"title":"How to add an existing GIT repository to github.","author":"gubatron","date":"August 31, 2013","format":false,"excerpt":"Most of the times, it makes more sense to start working on something that slowly transforms into the beginning of a project that deserves to be on github. This post is about creating a local repository and putting it on github. 1. First we must convert the main local folder\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4122,"url":"https:\/\/www.gubatron.com\/blog\/screw-configuring-gpg-to-sign-git-commits-do-it-with-your-same-ssh-key-like-this\/","url_meta":{"origin":3646,"position":2},"title":"Screw configuring GPG to sign git commits, do it with your same SSH key like this","author":"gubatron","date":"January 30, 2025","format":false,"excerpt":"GPG gives a lot of shit on macos for some reason. It's best to just add the same SSH authentication key you have on github as a \"Signing Key\" in your github account signing configuration. Go to Settings > SSH and GPG Keys You can then configure your github client\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/01\/ssh_sign.jpg?fit=736%2C441&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/01\/ssh_sign.jpg?fit=736%2C441&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/01\/ssh_sign.jpg?fit=736%2C441&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/01\/ssh_sign.jpg?fit=736%2C441&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":4178,"url":"https:\/\/www.gubatron.com\/blog\/mining-git-history-to-build-developer-agent-personas\/","url_meta":{"origin":3646,"position":3},"title":"Mining Git History to Build Developer Agent Personas","author":"gubatron","date":"February 22, 2026","format":false,"excerpt":"A new software engineering practice for the age of agentic teams and an honest look at what it costs There is a new kind of software team forming inside repositories everywhere. It is not made of humans alone. It is made of humans and AI agents working together, agents that\u2026","rel":"","context":"In &quot;AI&quot;","block_context":{"text":"AI","link":"https:\/\/www.gubatron.com\/blog\/category\/ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":2068,"url":"https:\/\/www.gubatron.com\/blog\/bitbucket-or-github\/","url_meta":{"origin":3646,"position":4},"title":"Bitbucket or Github?","author":"gubatron","date":"September 28, 2010","format":false,"excerpt":"This week we have to make a final choice of what technology will be used to replace our somewhat big subversion repository (2 Gigs). Huge imports are beginning to be a pain and we're tired of the .svn folders and not being able to commit if the central server is\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3313,"url":"https:\/\/www.gubatron.com\/blog\/solved-sublime-text-2-git-binary-could-not-be-found-in-path\/","url_meta":{"origin":3646,"position":5},"title":"[SOLVED] Sublime Text 2: Git binary could not be found in PATH","author":"gubatron","date":"July 22, 2014","format":false,"excerpt":"Got this annoying dialog popping up on Sublime Text 2? Go to Preferences > Browse Packages ... a Finder window will open, go to the \"Git\" folder, open the file called \"Git.sublime-settings\" Look for \"git_command\" and set it's value to the path of your git executable (you can find the\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"Screen Shot 2014-07-22 at 4.31.03 PM","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/07\/Screen-Shot-2014-07-22-at-4.31.03-PM.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3646"}],"version-history":[{"count":3,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3646\/revisions"}],"predecessor-version":[{"id":3650,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3646\/revisions\/3650"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}