{"id":101,"date":"2022-04-29T03:04:50","date_gmt":"2022-04-29T03:04:50","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/?p=101"},"modified":"2022-04-29T03:06:28","modified_gmt":"2022-04-29T03:06:28","slug":"git-ready-to-rebase","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/2022\/04\/29\/git-ready-to-rebase\/","title":{"rendered":"Git Ready to Rebase"},"content":{"rendered":"\n<h2 class=\"has-large-font-size wp-block-heading\">The basics of git rebase.<\/h2>\n\n\n\n<p>Hello again! This week the team ran into some git trouble which led to me tumbling down a git rabbit hole.  Seems fitting that it would be a R-abbit hole, since the primary commands I needed to sort through were R-eset, R-evert, R-estore, and R-ebase.  Today though, we will be focusing primarily on git&#8217;s rebase command.  Rebasing is exactly what it sounds like you are changing the commit that a particular branch or commit connects to. First, we will take a look at a basic rebase and then we&#8217;ll check out how to move a branch back to an earlier commit. <\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">Visualizing the Repo<\/h3>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git log --graph --all<\/code><\/pre>\n\n\n\n<p>First things first, when starting out with git in general and rebase more specifically, it&#8217;s really helpful to be able to checkout visual graph representation of the git repo.  Above is a command you can use to get just that. There are a ton of ways to format the log with a graph and if you&#8217;re interested in customizing it, you can checkout <a href=\"https:\/\/www.theserverside.com\/blog\/Coffee-Talk-Java-News-Stories-and-Opinions\/How-to-use-the-git-log-graph-command\">this article<\/a> for an overview and the answers to <a href=\"https:\/\/stackoverflow.com\/questions\/1057564\/pretty-git-branch-graphs\">this post<\/a> if you want to make each commit only take up one line. I definitely recommend taking a look at <a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Basics-Git-Aliases\">this explanation<\/a> of how to create aliases as well that way you can save your fancy customized graph preferences in an easy to remember command.<\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">Basic Rebase Operation<\/h3>\n\n\n\n<p>A word of caution, it&#8217;s best not to rebase on branches or main that other folks are working on unless you have a specific plan or workflow everyone has agreed upon. This is because a rebase will not only change the base of the commits or branch being moved, but <strong>rebasing will change the hash (alpha numeric string associated with the commit) of the commits that are being moved.<\/strong><\/p>\n\n\n\n<p>Before we begin, just like a merge you can abort a rebase at any time before it&#8217;s complete by using the abort flag:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git rebase --abort<\/code><\/pre>\n\n\n\n<p>If you use the commands:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git checkout topic\ngit rebase main<\/code><\/pre>\n\n\n\n<p>Or the command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git rebase main topic<\/code><\/pre>\n\n\n\n<p>It will take your repo from this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>topic:           E - F\n               \/\nmain: A - B - C - D<\/code><\/pre>\n\n\n\n<p>To this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>A - B - C - D - <strong>E<\/strong> - <strong>F<\/strong> - <strong>G<\/strong> \n            |   |       |\n            |   |       topic branch stops\n            |   topic branch starts\n            main stops\n\n<strong>E, F and G have new hashes <\/strong><\/code><\/pre>\n\n\n\n<p>If there are conflicts you will need to manually go into the files and decide what to keep in the files with conflicts (the first resolution will be any conflicts between D and E).  If not you&#8217;ll, be able to skip ahead to the next steps section.<\/p>\n\n\n\n<p>When you resolve all the conflicts for the first commit, in this case E, you&#8217;ll need these two commands:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git add .\ngit rebase --continue<\/code><\/pre>\n\n\n\n<p>You&#8217;ll have to repeat the resolve conflicting files, add resolved files, and rebase continue process for each commit.  In our example, we would repeat it for F and G.<\/p>\n\n\n\n<p><strong>Next Steps<\/strong><\/p>\n\n\n\n<p>In most situations, you&#8217;ll want to do two additional steps after the rebase is complete.<\/p>\n\n\n\n<p>Merge the topic branch into main:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git checkout main\ngit merge topic<\/code><\/pre>\n\n\n\n<p>Resulting in:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-tertiary-background-color has-text-color has-background\"><code>A - B - C - D - E - F - G\n                        |\n                        main AND topic end<\/code><\/pre>\n\n\n\n<p>Remove the topic branch:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git branch -d topic<\/code><\/pre>\n\n\n\n<p>Finally, we arrive here with a single branch on main:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-tertiary-background-color has-text-color has-background\"><code>main: A - B - C - D - E - F - G<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">Rebasing a Branch on a Previous Commit<\/h3>\n\n\n\n<p>This word of caution is worth mentioning again. It&#8217;s best not to rebase on branches or main that other folks are working on unless you have a specific plan or workflow everyone has agreed upon. This is because a rebase will not only change the base of the commits or branch being moved, but <strong>rebasing will change the hash (alpha numeric string associated with the commit) of the commits that are being moved.<\/strong><\/p>\n\n\n\n<p>Now that we are very clear on that, say we have this situation:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>topic:              E - F\n                   \/\nmain: A - B - C - D<\/code><\/pre>\n\n\n\n<p>And we want this to be our end result:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>topic:      E - F\n           \/\nmain: A - B - C - D<\/code><\/pre>\n\n\n\n<p>It is very similar to a basic rebase.  You will just need the hash of the commit you want to move the branch to.  In this case commit B:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git checkout topic\ngit rebase --onto hash_of_B main<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git rebase main topic --onto hash_of_B<\/code><\/pre>\n\n\n\n<p>If you want to make sure you&#8217;re moving the intended commits, you can use the interactive mode by adding an -i flag:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git rebase main -i topic --onto hash_of_B<\/code><\/pre>\n\n\n\n<p>Like it was described for the basic merge, you will need to cycle through the commits resolving any conflicts and using the commands:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git add .\ngit rebase --continue<\/code><\/pre>\n\n\n\n<p>Once all of the conflicts are resolved and the rebase is complete, we will arrive to our destination:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>topic:      E - F\n           \/\nmain: A - B - C - D<\/code><\/pre>\n\n\n\n<p><strong>Root Included Variation<\/strong><\/p>\n\n\n\n<p>If you wanted to include the root in the rebase option to get this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>topic:      D - E - F\n           \/\nmain: A - B - C<\/code><\/pre>\n\n\n\n<p>You would use the root flag:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\"><code>git checkout topic\ngit rebase --onto hash_of_B --root main<\/code><\/pre>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">In Conclusion<\/h3>\n\n\n\n<p>These are a couple basic rebase commands.  Rebase can do quite a few things.  I also recommend checking out <a href=\"https:\/\/www.git-tower.com\/learn\/git\/faq\/git-squash\/\">the squash feature<\/a>, if you don&#8217;t know it already.  It is very useful for those times you realize you missed something in your commit and you have to commit again.  It allows you to make one commit out of multiple commits.<\/p>\n\n\n\n<p>Until next post, be a goldfish.<\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\">Sources<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.theserverside.com\/blog\/Coffee-Talk-Java-News-Stories-and-Opinions\/How-to-use-the-git-log-graph-command\">Formatting Your Log Graph<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/1057564\/pretty-git-branch-graphs\">Single Line Log Graphs<\/a><\/li><li><a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Basics-Git-Aliases\">Creating an Alias<\/a><\/li><li><a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Branching-Rebasing\">Rebase Basics&#8230; Rebasics if you will<\/a><\/li><li><a href=\"https:\/\/www.git-tower.com\/learn\/git\/faq\/git-squash\/\">Rebase Squash<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basics of git rebase.<\/p>\n","protected":false},"author":12319,"featured_media":106,"comment_status":"closed","ping_status":"open","sticky":false,"template":"single-no-separators","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23,28,24],"class_list":["post-101","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-git","tag-log-graph","tag-rebase"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/users\/12319"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":6,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts\/101\/revisions\/108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/media\/106"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}