{"id":926,"date":"2022-05-02T12:21:00","date_gmt":"2022-05-02T10:21:00","guid":{"rendered":"https:\/\/nubisoft.io\/blog\/?p=926"},"modified":"2022-08-02T12:33:27","modified_gmt":"2022-08-02T10:33:27","slug":"mastering-git-with-254-simple-commands","status":"publish","type":"post","link":"https:\/\/nubisoft.io\/blog\/mastering-git-with-254-simple-commands\/","title":{"rendered":"Mastering Git with 254 simple commands"},"content":{"rendered":"\n<p><a href=\"https:\/\/git-scm.com\/\" data-type=\"URL\" data-id=\"https:\/\/git-scm.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Git <\/a>is by far the most popular Version Control System (VCS) used in web development.<\/p>\n\n\n\n<p>According to the latest StackOverflow survey over 90% of respondents use it on a regular basis. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"377\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1024x377.png\" alt=\"\" class=\"wp-image-927\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1024x377.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-300x110.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-768x283.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1200x442.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image.png 1399w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>https:\/\/insights.stackoverflow.com\/survey\/2021#section-most-popular-technologies-other-tools<\/figcaption><\/figure>\n\n\n\n<p>The question one would ask is how to use it effectively?<br>This post will guide you through the most best practices, important git commands and do&#8217;s and don&#8217;ts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic concepts<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>git &#8211; is a version control system, don&#8217;t confuse it with Github or Gitlab which are hosting providers<\/li><li>repository &#8211; home to all your branches and commits, everything that we do with git is done in the context of a repository<\/li><li>stage &#8211; in order to commit something you&#8217;ll need to say what things you actually want to commit, that&#8217;s staging<\/li><li>commit &#8211; making your changes ready for a push<\/li><li>branch &#8211; all your commits will be done on a branch<\/li><li>origin &#8211; remote repository<\/li><li>push &#8211; uploading changes to origin <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">#1 Use Git GUI<\/h3>\n\n\n\n<p>This post&#8217;s title says it all &#8211; it might be a controversial thing, especially with this being the first point, but it&#8217;s nothing but the truth. Once you acknowledge this you&#8217;ll be able to boost your productivity by a serious amount. <\/p>\n\n\n\n<p>Obviously, this isn&#8217;t true for all people and in all cases. If you prefer using <code>git<\/code> CLI good for you, but most people will find using a <code>git<\/code> client much more straightforward. <\/p>\n\n\n\n<p>My personal recommendations would be <a href=\"https:\/\/www.gitkraken.com\/\" data-type=\"URL\" data-id=\"https:\/\/www.gitkraken.com\/\">GitKraken <\/a>and <a href=\"https:\/\/git-fork.com\/\" data-type=\"URL\" data-id=\"https:\/\/git-fork.com\/\">Fork<\/a>, although there are many other alternatives available. <\/p>\n\n\n\n<p>There might be some cases in which there&#8217;s a necessity to use git from the command line and I&#8217;ll cover that later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">#2 You won&#8217;t break anything if you don&#8217;t push to the origin<\/h3>\n\n\n\n<p>It might be a trivial thing however it&#8217;s crucial. If you don&#8217;t have full confidence behind your commit just don&#8217;t push it. I wouldn&#8217;t recommend using force push for master or feature branches but sooner than later you&#8217;ll have to resolve merge conflicts and that process may not be that straightforward. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">#3 Merge but also rebase, squash and reset<\/h3>\n\n\n\n<p>The most common approach when we want to connect two branches together is doing a merge, which is fine for adding feature branch&#8217;s changes to the master branch. However, working with other people will force you sometimes to do the opposite. In order to change your branch&#8217;s root use rebase. Apart from the obvious, your git history will be clean so a clear win-win situation here.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"382\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1-1024x382.jpg\" alt=\"\" class=\"wp-image-929\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1-1024x382.jpg 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1-300x112.jpg 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1-768x287.jpg 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1-1536x573.jpg 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1-1200x448.jpg 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/IMG_16BF822DD285-1.jpg 1543w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>https:\/\/matt-rickard.com\/squash-merge-or-rebase\/<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>git rebase &lt;base&gt;<\/code><\/pre>\n\n\n\n<p>Squashing is another helpful technique that allows us to reduce two or more commits into one. The best way to do this in the CLI is to reset &#8211;soft your local to one of the previous commits and then create a new commit and use <code>force-push<\/code> to keep git history clean.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~n &amp;&amp;\ngit commit<\/code><\/pre>\n\n\n\n<p>There are 3 types of git resets:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>reset --soft <\/code>&#8211; uncommit&nbsp;changes, changes are left staged,<\/li><li><code>reset --mixed <\/code>&#8211; uncommit + unstage&nbsp;changes,<\/li><li><code>reset --hard <\/code>&#8211; uncommit + unstage + delete<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">#4 Use conventional commits (really)<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.conventionalcommits.org\/en\/v1.0.0\/\" data-type=\"URL\" data-id=\"https:\/\/www.conventionalcommits.org\/en\/v1.0.0\/\">Conventional commits<\/a> are a great way to make your commit&#8217;s message more meaningful. There&#8217;s a really comprehensive Github <a href=\"https:\/\/gist.github.com\/qoomon\/5dfcdf8eec66a051ecd85625518cfd13\">gist <\/a>that documents how to use them properly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">#5 Cherrypick for the win<\/h3>\n\n\n\n<p>Let&#8217;s imagine a situation where you find yourself in need of changes done on another branch and usually merging or rebasing might not be doable (and we don&#8217;t just copy files, period). The best thing that you can do here is make a cherrypick &#8211; it picks a specific commit and adds it to your branch.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git cherry-pick commitSha<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"500\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/AfterCherryPick.png\" alt=\"\" class=\"wp-image-930\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/AfterCherryPick.png 1000w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/AfterCherryPick-300x150.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/AfterCherryPick-768x384.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption>https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20220302150549\/AfterCherryPick.jpg<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">#6 Amend for a cleaner win<\/h3>\n\n\n\n<p>Developers make typos. Everyone makes them and sometimes we just need to correct them. Fortunately, there&#8217;s a git concept called <code>amend<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git&nbsp;commit&nbsp;--amend&nbsp;-m&nbsp;\"an&nbsp;updated&nbsp;commit&nbsp;message\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">#7 Delete unnecessary branches and stashes<\/h3>\n\n\n\n<p>A very trivial thing that helps to keep your repo clean is to remove unused branches and stashes, you&#8217;ll be grateful later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">GitKraken<\/h2>\n\n\n\n<p>I want to dedicate a separate section to GitKraken, which is by far my favorite tool to interact with git repositories is GitKraken. We&#8217;re quite fond of it at NubiSoft because of its ability to align our workflows. It&#8217;s not free but it massively streamlines your basic and not-so-basic git tasks.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"457\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1-1024x457.png\" alt=\"\" class=\"wp-image-931\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1-1024x457.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1-300x134.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1-768x343.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1-1200x536.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2022\/07\/image-1.png 1474w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>With just a right click you&#8217;ll be presented with pretty much all the git commands that you need.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this post, I&#8217;ve described how to use some of the most important and useful git concepts. Obviously, there is more to it like tags, dropping commits and so on, but this should get you properly started.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git is by far the most popular Version Control System (VCS) used in web development. According to the latest StackOverflow survey over 90% of respondents use it on a regular basis. The question one would ask is how to use it effectively?This post will guide you through the most best practices, important git commands and [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":940,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_case_study_excerpt":"","footnotes":""},"categories":[4,3],"tags":[246,247],"class_list":["post-926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-productivity","category-software-development","tag-git","tag-vcs"],"_links":{"self":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/comments?post=926"}],"version-history":[{"count":6,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"predecessor-version":[{"id":942,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/926\/revisions\/942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media\/940"}],"wp:attachment":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}