{"id":82013,"date":"2020-10-05T15:00:22","date_gmt":"2020-10-05T12:00:22","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-git-rebase-and-how-is-it-different-than-merging-cloudsavvy-it\/"},"modified":"2020-10-05T15:00:22","modified_gmt":"2020-10-05T12:00:22","slug":"what-is-git-rebase-and-how-is-it-different-than-merging-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-git-rebase-and-how-is-it-different-than-merging-cloudsavvy-it\/","title":{"rendered":"#What is Git Rebase and How Is it Different than Merging? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2eba5c36b79\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2eba5c36b79\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-git-rebase-and-how-is-it-different-than-merging-cloudsavvy-it\/#Whats_The_Deal_With_Rebasing\" >What\u2019s The Deal With Rebasing?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-git-rebase-and-how-is-it-different-than-merging-cloudsavvy-it\/#Do_Not_Rebase_Shared_Branches\" >Do Not Rebase Shared Branches<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-git-rebase-and-how-is-it-different-than-merging-cloudsavvy-it\/#When_Is_Rebasing_Better_Than_Merging\" >When Is Rebasing Better Than Merging?<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What is Git Rebase and How Is it Different than Merging? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/fbf952651707fd0325c6d7e4bd79fefd\/p\/uploads\/2019\/06\/e8422676.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Merging and rebasing accomplish similar goals, but go about them in different ways. Both help manage working with branches and collaborating with multiple people, but they\u2019re not interchangeable, and rebasing can be harmful if not done properly.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_The_Deal_With_Rebasing\"><\/span>What\u2019s The Deal With Rebasing?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Rebasing is very complicated, so it\u2019s important to understand how <code>git<\/code>\u00a0works under the hood if we\u2019re going to make any sense of it.<\/p>\n<p>Git stores your code as a <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">series<\/a> of edits. You can think of this like a chain, working backwards. Each edit, or \u201ccommit,\u201d references the ID of the previous commit and includes what changed from the previous commit. This chain is only stored on your computer; your Git client doesn\u2019t talk to any other Git clients unless it\u2019s performing a <code>fetch<\/code>\u00a0or <code>push<\/code>\u00a0(<code>pull<\/code>\u00a0is really just <code>fetch + merge<\/code>\u00a0with your local branch), and even then, it\u2019s only talking to a shared remote repository.<\/p>\n<p>Branches are more complicated. Git only stores one thing when dealing with branches: the ID of the commit at the end of the branch. In this way, you can think of them like the playhead on a record player; you put the branch head at a specific position, and it works its way back through the chain, \u201cplaying\u201d your code and arriving at a final version.\u00a0Whenever you commit, your local git client will automatically move the playhead forward to the new commit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-863\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/0b7bbc874f8adf67b1ac0e05a5b3cd4a\/p\/uploads\/2019\/06\/3b3431f3-1.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you wanted to merge feature into master, you would run:<\/p>\n<pre>git checkout master&#13;\ngit merge feature<\/pre>\n<p>This creates a new merge commit, and if there are any conflicts, you\u2019ll have to solve them manually. The <code>git merge<\/code>\u00a0command moves the master playhead to the new merge commit, and deletes the feature playhead, as it\u2019s no longer necessary.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-864\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b7b4e8be832d7de8a656c772220157d4\/p\/uploads\/2019\/06\/900fd32c-1.png\" alt=\"\" width=\"700\" height=\"299\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This method of merging code presents three problems:<\/p>\n<ul>\n<li>There may be changes on the master branch that the feature branch would like to include, particularly if feature is taking a while to develop.<\/li>\n<li>Being forced to go through the merge process every time you want to work with branches is annoying.<\/li>\n<li>The commit history is messy, though this is largely an aesthetic problem.<\/li>\n<\/ul>\n<p>Rebasing tries to solve these issues, to varying degrees of success. Rebasing changes where you started your branch. The whole branch is lifted up, and transported to the end of the current master branch, where it connects to the end. The master branch is left untouched, and is free to continue receiving commits.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-866\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/e1c3895e611a2615475fcb5e4336c81a\/p\/uploads\/2019\/06\/86db9305.png\" alt=\"\" width=\"700\" height=\"200\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The commits aren\u2019t actually <em>moved<\/em>, however, since commits are immutable. Rather, they\u2019re copied, which results in new commit IDs. The previous commits are left stranded, hiding in your Git files but never to be seen again, since the playhead has moved somewhere else.<\/p>\n<p>To execute this process from the command line, you would run:<\/p>\n<pre>git checkout feature&#13;\ngit pull&#13;\ngit rebase master<\/pre>\n<p>This opens the branch, pulls the current changes to master, and then rebases the feature branch onto the master branch.<\/p>\n<p>At this point, the code in the feature branch is now more up to date, which is the only real feature of <code>git rebase<\/code>. Rebasing does not merge branches, since it does not create any merge commits or move master\u2019s playhead.<\/p>\n<p>If you want to merge after rebasing, you\u2019d run:<\/p>\n<pre>git checkout master&#13;\ngit merge\u00a0feature<\/pre>\n<p>Which would look like this, with master\u2019s playhead replacing the feature playhead:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-868\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/11478df642579c9623aa0fca687d02b4\/p\/uploads\/2019\/06\/5cce6cb9.png\" alt=\"\" width=\"700\" height=\"200\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>So rebasing doesn\u2019t end up solving the problem of dealing with merges, since you\u2019ll need to merge at the end anyway to update the master branch. The actual <code>merge<\/code>\u00a0command at the end should go off without a hitch though, since the process of rebasing requires you to \u201cmerge\u201d in changes, which still can cause conflicts. And if you\u2019d still like to continue working on your branch, you still need to \u201cmerge\u201d in changes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Do_Not_Rebase_Shared_Branches\"><\/span>Do Not Rebase Shared Branches<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Remember how rebasing copies commits and leaves a stranded playhead? That\u2019s actually a major issue if you\u2019re working with shared code. Let\u2019s say you created the feature branch, and push it to your repo so your coworkers can test it out. That\u2019s entirely fine, but if one of them wanted to branch off of your branch, when you eventually rebase, you end up with this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/fbf952651707fd0325c6d7e4bd79fefd\/p\/uploads\/2019\/06\/e8422676.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Your coworker\u2019s feature2 branch is now referencing an old chain of commits. Your Git client has no way of knowing this, since the feature2 branch is stored on your coworker\u2019s computer. They also have no way of knowing that you rebased until you push your changes.<\/p>\n<p>When you rebased, it didn\u2019t copy the feature2 branch when it copied all the commits. Even if it could, it wouldn\u2019t effect your coworker\u2019s local Git repo, making everything out of sync. The solution here would be to rebase feature2 onto feature at the spot that it would be at, but that\u2019s messy, even by Git standards, and this is just a very simple example.<\/p>\n<p>Bottom line is, <strong>do not rebase if you\u2019re not working locally<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"When_Is_Rebasing_Better_Than_Merging\"><\/span>When Is Rebasing Better Than Merging?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If your branch is going to take a while to develop, rebasing solves the issue of \u201cbranch syndrome,\u201d where your code is way too out of date with the working master, and you need to update it to continue working. Speaking <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly, you should try to avoid this problem as much as possible, but rebasing can fix it when it arises.<\/p>\n<p>If you\u2019re just making small, incremental, daily changes, you should instead work on a local master branch and use <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/yangsu.github.io\/pull-request-tutorial\/\">Pull Requests<\/a>\u00a0when you\u2019re ready to push your changes. These use the model of topical branches, created specifically to store your code before it\u2019s <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roved for a merge.<\/p>\n<p>But, if you\u2019re working on a weekly timespan, and are going to end up making multiple pull requests and merging multiple times, you can work on your code for a bit longer, rebase locally for updates, and perform one pull request at the end to cut down on the amount of testing and talking to supervisors. Rebasing is primarily a local thing, so you can do it on your staging branches without waiting for approval.<\/p>\n<p>If nobody else depends on your branch, you <em>can<\/em> rebase before a branch merge to make the commit history clean and one dimensional. Though, one could argue that traditional merging, while certainly uglier, is easier to follow and debug, since merge commits are entirely nondestructive. Either way, the rebasing needs to be done right before your changes are merged, or you may run into the issue of Master being updated before your changes are approved.\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/852\/what-is-git-rebase-and-how-is-it-different-than-merging\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What is Git Rebase and How Is it Different than Merging? \u2013 CloudSavvy IT&#8221; Merging and rebasing accomplish similar goals, but go about them in different ways. Both help manage working with branches and collaborating with multiple people, but they\u2019re not interchangeable, and rebasing can be harmful if not done properly. What\u2019s The Deal With&#8230;<\/p>\n","protected":false},"author":1,"featured_media":82014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/e8422676.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-82013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/82013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=82013"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/82013\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/82014"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=82013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=82013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=82013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}