Git Basics: A Beginner's Guide

Whether you're a beginner diving into the world of coding or a developer refining your workflow, Git is an essential tool to manage and collaborate on projects. This guide walks you through the basics, ensuring you’re equipped to start using Git confidently. Bookmark this page for quick reference anytime!

What is Git?

Git is a version control system that tracks changes to your files, making it easy to manage projects, collaborate with teams, and revert to previous versions if needed. It’s like an "undo" button for your code—only much more powerful.

Getting Started with Git

Step 1: Install Git

  1. Visit the Git website.

  2. Download the installer for your operating system.

  3. Follow the installation instructions.

Step 2: Configure Git

Before using Git, set up your name and email:


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

These details will appear in your commit history.

Step 3: Create a New Repository on GitHub

  1. Log in to GitHub.

  2. Click the + icon (top-right corner) → New repository.

  3. Fill in the details:

    • Repository name: Choose a descriptive name (e.g., my-first-project).

    • Description: Add an optional description of your project.

    • Visibility: Public (anyone can see) or Private (only you and collaborators can access).

  4. Click Create repository.


Step 4: Add Code to Your Repository

Option 1: Directly Upload Files on GitHub Website

  1. Open your repository.

  2. Click Add fileUpload files.

  3. Drag and drop your files or click Choose your files.

  4. Add a commit message (e.g., "Initial upload of project files").

  5. Click Commit changes.

Option 2: Upload Files Using Git on Your Computer

  1. Clone the Repository
    Copy the repository URL from GitHub and run:

    
     git clone <repository-url>
    

    Example:

    
     git clone https://github.com/username/my-first-project.git
    
  2. Add Your Code
    Move or copy your project files into the cloned repository folder on your computer.

  3. Stage Your Changes
    Add files to the staging area:

    
     git add .
    
  4. Commit Your Changes
    Save a snapshot of your changes:

    
     git commit -m "Added project files"
    
  5. Push Changes to GitHub
    Send the changes to your repository:

    
     git push origin main
    

Step 5: Collaborate on GitHub

  1. Fork a Repository: Make a copy of someone else’s repository to work on it.

  2. Create Pull Requests: Suggest changes to a repository by creating a pull request.

  3. Manage Issues: Track tasks, enhancements, or bugs in your project.


Step 6: Keep Your Repository Updated

  1. Pull Changes: Fetch and merge changes from the remote repository:

    
      git pull origin main
    
  2. Push Updates: Add new features or changes using the same add, commit, and push process.


Common Git Commands Cheat Sheet

CommandPurpose
git initInitialize a Git repository.
git statusCheck the status of your repository.
git add <file>Stage a specific file.
git add .Stage all changes.
git commit -m "msg"Save changes with a message.
git push origin mainUpload changes to the remote repository.
git pull origin mainSync changes from the remote repository.

Best Practices for Using GitHub

  1. Use Clear Commit Messages: Describe what your commit does in simple terms.

  2. Organize Files: Use meaningful names and structure your project logically.

  3. Add a README: Include a README.md file to describe your project, usage, and setup.

  4. Use .gitignore: Exclude unnecessary files (e.g., system files, logs) from your repository.


Conclusion

Using GitHub for your projects enhances collaboration, keeps your code organized, and provides a safety net with version control. With practice, these steps will become second nature.