{"id":1590,"date":"2024-06-04T22:00:00","date_gmt":"2024-06-04T20:00:00","guid":{"rendered":"https:\/\/demo.sidekick.be\/itenium\/exotic-git-recipes\/"},"modified":"2025-11-28T13:31:45","modified_gmt":"2025-11-28T12:31:45","slug":"exotic-git-recipes","status":"publish","type":"post","link":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/","title":{"rendered":"Exotic Git Snippets"},"content":{"rendered":"<h1 id=\"splitting-repositories\">Splitting Repositories<\/h1>\n<p>You start with a small project. It grows over time as you add more and more features. And then, at some point, you realize one of these features should actually have been a separate repository because it provides functionality that can be useful in multiple contexts.<\/p>\n<p>So far I moved the files and did a <code>git init<\/code>. At this point, I was always sad because well, I lost the history of the feature. Not only less commits for my Github statistics but I also lost <code>git blame<\/code> so it could no longer tell me why a particular algorithm was written the way it was.<\/p>\n<p>Turns out git allows you to have your cake and eat it too!<\/p>\n<pre><code class=\"language-bash\"># DEPRECATED!!\ngit filter-branch --prune-empty --subdirectory-filter src master\n<\/code><\/pre>\n<p>These days you get a deprecation warning when using <code>filter-branch<\/code> and are pointed to a third party package instead:<\/p>\n<p><a href=\"https:\/\/github.com\/newren\/git-filter-repo\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/newren\/git-filter-repo.svg?style=social&amp;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/newren\/git-filter-repo\">newren\/git-filter-repo<\/a>: Quickly rewrite git repository history (filter-branch replacement)<\/p>\n<p><a href=\"https:\/\/htmlpreview.github.io\/?https:\/\/github.com\/newren\/git-filter-repo\/blob\/docs\/html\/git-filter-repo.html\">git-filter-repo(1) docs<\/a><\/p>\n<pre><code class=\"language-sh\"># Requires git &gt;= 2.24.0 and Python &gt;= 3.5\npip3 install git-filter-repo\ngit filter-repo -h\ngit filter-repo --path src --subdirectory-filter src\n<\/code><\/pre>\n<p><\/p>\n<h1 id=\"locking-out-package-lockjson\">Locking out package-lock.json<\/h1>\n<p>These days each <code>npm install<\/code> makes a <code>git diff<\/code> on frontend projects a real drag. Page upon page of uninteresting changes.<\/p>\n<pre><code class=\"language-bash\">git diff -- ':!*package-lock.json' ':!*yarn.lock'\n<\/code><\/pre>\n<p>Or as a <code>~\/.gitconfig<\/code> alias:<\/p>\n<pre><code class=\"language-bash\">df = \"!f() { git diff --ignore-all-space -- $1 ':(exclude)*yarn.lock' ':!**package-lock.json'; }; f\"\n<\/code><\/pre>\n<p>Important note: If you use the <code>git df<\/code> alias, <code>git df package-lock.json<\/code> will <strong>never<\/strong> show anything. You\u2019ll have to use plain old <code>git diff<\/code> for checking the excluded files!<\/p>\n<p><\/p>\n<h1 id=\"deleting-merged-branches\">Deleting merged branches<\/h1>\n<p><strong>Locally<\/strong>:<br \/><code>~\/.gitconfig<\/code> aliases. <code>(develop|master)<\/code>: Do not delete these branches even if merged with current branch.<\/p>\n<pre><code class=\"language-ini\">merged-branches = !git branch --merged | egrep -v \"(\\*| ) (develop|master)\" | xargs -n 1\ndm = !git merged-branches git branch -d\n<\/code><\/pre>\n<p><strong>Remote<\/strong>:<\/p>\n<p>Somehow a CI server always seems to be amassing feature branches. A simple cleanup script to the rescue!<\/p>\n<p>Replace <code>name-committer<\/code> (author filter) and <code>origin<\/code> (remote filter) and add as a <code>~\/.gitconfig<\/code> alias:<\/p>\n<pre><code class=\"language-ini\">my-merged-remote-branches = !git for-each-ref --format='%(authorname):%(refname)' | egrep \"name-committer\" | egrep \"refs\/remotes\" | sed -e \"s\/^.*:refs\\\/remotes\\\/origin\\\/\/:\/\"\n<\/code><\/pre>\n<p>Followed by PowerShell:<\/p>\n<pre><code class=\"language-powershell\">git my-merged-remote-branches | % { git push --no-verify origin $_ }\n<\/code><\/pre>\n<p><\/p>\n<h1 id=\"filename-case-renaming\">Filename case renaming<\/h1>\n<p>Pesky Windows.<br \/> Also note the <a href=\"https:\/\/git-scm.com\/docs\/git-config#Documentation\/git-config.txt-coreignoreCase\"><code>core.ignorecase<\/code><\/a> configuration value.<\/p>\n<pre><code class=\"language-bash\">git mv -f OldFileNameCase newfilenamecase\n<\/code><\/pre>\n<p><\/p>\n<h1 id=\"remapping-remote-urls\">Remapping remote urls<\/h1>\n<p>When the remote url changes for all your repositories.<\/p>\n<pre><code class=\"language-powershell\">$paths = Get-ChildItem \"c:git-repos\" | ? { $_.PSIsContainer }\nforeach ($path in $paths) {\n    $gitPath = Join-Path $path.FullName \".git\"\n    if (Test-Path -Path $gitPath -PathType Container) {\n        Write-Host $path.FullName \" -&gt; \" $gitPath\n        $url = \"https:\/\/new-base-url.com\/$($path.Name)\"\n        Write-Host \"Remap to $url\"\n        Push-Location $path.FullName\n        git remote set-url origin $url\n        Write-Host \" \"\n        Pop-Location\n    }\n}\n<\/code><\/pre>\n<p><\/p>\n<h1 id=\"create-pr-in-browser\">Create PR in browser<\/h1>\n<p>From your current branch.<\/p>\n<pre><code class=\"language-powershell\">function Create-PullRequest() {\n  $baseUrl = git remote get-url origin\n\n  if ($baseUrl -match 'github.com') {\n    $urlTemplate += \"{base-url}\/compare\/{target-branch}...{source-branch}\"\n  } else {\n    $urlTemplate += \"{base-url}\/pullrequestcreate?sourceRef={source-branch}&amp;targetRef={target-branch}\"\n  }\n\n  $sourceBranch = git rev-parse --abbrev-ref HEAD\n  # $targetBranch = \"develop\"\n  $targetBranch = \"master\"\n\n  git push -u origin $sourceBranch\n  start $urlTemplate.Replace(\"{base-url}\", $baseUrl).Replace(\"{source-branch}\", [uri]::EscapeDataString($sourceBranch)).Replace(\"{target-branch}\", $targetBranch)\n}\n\nSet-Alias pr Create-PullRequest\n<\/code><\/pre>\n<\/p>\n<h1 id=\"other-blog-posts\">Other blog posts<\/h1>\n<ul>\n<li><a href=\"https:\/\/itenium.be\/blog\/dev-setup\/git-assume-unchanged\/\">git assume unchanged<\/a><\/li>\n<li><a href=\"https:\/\/itenium.be\/blog\/productivity\/git-hooks-with-husky\/\">git hooks with Husky<\/a><\/li>\n<\/ul>\n<h1>Extras<\/h1>\n<p><b>Other interesting reads<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/a\/21079437\/540352\">Ignore certain files in git output<\/a><\/li>\n<\/ul>\n<h1>Updates<\/h1>\n<ul>\n<li> 5 June 2024: git filter-repo and Create-PullRequest for Github<\/li>\n<li> 30 December 2019: PS scripts to remap remote urls &#038; create a PR<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Some git commands and scripts that come in handy from time to time.<\/p>\n","protected":false},"author":1,"featured_media":2080,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[26],"class_list":["post-1590","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev-setup","tag-git"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Exotic Git Snippets - Itenium<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exotic Git Snippets - Itenium\" \/>\n<meta property=\"og:description\" content=\"Some git commands and scripts that come in handy from time to time.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/\" \/>\n<meta property=\"og:site_name\" content=\"Itenium\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-04T20:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-28T12:31:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"360\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Usdevelop\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Usdevelop\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/\",\"name\":\"Exotic Git Snippets - Itenium\",\"isPartOf\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg\",\"datePublished\":\"2024-06-04T20:00:00+00:00\",\"dateModified\":\"2025-11-28T12:31:45+00:00\",\"author\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\"},\"breadcrumb\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#primaryimage\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg\",\"contentUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg\",\"width\":360,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/demo.sidekick.be\/itenium\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Exotic Git Snippets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/\",\"name\":\"Itenium\",\"description\":\"We build software. We build careers.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\",\"name\":\"Usdevelop\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"caption\":\"Usdevelop\"},\"sameAs\":[\"https:\/\/demo.sidekick.be\/itenium\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exotic Git Snippets - Itenium","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Exotic Git Snippets - Itenium","og_description":"Some git commands and scripts that come in handy from time to time.","og_url":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/","og_site_name":"Itenium","article_published_time":"2024-06-04T20:00:00+00:00","article_modified_time":"2025-11-28T12:31:45+00:00","og_image":[{"width":360,"height":300,"url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg","type":"image\/jpeg"}],"author":"Usdevelop","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Usdevelop","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/","url":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/","name":"Exotic Git Snippets - Itenium","isPartOf":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#website"},"primaryImageOfPage":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#primaryimage"},"image":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#primaryimage"},"thumbnailUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg","datePublished":"2024-06-04T20:00:00+00:00","dateModified":"2025-11-28T12:31:45+00:00","author":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022"},"breadcrumb":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#primaryimage","url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg","contentUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/git-snippets.jpg","width":360,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/exotic-git-recipes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/demo.sidekick.be\/itenium\/"},{"@type":"ListItem","position":2,"name":"Exotic Git Snippets"}]},{"@type":"WebSite","@id":"https:\/\/demo.sidekick.be\/itenium\/#website","url":"https:\/\/demo.sidekick.be\/itenium\/","name":"Itenium","description":"We build software. We build careers.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022","name":"Usdevelop","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","caption":"Usdevelop"},"sameAs":["https:\/\/demo.sidekick.be\/itenium"]}]}},"_links":{"self":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/comments?post=1590"}],"version-history":[{"count":3,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1590\/revisions"}],"predecessor-version":[{"id":2295,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1590\/revisions\/2295"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media\/2080"}],"wp:attachment":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media?parent=1590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/categories?post=1590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/tags?post=1590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}