{"id":2135,"date":"2025-02-02T14:07:24","date_gmt":"2025-02-02T03:07:24","guid":{"rendered":"https:\/\/ntsblog.homedev.com.au\/?p=2135"},"modified":"2025-05-02T09:51:36","modified_gmt":"2025-05-01T23:51:36","slug":"git-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2025\/02\/02\/git-tips-and-tricks\/","title":{"rendered":"git &#8211; tips and tricks"},"content":{"rendered":"<div id=\"ntsbl-4166037695\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>This is a set of tips and tricks that I commonly use when working day to day with git.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">General Tips<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">File Path length<\/h3>\n\n\n\n<p>Keep your file paths short, as sometimes you will hit a file path limit.<br>I keep my repos as close to the root of the drive as possible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">c:\\code\nor if you work for multiple clients or projects you might have a subfodler\nc:\\code\\{project}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Setup<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Username and email<\/h4>\n\n\n\n<p>When building a new computer you will need to setup your username and email.<\/p>\n\n\n\n<p>If working for one company you can use a -global switch to <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git config --global user.name \"Your Name\"\ngit config --global user.email \"youremail@yourdomain.com\"<\/code><\/pre>\n\n\n\n<p>To set it on a single repo enter the repo and use <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">cd \/path\/to\/your\/repository\ngit config user.name \"Your Name\"\ngit config user.email \"youremail@yourdomain.com\"<\/code><\/pre>\n\n\n\n<p>To see your configuration <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git config --list<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Long filenames<\/h4>\n\n\n\n<p>Sometimes with folder structures that are too deep inside your repo you will hit a path limit. Setting this on can help extend the length to 32767 long or 2^15<\/p>\n\n\n\n<p>In a window as administrator (powershell or git bash), run the following command to enable long file paths.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git config --system core.longpaths true<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Git Commands<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Clone<\/h4>\n\n\n\n<p>cd into the root of the folder where you keep your code<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">New branch &#8211; first commit<\/h4>\n\n\n\n<p>These are the commands you may use to create a new branch.. once worked on add you changes, commit and the push for the first time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git checkout -b feature\/1234\n... do some work and make changes\n... feature\/1234 where 1234 might be a reference to the task # you are working on\n\ngit add .\ngit commit -m \"first commit\"\ngit push -u origin feature\/1234<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Merge Process<\/h4>\n\n\n\n<p>This is an example of how you may work on a release branch from your feature branch<\/p>\n\n\n\n<p>After creating a pushing your feature branch you may pull together a release branch.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git checkout -b release\/2025-05-R1\ngit push -u origin release\/2025-05-R1\n\ngit checkout feature\/1234<\/code><\/pre>\n\n\n\n<p>The above creates a new release branch and pushes it to the remote repo. This release branch could contain multiple feature branches that are then CI\/CD deployed to your test environments.<\/p>\n\n\n\n<p>Assume you release a bug in your feature branch. You would switch back to your feature branch to do more work. After unit testing the change to prove you have fixed the bug, you would then wish to update the release branch and deploy to test again.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git add . \ngit commit -m \"1234 - Fix bug\"\ngit push\ngit checkout release\/2025-05-R1\ngit merge feature\/1234\ngit push<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Pull a specific commit<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"\" class=\"\">git checkout {commit hash}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Rename a folder<\/h4>\n\n\n\n<p>Sometimes you want git to treat a folder rename as a move not a delete and add so this is the command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git mv {old name} {new name}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Discard local uncommitted changes<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git checkout .<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Fix untracked files<\/h4>\n\n\n\n<p>Sometimes people start with a bad .gitignore and so all the bin and obj files and .vs folders are committed to source control, which is bad and you want to get them out of the remote repo<\/p>\n\n\n\n<p>So you adjust the .gitignore but now all the files that you are no longer tracking are still in the remote rep.<\/p>\n\n\n\n<p>To clear the remote repo and re-align the remote repo with the latest .git ignore do the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git rm -r --cached .\ngit status\ngit add .\ngit commit -m \"fixed untracked files\"\ngit push<\/code><\/pre>\n\n\n\n<p>The git status is just so you can see what it has done.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Tag a branch<\/h4>\n\n\n\n<p>Here is an example where you might tag a release by sprint name<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git tag -a 2025-01-R1 -m \"2025-01 - release-1\"\ngit push origin 2025-01-R1<\/code><\/pre>\n\n\n\n<p>to remove that tag<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git tag -d 2025-01-R1\ngit push --delete origin 2025-01-R1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Cancel Merge<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git merge --abort<\/code><\/pre>\n\n\n\n<p>that didn&#8217;t work for me on an occasion so I used the following to cancel the merge<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git reset\ngit checkout .<\/code><\/pre>\n\n\n\n<p>The first command finds and clears all the files no longer being tracked, you then add all the changes, commit and then push.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Pull a Tag<\/h4>\n\n\n\n<p>You wish to create a branch from a tag<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">git checkout tags\/&lt;tag&gt; -b &lt;branch&gt;<\/code><\/pre>\n\n\n\n<p>So this will pull the tag into your local branch. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My git tips and tricks for working with git<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","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":""},"categories":[1],"tags":[],"class_list":["post-2135","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/2135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=2135"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/2135\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=2135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=2135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=2135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}