DEV Community

Cover image for Git Crash Course 2021 with GitHub
Coding Jitsu
Coding Jitsu

Posted on • Edited on

Git Crash Course 2021 with GitHub

Let's learn about git! This article is dedicated to beginners and complete crash course on git and GitHub to get you started with your first git project. I will provide some link at the end of this article, so you can deep dive even more.
if you prefer video over article check this out

What is Git?

  • Git is free and open source distributed version control system.
  • Git was created by Linus Torvalds, he also created Linux Kernel.
  • Git is designed to handle everything from small to very large projects with speed and efficiency.
  • Git is easy to learn and has a tiny footprint with lightning fast performance.

what is version control?

Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.

Software developers working in teams are continually writing new source code and changing existing source code. The code for a project, app or software component is typically organized in a folder structure or "file tree". One developer on the team may be working on a new feature while another developer fixes an unrelated bug by changing code, each developer may make their changes in several parts of the file tree and at the end it all gets merged to a single source which can be deployed.

so, let's say you have a project that is deployed. Now, if you want to make any changes to that project for an instance adding an AUTH feature you would probably make a copy of the final version and then make any modification to that copy. That will serve two purposes: 1. you can test your code beforehand, and 2. you have now a history record of your initial codebase and the newer code base.

This above process might just work for you if you are the only developer working on the project and you make modification to your code every now and then.

However, if you have hundred's of developers working on the codebase and every one making thousands of changes every day, you can clearly imagine the above process will be a mess. Git solves this issue.

on a side note: why every software has a three number name, like: macOS Big Sur version- 11.0.1?

Semantic Versioning 2.0.0 ⬅ check this out.

installing Git:

Install Git on Mac OS X: There are several ways to install Git on a Mac. In fact, if you've installed XCode (or it's Command Line Tools), Git may already be installed. To find out, open a terminal and enter git --version.

$ git --version git version 2.7.0 (Apple Git-66)
Enter fullscreen mode Exit fullscreen mode

Apple actually maintain and ship their own fork of Git, but it tends to lag behind mainstream Git by several major versions. You may want to install a newer version of Git using one of the methods below:

Git for Mac Installer

The easiest way to install Git on a Mac is with Homebrew:

  1. Open your terminal and install Git using Homebrew:

    $ brew install git
    
  2. Verify the installation was successful by typing which git --
    version:

     $ git --version git version 2.9.2
    
  3. Configure your Git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create:

   $ git config --global user.name "Emma Paris"
Enter fullscreen mode Exit fullscreen mode
   $ git config --global user.email "user@email.com"
Enter fullscreen mode Exit fullscreen mode
Install Git on Windows
  1. Download the latest Git for Windows installer.
  2. When you've successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation. The default options are pretty sensible for most users.

  3. Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt).

  4. Run the following commands to configure your Git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create:

$ git config --global user.name "Emma Paris" $ git config --global user.email "eparis@atlassian.com"
Enter fullscreen mode Exit fullscreen mode

Git repository

We will be working with GitHub. If you don't have an account, go ahead and open an account in GitHub. Alternatively if you prefer to use other services like bitbucket or GitLab, feel free to do so, for the most part they all works the same way.

  1. click on the repository:
    image

  2. click on new:
    image

  3. On the next page, name your repo and choose the appropriate options. Optionally I always keep the add a README file unchecked because I usually add my own README.md file with the first commit and also it does give you the second screen shot where you can get some useful git commands.
    image

  4. This is the page you get next if you unchecked the add README file.
    image

as you can see the above instructions gives you a step by step commands to run in your terminal to get started.

The idea is to first initialize the git in your project file.
  • git init will create a folder in your project directory and it will be hidden because you would never have to open that file.
      git init
Enter fullscreen mode Exit fullscreen mode
  • next command to run in the terminal is git status.
      git status 
Enter fullscreen mode Exit fullscreen mode

git status will show you any untracked files meaning any file that has modification but git did not staged it yet. The files needs to be tracked will be in red.
image

  • next command to run is git add <filename> or git add .
      git add .
Enter fullscreen mode Exit fullscreen mode

This . will stage all the files and you will be using this
method for the most part. If you want to just track one specific
file, simply change the . with the filename like

     git add app.js
Enter fullscreen mode Exit fullscreen mode

this command will change the color of the files to green meaning
these has been staged. You can check that by running git status
command again.

image

notice on step 3 git actually showing you which command to run
next

  • Next command to run git commit.
     git commit -m 'first commit'
Enter fullscreen mode Exit fullscreen mode
  • By default git has the branch named as master. However we are going to change the name of the branch to main.
      git branch -M main
Enter fullscreen mode Exit fullscreen mode
  • It's time to link our remote GitHub link to our git project with the bellow command.
      git remote add origin https://github.com/w3tsa/git-crash-course-2021.git
Enter fullscreen mode Exit fullscreen mode
  • finally we are ready to push our code to the repo.
      git push -u origin main
Enter fullscreen mode Exit fullscreen mode

_There are two more commands I want to show you is git clone <repo link> which will clone down a repo from your GitHub to your local machine. The second one is git checkout -b branch-name which will create a separate branch for you to work on instead of main branch.

checkout the video bellow.

useful links:

  1. Git - https://git-scm.com/
  2. How to install Git - https://www.atlassian.com/git/tutorials/install-git
  3. Semantic Versioning 2.0.0 - https://semver.org/

Top comments (0)