Git and GitHub - 0 Experience to Professional in 1 Tutorial (Part 1)

4 min read 1 year ago
Published on Aug 28, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to guide you through the basics of Git and GitHub, taking you from no experience to a professional understanding in just a few steps. By following this guide, you'll learn how to install Git, set up your projects, manage your version control effectively, and understand key concepts such as branching and restoring previous versions. This foundational knowledge is essential for any developer working on collaborative projects or managing code.

Step 1: Understand What Git Is

  • Git is a version control system that helps track changes in your code.
  • It allows multiple developers to work on a project simultaneously without overwriting each other's work.
  • Familiarize yourself with important terms:
    • Repository: A storage space for your project.
    • Commit: A snapshot of your project at a specific point in time.
    • Branch: A separate line of development.

Step 2: Install Git

  • Go to the Git downloads page.
  • Choose the installer for your operating system (Mac or Windows) and download it.

Mac Installation

  1. Open the downloaded installer.
  2. Follow the prompts to complete the installation.
  3. Verify the installation by opening Terminal and typing:
    git --version
    

Windows Installation

  1. Open the downloaded .exe file.
  2. Follow the installation wizard’s steps.
  3. Verify the installation by opening Command Prompt and typing:
    git --version
    

Step 3: Set Up Your First Project

  • Create a new directory for your project using the command line:
    mkdir my-first-git-project
    cd my-first-git-project
    
  • Initialize a new Git repository:
    git init
    

Step 4: Configure Git

  • Set your username and email, which will be associated with your commits:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    

Step 5: Create a Version

  • Create a new file in your project directory, for example, index.html.
  • Add some content to the file.
  • Stage the file for commit:
    git add index.html
    
  • Commit the changes:
    git commit -m "Initial commit with index.html"
    

Step 6: View History and Edit a Commit

  • To see your commit history, use:
    git log
    
  • If you need to edit the last commit message, you can do so with:
    git commit --amend -m "Updated commit message"
    

Step 7: Visualize Git and Understand Fundamentals

  • Use visualization tools or commands like git status to see the current state of your repository and git diff to see changes.

Step 8: Understand Staging Area and Working Area

  • The working area is where you make changes, and the staging area is where you prepare changes to be committed.
  • Remember to always stage changes before committing.

Step 9: Complete the Version History

  • Keep committing frequently to build a comprehensive history of your project.

Step 10: View Previous Versions of the Code

  • Use:
    git checkout <commit_hash>
    
  • Replace <commit_hash> with your specific commit ID to revert to a previous version of your project.

Step 11: Restore Code to a Previous Version

  • To restore a file to its state in a previous commit, use:
    git checkout <commit_hash> -- <filename>
    

Step 12: Intro to Git Branching

  • Create a new branch for features:
    git checkout -b new-feature
    
  • Switch back to the main branch using:
    git checkout main
    

Step 13: Restore Code Like Google Docs

  • Utilize the history feature to revert files easily, similar to document recovery in Google Docs.

Step 14: Explore Extra Features

  • Git Aliases: Create shortcuts for common commands.
    git config --global alias.co checkout
    
  • Git Ignore: Create a .gitignore file to exclude files from being tracked by Git.
  • Removing Git: If you need to remove a Git repository, simply delete the .git folder in your project directory.

Conclusion

By following these steps, you have set up Git on your system, created your first repository, and learned fundamental commands for version control. This foundational knowledge is critical for collaborative development and managing your code effectively. For further learning, consider exploring GitHub to host your repositories and collaborate with others. Happy coding!