{"id":5934,"date":"2019-06-17T12:00:26","date_gmt":"2019-06-17T16:00:26","guid":{"rendered":"https:\/\/blogs.swarthmore.edu\/its\/?p=5934"},"modified":"2019-06-17T12:01:03","modified_gmt":"2019-06-17T16:01:03","slug":"utilizing-git-for-solo-development","status":"publish","type":"post","link":"https:\/\/blogs.swarthmore.edu\/its\/2019\/06\/17\/utilizing-git-for-solo-development\/","title":{"rendered":"Utilizing Git for Solo Development"},"content":{"rendered":"<h1>Let&#8217;s Git Started<\/h1>\n<p>Bad puns aside, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Git\">Git<\/a> is a very powerful tool. For those who don&#8217;t know, git is a version-control system used for tracking source code during software development. It&#8217;s an amazing tool for team development because it allows multiple people to contribute code to the same project, but did you know that git can do wonders for your solo workflow as well?<\/p>\n<h2>Branches<\/h2>\n<p>With git, there exists a concept called branches. A branch is a simply a pointer to a commit within git. When you initialize a git repo, it will have a default branch called <code>master<\/code>. It is generally considered good practice to keep the <code>master<\/code> branch of a project clean and functional.<\/p>\n<p>For team-based projects, branching allows multiple contributors to push code to the same repository (each contributor would likely have their own branch). The benefit for branching in a team project becomes immediately clear knowing this, but why should we branch as a solo developer? You may ask yourself, &#8220;<em>If I&#8217;m the only one working on a project, why do I should I bother with creating different branches for it?<\/em>&#8220;.<\/p>\n<p>Well, branching is a good practice to get into, even as a solo developer. Using branching, you can ensure that your <code>master<\/code> branch is always working correctly, and maintain an easy to read history of what has been done. Branching also gives you a way to split up features and issues into their own sections. This is extremely useful for larger projects. With branching, you can easily roll back features as well. Let&#8217;s say you you added some pretty crummy code to a feature branch, and it got merged into the <code>master<\/code> branch, breaking it a week later (I&#8217;ll explain the concept of branch merging, called a pull request, later in the article), well this merge can easily be rolled back, thus reverting the <code>master<\/code> branch to a working state.<\/p>\n<figure id=\"attachment_5945\" aria-describedby=\"caption-attachment-5945\" style=\"width: 782px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/git-flow.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-5945 size-full\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/git-flow.png\" alt=\"git branch flow\" width=\"782\" height=\"232\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/git-flow.png 782w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/git-flow-300x89.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/git-flow-768x228.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/git-flow-202x60.png 202w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/a><figcaption id=\"caption-attachment-5945\" class=\"wp-caption-text\">An example git branching strategy for an application that implements a login form, as well as an issue fix.<\/figcaption><\/figure>\n<h2>Creating a Project<\/h2>\n<p>Let&#8217;s step through starting a new project and initialize a git repo for it. Let&#8217;s create an application that displayed &#8220;Hello universe&#8221; (because Hello world programs are <em>so 2018<\/em>).<\/p>\n<h3>Initialization<\/h3>\n<p>In your terminal create a folder for your project, we&#8217;ll call this one &#8220;Hello Universe&#8221;<\/p>\n<p><code>mkdir ~\/HelloUniverse &amp;&amp; cd ~\/HelloUniverse<br \/>\ngit init<\/code><\/p>\n<p>Great! We now have our project initialized as a git repository! When we initialize a git repository, it automatically creates the <code>master<\/code> branch for it. You can verify what branch you are on by issuing the command <code>git status<\/code>.<\/p>\n<p>As you can see (via that git status output), we&#8217;re on the <code>master<\/code> branch, and do not have any commits yet.<\/p>\n<h3>Commits<\/h3>\n<p>Commits are essentially recorded changes to the repository. So, let&#8217;s say we added a few files to the <code>~\/HelloUniverse<\/code> directory and want to add these to the git repo, we can do so by issuing a <code>git add .<\/code> command to add all new files within the current directory to the git project, then a <code>git commit -m \"your commit messages\"<\/code> command to commit the changes to the current branch.<\/p>\n<p>Here, we will create an <code>index.js<\/code> file that will output &#8220;Hello Universe&#8221; to the console, add it to our git repository, but instead of adding this directly to the <code>master<\/code> branch of the project, we will create a new branch called <code>HelloUniverse<\/code> and commit our changes to that branch. What this does is it allows the <code>master<\/code> branch of our project to remain clean and working.<\/p>\n<p>We can create and switch to a new branch by issuing a <code>git checkout -b [branch name]<\/code> command. Once we&#8217;ve switched branches, issue a <code>git status<\/code> command to verify what branch you are on.<\/p>\n<p><code>git checkout -b HelloUniverse<br \/>\ngit status<\/code><\/p>\n<p>Your terminal should verify you&#8217;ve switched to a new branch.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-5937\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.25.57-1024x71.png\" alt=\"git branch example\" width=\"720\" height=\"50\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.25.57-1024x71.png 1024w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.25.57-300x21.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.25.57-768x53.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.25.57-270x19.png 270w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.25.57.png 1380w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>Now we can go ahead and create our <code>index.js<\/code> file, and add it to our newly created branch. For the sake of simplicity, I&#8217;ll create <code>index.js<\/code> in one line, but in normal practice, you&#8217;d likely be using a text editor or IDE to create this file.<\/p>\n<p><code>echo \"console.log('Hello Universe')\" &gt;&gt; index.js<br \/>\ngit add index.js<br \/>\ngit commit -m \"Added a friendly greeting.\"<\/code><\/p>\n<p><strong>Pro Tip:<\/strong> Commit often! Each commit should be representative of a minimal piece of work. Committing often gives you a history of what you&#8217;ve done, and will allow you to roll back any changes if you accidentally introduce something breaking to your project.<\/p>\n<p><strong>Another Pro Tip<\/strong>: Your commit messages should be minimal, but also explain what has been done. An example of a good commit message would be &#8220;Fixed typo in README.md&#8221;. Commit messages can span multiple lines for larger changes.<\/p>\n<h3>Creating the Origin<\/h3>\n<p>We have our files committed to our <code>HelloUniverse<\/code> branch, but we don&#8217;t have the repository stored anywhere else! How can we remedy this? Head over to <a href=\"https:\/\/github.com\">https:\/\/github.com<\/a> (or whatever cloud service you are using) and create your repository, go through the process to initialize the repository with a <code>README.md<\/code> file, and add the created repository as a source for your local repository.<\/p>\n<p>For a repository on GitHub (assuming you are using HTTPS)<\/p>\n<p><code>git remote add origin https:\/\/github.com\/[your username]\/[your git repo]<\/code><\/p>\n<p>If you are using SSH (which in most cases, you should be) you&#8217;ll have to <a href=\"https:\/\/help.github.com\/en\/articles\/adding-a-new-ssh-key-to-your-github-account\">add your SSH public key to your GitHub account<\/a>, then issue this command (notice that the only differences are the origin url).<\/p>\n<p><code>git remote add origin git@github.com:[your username]\/[your git repo]<\/code><\/p>\n<p>Let&#8217;s break this command down a bit before we actually push our code there. What this command does is adds a remote repository url to your existing git repo. This allows you to push any committed changes that you&#8217;ve made locally to the remote repository. The origin part of the command is just an alias for your system that points to a remote repository.<\/p>\n<p><strong>Pro tip<\/strong><em>: <\/em>At any time, you can see what your origin is by issuing a <code>git remote -v command<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5936 size-large\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.19.56-1024x106.png\" alt=\"Git remote -v example\" width=\"720\" height=\"75\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.19.56-1024x106.png 1024w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.19.56-300x31.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.19.56-768x80.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.19.56-270x28.png 270w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.19.56.png 1348w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<h3>Pushing your Changes<\/h3>\n<p>So, we&#8217;re still on the <code>HelloUniverse<\/code> branch, we have our code created, we have our repository created and added, and now we want to push our local changes to the remote repository.<\/p>\n<p>Once the changes are committed, we can issue this command to push our changes to GitHub. The format for this command is <code>git push [source] [branch name]<\/code>.<\/p>\n<p><code>git push origin HelloUniverse<\/code><\/p>\n<p><strong>Note<\/strong><em>: <\/em>If you are using a fresh install of git, you may need to first identify yourself to git via the <code>git config<\/code> command. You may also have to generate a <code>README<\/code> or other file to initially initialize the <code>master<\/code> branch of the repo. GitHub can do this for you upon creating a repository.<code><br \/>\n<\/code><br \/>\nYou should see something like the this on a successful push.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5938\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.31-1024x588.png\" alt=\"git commit to branch\" width=\"720\" height=\"413\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.31-1024x588.png 1024w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.31-300x172.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.31-768x441.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.31-104x60.png 104w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.31.png 1608w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>Take note of the <code>Create a pull request for 'HelloUniverse'<\/code> line, as that\u00a0 is the next step!<\/p>\n<p>If you head over to your GitHub repository, you can verify that GitHub sees the <code>HelloUniverse<\/code> branch, and it will ask you if you want to issue a pull request to merge this branch with the <code>master<\/code> branch.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5939\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.47-1024x639.png\" alt=\"github pull request example\" width=\"720\" height=\"449\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.47-1024x639.png 1024w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.47-300x187.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.47-768x479.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.47-96x60.png 96w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.29.47.png 2028w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<h2>Pull Requests<\/h2>\n<p>A pull request is a powerful concept, and a staple of git workflows. A pull request is basically a request to merge one branch with the <code>master<\/code> branch of a repo. So when I issue a pull request for the <code>HelloUniverse<\/code> branch I am telling GitHub: <strong>&#8220;My <code>HelloUniverse<\/code> branch is tested and working, and I would like to merge it into the <code>master<\/code> branch.&#8221;<\/strong> Merging branches sounds like it could get messy, luckily GitHub (as well as other source control platforms) does a good job of checking for any potential merge errors.<\/p>\n<p>When you head to GitHub and click the <strong>[Compare &amp; pull request]<\/strong> button, that tool will run, and provide any feedback for you to fix potential issues before the merge (don&#8217;t worry, one of the great things about using a <strong>code -&gt; branch -&gt; pull request workflow<\/strong> is that if a bad merge occurs, it can easily be rolled back).<\/p>\n<p>When you click on the pull request button described above, GitHub will greet you with the following screen.<a href=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13.png\"><br \/>\n<\/a><a href=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5942 size-large\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13-1024x920.png\" alt=\"Issuing a pull request\" width=\"720\" height=\"647\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13-1024x920.png 1024w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13-300x269.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13-768x690.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13-67x60.png 67w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.13.png 1982w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>And now we can finalize our pull request, which will merge <code>HelloUniverse<\/code> into <code>master<\/code>.<\/p>\n<p><a href=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5940 size-large\" src=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23-1024x1006.png\" alt=\"Issuing a pull request with github\" width=\"720\" height=\"707\" srcset=\"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23-1024x1006.png 1024w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23-300x295.png 300w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23-768x754.png 768w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23-61x60.png 61w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23-50x50.png 50w, https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/Screenshot-2019-06-17-10.40.23.png 1554w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>It&#8217;s that easy! Clicking the <strong>[Merge pull request]<\/strong> button will run the merge. You should now see <code>index.js<\/code> (or whatever files you added) in your <code>master<\/code> branch on the repository&#8217;s page. Woohoo!<\/p>\n<h2>TLDR<\/h2>\n<ul>\n<li>Git can be leveraged as a powerful tool for solo developers, even though it&#8217;s primarily geared towards team-based workflows.<\/li>\n<li>Adopting a branch-heavy workflow allow you to keep your code separate from the <code>master<\/code> branch.<\/li>\n<li>Committing often allows you to track and rollback problematic changes.<\/li>\n<li>Commit messages should be minimal, yet descriptive enough to represent what has been changed.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s Git Started Bad puns aside, Git is a very powerful tool. For those who don&#8217;t know, git is a version-control system used for tracking source code during software development. It&#8217;s an amazing tool for team development because it allows &hellip; <a href=\"https:\/\/blogs.swarthmore.edu\/its\/2019\/06\/17\/utilizing-git-for-solo-development\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Utilizing Git for Solo Development<\/span><\/a><\/p>\n","protected":false},"author":70,"featured_media":5935,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,303],"tags":[90,325,274,150,151],"class_list":{"0":"post-5934","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-academic-technology","8":"category-development","9":"tag-featured","10":"tag-git","11":"tag-github","12":"tag-howto","13":"tag-tips-2","15":"fallback-thumbnail"},"jetpack_featured_media_url":"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2019\/06\/branchallthethings.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/ph2nPL-1xI","_links":{"self":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts\/5934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/comments?post=5934"}],"version-history":[{"count":10,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts\/5934\/revisions"}],"predecessor-version":[{"id":5954,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts\/5934\/revisions\/5954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/media\/5935"}],"wp:attachment":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/media?parent=5934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/categories?post=5934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/tags?post=5934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}