Back

Git for Absolute Beginners: A Comprehensive Guide

October 11, 2024
Today

Git is an indispensable tool for modern software development, enabling efficient version control and collaboration. This guide will walk you through the fundamental Git commands every beginner should know, from installation to basic workflow.

Getting Started with Git

Installation and Setup

Before diving into Git commands, you need to install Git on your system:

  1. Download Git from the official website: git-scm.com

  2. After installation, verify the installed version:

    Terminal
    git --version

    or

    Terminal
    git -v

Initial Configuration

Configure your Git identity with these global settings:

Terminal
git config --global user.name "Your Name"
git config --global user.email "You@Email.com"
git config --global init.defaultBranch main

These commands set up your name, email, and default branch name for all your Git repositories.

Essential Git Commands for Beginners

Creating and Cloning Repositories

  1. Initialize a new repository:

    Terminal
    git init

    Use this command in an existing project directory to start tracking it with Git.

  2. Clone an existing repository:

    Terminal
    git clone <url-from-github>

    Example:

    Terminal
    git clone https://github.com/Edward-126/template-next.git

    This command creates a local copy of a remote repository.

Checking Repository Status

To see the current state of your repository:

Terminal
git status

For a more concise status overview:

Terminal
git status -s

Adding and Committing Changes

  1. Add files to the staging area:

    Terminal
    git add <filename>

    Example:

    Terminal
    git add index.html

    To add all changed files:

    Terminal
    git add .
  2. Commit staged changes:

    Terminal
    git commit -m "Short message"

    This creates a new commit with the staged changes and a descriptive message.

  3. Combine adding and committing:

    Terminal
    git commit -a -m "Short message"

    This command stages all modified files and creates a new commit in one step.

Managing Changes

  1. Discard changes in a file:

    Terminal
    git restore <filename>
  2. Unstage a newly added file:

    Terminal
    git rm --cached <filename>
  3. Unstage a previously modified file:

    Terminal
    git restore --staged <filename>

Connecting to GitHub

To link your local repository to GitHub:

Terminal
git remote add origin <gh-url>
git branch -M main
git push -u origin main

Replace <gh-url> with your GitHub repository URL.

Understanding File Statuses

Git tracks the status of your files:

  • Untracked: New files not yet added to the repository (U in VS Code)
  • Unmodified: Tracked files without changes
  • Modified: Tracked files with changes (M in VS Code)
  • Staged: Files added to the staging area, ready for commit (A in VS Code)
  • Ignored: Files matching patterns in .gitignore (grayed out in VS Code)

Basic Git Workflow

  1. Create new files or make changes to existing files.
  2. Stage and commit changes:
    Terminal
    git commit -a -m "Short detail message about changes"
  3. Push changes to the remote repository:
    Terminal
    git push

Pro Tip: Avoiding Repeated Credential Requests

To prevent Git from asking for credentials with every push:

Terminal
git config --global credential.helper 'cache --timeout=3600'

This caches your credentials for an hour (3600 seconds).

Conclusion

This guide covers the essential Git commands for beginners. As you become more comfortable with these basics, you can explore more advanced Git features to enhance your development workflow. Remember, practice is key to mastering Git, so don't hesitate to experiment in your projects!

Happy coding, and feel free to reach out if you have any questions or need further clarification on any Git concepts!