<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gavin Li</title>
    <description>The latest articles on DEV Community by Gavin Li (@tauag).</description>
    <link>https://dev.to/tauag</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F408862%2F84e64cd8-348c-401d-942a-731a7a81aca5.jpeg</url>
      <title>DEV Community: Gavin Li</title>
      <link>https://dev.to/tauag</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tauag"/>
    <language>en</language>
    <item>
      <title>Quick Start Guide to Git</title>
      <dc:creator>Gavin Li</dc:creator>
      <pubDate>Mon, 15 Jun 2020 01:15:20 +0000</pubDate>
      <link>https://dev.to/tauag/quick-start-guide-to-git-2of5</link>
      <guid>https://dev.to/tauag/quick-start-guide-to-git-2of5</guid>
      <description>&lt;p&gt;This guide is written for first time users of Git. My goal is to help you understand the basics of Git and to provide a reference for when you inevitably forget if you were supposed to add before committing... Or was it the other way around? Don't worry, this has happened to everyone, I promise.&lt;/p&gt;

&lt;p&gt;In this guide I will be using Github repo to walk through how to do some common Git workflows but the steps will be the same for any repository hosted on other platforms based on Git (GitLab, BitBucket, etc).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Git and why should I use it?
&lt;/h2&gt;

&lt;p&gt;Git is a distributed version control system (VCS). The point of version control is to provide a history of changes to a file or a set of files so that you can revert back to any previous version.&lt;/p&gt;

&lt;p&gt;Since Git is distributed, it does not require a central server for storage, you have a copy of the entire code base and its history on your machine. This lets you modify file(s) or revert to a previous version if you mess up without affecting anyone else's work! Also, &lt;code&gt;git blame&lt;/code&gt; is your friend whenever you ask yourself "&lt;strong&gt;who wrote this??&lt;/strong&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  The Git Workflow
&lt;/h2&gt;

&lt;p&gt;There are 4 states in your Git workflow: &lt;strong&gt;Working Directory&lt;/strong&gt;, &lt;strong&gt;Staging Area&lt;/strong&gt;, &lt;strong&gt;Local Repo&lt;/strong&gt; and &lt;strong&gt;Remote Repo&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Working Directory
&lt;/h4&gt;

&lt;p&gt;All modifications to files are initially saved to the working directory. Changes here are considered "unsaved" by Git and can be freely altered without affecting your local main copy.&lt;/p&gt;

&lt;h4&gt;
  
  
  Staging Area
&lt;/h4&gt;

&lt;p&gt;The staging area holds all of the file edits that will be included in your next commit. You can "stage" any file you have added or made modifications to from your Working Directory.&lt;/p&gt;

&lt;h4&gt;
  
  
  Local Repository
&lt;/h4&gt;

&lt;p&gt;This is your local main copy of the repository. Your local repository is your source of truth and can be used to recover any files you accidentally deleted or made unwanted modifications to. This is also the place where you can push your changes to a remote repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Remote Repository
&lt;/h4&gt;

&lt;p&gt;Like the Local Repo, this is the main copy of the repository but is hosted on a server somewhere (such as on Github, GitLab, etc.) instead of being on your local machine. Multiple developers can pull from the remote repository and it serves as the source of truth for everyone working together on the same code base.&lt;/p&gt;

&lt;p&gt;Here is a diagram of the 4 states, we will go this in detail in later sections:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ut1NyPMr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dg2s8w9xeyih3recxfpq.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ut1NyPMr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dg2s8w9xeyih3recxfpq.JPG" alt="Git Workflow Diagram"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Git Workflow Diagram&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How do I get a Local Repository?
&lt;/h2&gt;

&lt;p&gt;There are two main ways of getting a local repo:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There already exists some remote repository and you clone it to your machine.&lt;/li&gt;
&lt;li&gt;You make your own local directory and turn it into a Git repo.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Cloning a Repository
&lt;/h4&gt;

&lt;p&gt;If there is an existing Git repository that you would like to clone, such as an open source project you want to contribute to or if your job uses Git, then you can simply clone it.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone https://github.com/Tauag/git-sample.git&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will create a git-sample directory with a &lt;code&gt;.git&lt;/code&gt; file inside of it. It will also have all of the files contained in the remote repo and the complete commit history.&lt;/p&gt;

&lt;p&gt;You can also name the folder something different by adding target directory to the command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone https://github.com/Tauag/git-sample.git other-directory-name&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;It does the same thing as the previous command but the repo will be saved to &lt;code&gt;other-directory-name&lt;/code&gt; folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  Initializing a Repository
&lt;/h4&gt;

&lt;p&gt;You can convert an existing directory on your machine into a Git repo. First you want to navigate to the root of the folder that you want to convert (usually the command is &lt;code&gt;cd&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Now you want to run&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git init&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will create a .git subdirectory in your folder. You may want to tell git to start tracking some of the files in your folder with &lt;code&gt;git add&lt;/code&gt; and &lt;code&gt;git commit&lt;/code&gt; commands. I will go over these commands do in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  But what about saving to the Remote Repository?
&lt;/h2&gt;

&lt;p&gt;Now that you have your local repository set up lets move on to pushing your changes up to a remote. For the following examples I will be using the git-sample repo from the previous section.&lt;/p&gt;

&lt;p&gt;First you want to make some edits; write some code, add much needed documentation, done yet? Great!&lt;/p&gt;

&lt;h4&gt;
  
  
  Stage Your Changes
&lt;/h4&gt;

&lt;p&gt;Use &lt;code&gt;git status&lt;/code&gt; to see what files you have changed on your Working Directory. I added a couple of lines to my README file and this is what I see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git checkout -- &amp;lt;file&amp;gt;..." to discard changes in working directory)

    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To stage README.md do&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add README.md&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will save the current edits to the Staging Area (also called staging your edits) and make it available for commits. If you &lt;code&gt;git status&lt;/code&gt; again you should now see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD &amp;lt;file&amp;gt;..." to unstage)

    modified:   README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Keep in mind that if you now make additional changes to README.md the new edits will not be staged. &lt;code&gt;git add&lt;/code&gt; only stages the edits that it could see &lt;strong&gt;at the time the command was called&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Commit Your Changes
&lt;/h4&gt;

&lt;p&gt;Now that you have made all the changes you want and put them in the Staging Area, you can now commit to your Local Repository. Go ahead and so the following:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git commit -m "Modified my README"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Lets break down this command. &lt;code&gt;git commit&lt;/code&gt; tells Git to pack up all of the staged files into one commit and add it to the end of your Local Repository's history log. &lt;code&gt;-m&lt;/code&gt; is a message flag that tells Git to use the next string as the commit message. &lt;code&gt;"Modified my README"&lt;/code&gt; is the string that is passed as an argument for the message flag. You generally want your commit message to concisely describe what you changed.&lt;/p&gt;

&lt;p&gt;If you do &lt;code&gt;git status&lt;/code&gt; now you should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This indicates that your Local Repository has 1 more commit in its history log compared to the Remote Repository's history.&lt;/p&gt;

&lt;h4&gt;
  
  
  Push!
&lt;/h4&gt;

&lt;p&gt;Now that you have successfully committed changes to your Local Repo you probably want to push to the Remote so that your code can be shared to other developers. At this point you want to push your changes.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git push&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Congratulations! You have now pushed your code to the Remote Repo and it is now available for all with access to see.&lt;/p&gt;

&lt;h2&gt;
  
  
  But Git Rejected My Push
&lt;/h2&gt;

&lt;p&gt;Someone else probably pushed some commits up to your branch and you need to pull it down so that your Local Repo is in sync with the Remote Repo.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git fetch&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Will download the new commit history and edits.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git merge&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Will merge the new files that you fetched into your Working Directory, if there are any merge conflicts you will need to resolve those in your preferred IDE/text editor and do &lt;code&gt;git merge --continue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alternatively you can just do &lt;code&gt;git pull&lt;/code&gt; which is just a shorthand command that combines both &lt;code&gt;git fetch&lt;/code&gt; and &lt;code&gt;git merge&lt;/code&gt;. It is recommended to use this command regularly if there are a lot of contributors or if the Remote Repo gets a lot of commit traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  That's Basically It
&lt;/h2&gt;

&lt;p&gt;There are many advanced uses for git but &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;commit&lt;/code&gt;, &lt;code&gt;pull&lt;/code&gt; and &lt;code&gt;push&lt;/code&gt; will be the commands you use the most. The next thing you want to look into is &lt;a href="https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging"&gt;branching&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
