{"id":40,"date":"2023-11-20T11:07:58","date_gmt":"2023-11-20T10:07:58","guid":{"rendered":"https:\/\/www.huwig.de\/en\/?p=40"},"modified":"2023-11-20T11:07:58","modified_gmt":"2023-11-20T10:07:58","slug":"handy-git-tricks","status":"publish","type":"post","link":"https:\/\/www.huwig.de\/en\/de\/2023\/11\/20\/handy-git-tricks\/","title":{"rendered":"Handy Git tricks"},"content":{"rendered":"<div id=\"main-header\" class=\"\">\n<div id=\"title-heading\" class=\"pagetitle with-breadcrumbs\">\n<p>This is a collection of tricks I use to make my life with Git easier while maintaining a clean history that helps to understand the code. Which is the whole point of version control, isn&#8217;t it?<\/p>\n<h1>Transplant a branch<\/h1>\n<p><a href=\"https:\/\/www.huwig.de\/wp-content\/uploads\/2020\/08\/Transplant-branch.svg\"><img decoding=\"async\" class=\"alignright size-full wp-image-269\" src=\"https:\/\/www.huwig.de\/wp-content\/uploads\/2020\/08\/Transplant-branch.svg\" alt=\"\" \/><\/a>To transplant a Git branch from one branch to another, use<\/p>\n<pre>git rebase --onto<\/pre>\n<h2>Example<\/h2>\n<pre>git rebase --onto beta develop fix<\/pre>\n<h1>Update without checkout<\/h1>\n<p><a href=\"https:\/\/www.huwig.de\/wp-content\/uploads\/2020\/08\/Update-without-checkout.svg\"><img decoding=\"async\" class=\"alignright size-full wp-image-270\" src=\"https:\/\/www.huwig.de\/wp-content\/uploads\/2020\/08\/Update-without-checkout.svg\" alt=\"\" \/><\/a><\/p>\n<p>To update a branch without checking it out first &#8211; e.g. to rebase onto the latest version of the parent branch, use<\/p>\n<pre>git fetch origin &lt;branch&gt;:&lt;branch&gt;<\/pre>\n<p>You really need to type the target branch twice!<\/p>\n<h2>Example<\/h2>\n<pre>git fetch origin develop:develop<\/pre>\n<h1>Rewrite history<\/h1>\n<p>Before I merge I rewrite my history so that each commit is clean and there are no &#8222;changes after review&#8220; commits: they don&#8217;t help to understand why the code is the way it is.<\/p>\n<pre>git add .\r\ngit commit --fixup\r\ngit rebase -i --autosquash develop<\/pre>\n<p><a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Tools-Rewriting-History\">Read more about this in the Git Book<\/a><\/p>\n<h1>Know the version<\/h1>\n<p>We use annotated tags to mark each release. We also never merge back but only merge older branches into newer branches. This allows us to easily determine the version number based on the tags.<\/p>\n<pre>git describe --first-parent [branch]<\/pre>\n<p>If you don&#8217;t give the name of a branch (or hash or similar), you get the version number of the current branch.<\/p>\n<h2>Example<\/h2>\n<pre>git describe --first-parent beta<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is a collection of tricks I use to make my life with Git easier while maintaining a clean history that helps to understand the code. Which is the whole point of version control, isn&#8217;t it? Transplant a branch To transplant a Git branch from one branch to another, use git rebase &#8211;onto Example git &hellip; <a href=\"https:\/\/www.huwig.de\/en\/de\/2023\/11\/20\/handy-git-tricks\/\" class=\"more-link\"><span class=\"screen-reader-text\">Handy Git tricks<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":1,"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":41,"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/posts\/40\/revisions\/41"}],"wp:attachment":[{"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huwig.de\/en\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}