Git and GitHub Tutorial for Beginners

3 min read 5 months ago
Published on Aug 25, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a step-by-step guide on using Git and GitHub for source control management. Whether you're a beginner or looking to refresh your skills, you'll learn how to install, configure, and utilize Git and GitHub effectively. By the end, you'll have a solid understanding of version control and collaboration using these powerful tools.

Step 1: Understand Git

  • Git is a distributed version control system that helps track changes in files and coordinate work among multiple people.
  • It allows multiple versions of a project to be maintained, making it easier to collaborate and manage changes over time.

Step 2: Install Git

  • Download Git from the official website: git-scm.com.
  • Follow the installation instructions for your operating system (Windows, macOS, or Linux).

Step 3: Configure Git

  • Set your username and email for commits:
    git config --global user.name "YOUR NAME"
    git config --global user.email "YOUR EMAIL"
    
  • Optionally, set the default branch to main:
    git config --global init.defaultBranch main
    

Step 4: Get Help with Git

  • Use the following commands to access help:
    • For command-specific help:
      git help COMMAND
      
    • To display general help:
      git COMMAND -h
      

Step 5: Initialize a Repository

  • Create a new Git repository in your project folder:
    git init
    

Step 6: Check Git Status

  • View the current state of your repository:
    git status
    

Step 7: Track and Untrack Files

  • To track new files:
    git add FILENAME
    
  • To untrack files that are already tracked:
    git rm --cached FILENAME
    

Step 8: Use .gitignore

  • Create a .gitignore file in your repository to specify files and directories that Git should ignore. This prevents unnecessary files from being tracked.

Step 9: Stage and Commit Changes

  • Stage all changes:
    git add --all
    
  • Commit your changes with a meaningful message:
    git commit -m "Your commit message"
    

Step 10: View Changes and Differences

  • Check unstaged changes:
    git diff
    
  • View commit history:
    git log
    

Step 11: Amend a Commit

  • If you need to modify the last commit:
    git commit --amend -m "New commit message"
    

Step 12: Reset and Restore Files

  • To reset to the last commit:
    git reset
    
  • To restore a deleted file:
    git checkout -- FILENAME
    

Step 13: Branching and Merging

  • Create a new branch:
    git branch NEW_BRANCH_NAME
    
  • Switch to a branch:
    git switch NEW_BRANCH_NAME
    
  • Merge branches:
    git merge BRANCH_NAME
    

Step 14: Set Up GitHub

  • Create a GitHub account at github.com.
  • Create a new repository on GitHub.

Step 15: Push Local Repository to GitHub

  • Link your local repository to the GitHub repository:
    git remote add origin REPOSITORY_URL
    
  • Push your local commits to GitHub:
    git push -u origin main
    

Step 16: Collaborate Using GitHub

  • Use GitHub features like Issues and Pull Requests to manage tasks and collaborate with others.
  • Always fetch and pull updates from the remote repository to keep your local repository synchronized:
    git fetch
    git pull
    

Conclusion

Now you have a foundational understanding of Git and GitHub, including installation, configuration, and basic commands for managing your code. As you continue to work with these tools, consider exploring advanced features like rebasing, resolving merge conflicts, and utilizing GitHub Actions for automation. Start practicing by creating your own projects and collaborating with others on GitHub!