Posts Getting Started with Git: A Beginner's Guide
Post
Cancel

Getting Started with Git: A Beginner's Guide

For many self-taught web developers, especially those who have worked solo, version control systems like Git can seem daunting. This guide aims to simplify Git, helping you understand its basics and how to get started with it effectively.

“I’m an egotistical bastard, and I name all my projects after myself. First Linux, now Git.” - Linus Torvalds, creator of Git

Prerequisites

  • Basic ability to create and upload a website.

Goals

  • Understand what Git is and its usefulness.
  • Learn how to create a local project and deploy it to a live server using Git via the command line.

What is Git?

Git is a distributed version control system (VCS) designed to track changes in source code during software development. It enables multiple developers to work on the same project by using branches.

Note: Git is different from GitHub. Git is the system for managing version control, while GitHub is a platform for hosting Git repositories.

Git vs. FTP

FTP

Using FTP (File Transfer Protocol), files are uploaded directly to a server.

Process:

Local Environment ⇋ FTP Program ⇋ Live Server

For example:

file:///Users/you/projectTransmit/WinFTPhttps://example.com

Git

With Git, changes are managed through a repository.

Process:

Local Environment ⇋ Git Repository ⇋ Live Server

For example:

file:///Users/you/projectGitHub.comhttps://example.com

Note: Tower, SourceTree, GitHub Desktop - these are GUIs (Graphical User Interfaces) for Git, offering a more visual interface. However, this guide focuses on using Git through the command line.

Step 1: Installation

Mac

  1. Open the Terminal application.
  2. Check if Git is installed by typing:
    1
    
    git --version
    
    • If Git is installed, a version number will appear.
    • If not, install Xcode from the App Store, and enable Command Line Tools via Xcode > Preferences > Downloads.
  3. (Optional) Install Homebrew, a package manager for macOS that simplifies software installation.

Windows

  1. Download and install Git for Windows.
  2. Use the Git Bash program, which emulates a Unix-style command line, compatible with all Git commands.

Step 2: Create an Online Git Repository

  1. Create an account on GitHub.
  2. Create a new repository (repo) without initializing with a README.md or .gitignore. Name it project (or any name you prefer). Your repo URL will look like https://github.com/your-username/project.

Step 3: Create a Local Project

If you’re new to the command line, consider reading introductory guides. Below are essential commands:

Basic Command Line Reference

  • pwd - Print Working Directory: Shows your current directory.
  • ls - List Directories: Lists files and directories in your current location.
  • cd - Change Directory: Moves you to a different directory.
  • mkdir - Make Directory: Creates a new directory.

Navigate to your desired location in the Terminal:

  1. Confirm your location:
    1
    
    pwd
    
  2. Create a new directory for your project:
    1
    
    mkdir project-local
    
  3. Move into this new directory:
    1
    
    cd project-local
    

Basic Git Command Reference

  • git config - Configures Git settings.
  • git init - Initializes a new Git repository.
  • git status - Displays the state of the working directory and the staging area.
  • git add - Adds changes to the staging area.
  • git commit - Commits the changes.
  • git push - Uploads local commits to a remote repository.
  • git pull - Fetches and merges changes from a remote repository.

Configure Your Git Account

Set up your global Git configuration with your username and email:

1
2
git config --global user.name "Your Name"
git config --global user.email your.email@example.com

Initialize the Repository

  1. Initialize a Git repository:
    1
    
    git init
    
    • This creates a new subdirectory named .git that contains all the necessary metadata for the repository.
  2. Link your local directory to the GitHub repository:
    1
    
    git remote add origin https://github.com/your-username/project
    

Adding and Committing Files

  1. Add files to your project directory, such as index.html and style.css.

  2. Track these files with Git:
    1
    
    git add .
    
  3. Check the status of your repository:
    1
    
    git status
    
  4. Commit the tracked files:
    1
    
    git commit -am "Initial Commit"
    
  5. Push the changes to GitHub:
    1
    
    git push origin master
    

Now, your files are hosted on GitHub!

Step 4: Deploy to a Live Server

If you have SSH access to your server, you can deploy your Git-managed project directly:

  1. SSH into your server:
    1
    
    ssh username@your-server.com
    
  2. Navigate to the directory where you want to deploy your project and create a new directory:
    1
    2
    
    mkdir project-remote
    cd project-remote
    
  3. Initialize a Git repository and link it to your GitHub repository:
    1
    2
    
    git init
    git remote add origin https://github.com/your-username/project.git
    
  4. Pull the files from GitHub:
    1
    
    git pull origin master
    

Your website should now be live at your domain!

This guide provides a foundational understanding of Git, from setup to deploying a project. For further reading, explore more advanced topics and best practices in version control.

This post is licensed under CC BY 4.0 by the author.