Learn Git – Full Course for Beginners

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

Table of Contents

Introduction

This tutorial aims to guide you through the essentials of using Git and GitHub for version control and collaboration in software development. It covers everything from basic commands to advanced features like branching, merging, and making pull requests, providing you with the knowledge to manage your projects efficiently.

Chapter 1: Introduction to Git

  • Understanding Version Control: Git is a version control system that allows you to track changes in your code, collaborate with others, and revert to previous states of your project easily.
  • Key Tools:
    • GitHub: A platform for hosting Git repositories online, enabling collaboration and sharing.
    • Git CLI: The command-line interface for Git, which is essential for powerful version control.

Getting Started

  • Install Git on your machine from the official website.
  • Familiarize yourself with the command line, as Git is primarily a command-line tool.

Chapter 2: Initializing a Git Repository

  1. Create a New Directory:
    mkdir my_project
    cd my_project
    
  2. Initialize Git:
    git init
    
  3. Check Git Status:
    git status
    

Chapter 3: Basic Git Commands

  • Adding Files: Use git add to stage changes for commit.
    git add filename.txt
    
  • Committing Changes: Commit staged changes with a message.
    git commit -m "Initial commit"
    
  • Viewing Commit History: Use git log to see the commit history.
    git log
    

Chapter 4: Branching and Merging

  • Creating a Branch:
    git checkout -b new-branch
    
  • Switching Branches:
    git checkout main
    
  • Merging Branches:
    git merge new-branch
    

Chapter 5: Resolving Conflicts

  • If you encounter merge conflicts:
    • Open the affected file in an editor.
    • Resolve the conflict markers (<<<<<<, ======, >>>>>>).
    • Stage the resolved file and commit.
    git add filename.txt
    git commit -m "Resolved merge conflict"
    

Chapter 6: Working with GitHub

  • Creating a Remote Repository:
    • Go to GitHub and create a new repository.
  • Adding Remote:
    git remote add origin https://github.com/username/repo.git
    
  • Pushing Changes:
    git push -u origin main
    

Chapter 7: Making Pull Requests

  1. Fork the Repository: Click on the "Fork" button on GitHub.
  2. Clone Your Fork:
    git clone https://github.com/your-username/repo.git
    
  3. Create a New Branch:
    git checkout -b feature-branch
    
  4. Make Changes and Commit:
    git add .
    git commit -m "Added new feature"
    
  5. Push Your Branch:
    git push origin feature-branch
    
  6. Open a Pull Request: Go to your repository on GitHub and select "Pull Requests," then click "New Pull Request."

Chapter 8: Understanding Stashing

  • Stashing Changes: Temporarily save your changes without committing.
    git stash
    
  • Applying Stashed Changes:
    git stash pop
    

Conclusion

You are now equipped with the fundamental skills to use Git and GitHub effectively for version control and collaboration. Start applying these concepts in your projects to enhance your development workflow. As you become more comfortable, explore advanced features like rebasing and using GitHub Actions for CI/CD. Happy coding!