Git and GitHub Training Document

This Git and GitHub training guide is your go-to resource for learning version control. Whether you’re new to Git or looking to refine your skills, this guide provides clear, step-by-step instructions to help you manage your code and collaborate effectively.

 

Table of Contents

  1. Introduction to Git and GitHub
  2. Setting Up Git
  3. Creating and Managing a Local Repository
  4. Branching and Merging
  5. Remote Repositories and GitHub
  6. Collaborating with GitHub
  7. Advanced Git Techniques
  8. Best Practices
  9. Troubleshooting and Common Commands
  10. Additional Resources

1. Introduction to Git and GitHub

What is Git?
Git is a distributed version control system that allows multiple developers to work on a project simultaneously. It tracks changes in source code during software development, enabling you to revert to previous states, collaborate on code, and manage large codebases.

What is GitHub?
GitHub is a web-based platform that uses Git for version control. It provides a collaborative environment where you can share your projects, contribute to open-source software, and manage repositories online.

Why Use Git and GitHub?

  • Version Control: Keep track of every change made to your codebase.
  • Collaboration: Work on projects with others, regardless of location.
  • Backup: Securely store your code online.
  • Open Source: Contribute to and use open-source projects.

2. Setting Up Git

Installing Git

  • Windows: Download and install Git from Git for Windows.
  • macOS: Install Git using Homebrew: brew install git
  • Linux: Install Git using your package manager (e.g., apt-get, yum): sudo apt-get install git

Configuring Git

  • Set up your username and email:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
  • Check your configuration:
    git config --list

3. Creating and Managing a Local Repository

Initializing a Git Repository

  • Navigate to your project directory:
    cd path/to/your/project
  • Initialize a new Git repository:
    git init

Staging and Committing Changes

  • Stage files for commit:
    git add .
  • Commit changes with a message:
    git commit -m "Your commit message"

Viewing Commit History

  • View the commit history:
    git log

4. Branching and Merging

Creating and Switching Branches

  • Create a new branch:
    git checkout -b new-branch-name
  • Switch to an existing branch:
    git checkout branch-name

Merging Branches

  • Merge a branch into the current branch:
    git merge branch-name

Handling Merge Conflicts

  • If conflicts arise, edit the conflicting files, then:
    git add .
    git commit -m "Resolved merge conflict"

5. Remote Repositories and GitHub

Setting Up a GitHub Account

  • Sign up at GitHub.

Creating a Remote Repository

  • Create a new repository on GitHub.
  • Link your local repository to the remote repository:
    git remote add origin https://github.com/yourusername/your-repo-name.git

Pushing Local Changes to GitHub

  • Push your changes to GitHub:
    git push -u origin main

Pulling Changes from GitHub

  • Pull changes from the remote repository:
    git pull origin main

Cloning a Repository

  • Clone a repository from GitHub:
    git clone https://github.com/username/repo-name.git

6. Collaborating with GitHub

Forking and Cloning a Repository

  • Fork a repository on GitHub, then clone it:
    git clone https://github.com/yourusername/forked-repo.git

Creating and Managing Pull Requests

  • After making changes, push them to your branch and create a pull request on GitHub.

Collaborating on Open Source Projects

  • Contribute by forking, cloning, and submitting pull requests to open-source projects.

7. Advanced Git Techniques

Interactive Rebase

  • Rebase interactively to clean up commits:
    git rebase -i HEAD~3

Git Tags

  • Tag a specific commit:
    git tag -a v1.0 -m "Version 1.0"
    git push origin v1.0

Stashing Changes

  • Stash your changes temporarily:
    git stash
  • Apply stashed changes:
    git stash apply

Git Hooks

  • Use Git hooks for automation by editing scripts in the .git/hooks directory.

8. Best Practices

Writing Good Commit Messages

  • Use clear, descriptive commit messages.
  • Follow the format: type: subject.

Regular Commits and Pushes

  • Commit and push often to avoid losing work and to keep your repository up to date.

Managing Branches Effectively

  • Keep your main branch stable; develop new features in separate branches.

Using .gitignore

  • Use .gitignore to exclude files from version control (e.g., sensitive data, build files).