Git & GitHub Tutorial – Version Control for Beginners


Learn Git and GitHub basics with this complete tutorial. Covers version control, repositories, commits, branches, merging, and collaboration. Ideal for beginners, developers, and AI enthusiasts to manage code efficiently and work in teams.

1. Introduction to Git & GitHub

Git is a version control system that tracks changes in your code. GitHub is a cloud platform for hosting Git repositories, enabling collaboration with other developers.

Benefits:

  1. Track code changes over time.
  2. Revert to previous versions if needed.
  3. Collaborate with other developers.
  4. Work on multiple features simultaneously using branches.

Best Practices:

  1. Commit small, meaningful changes.
  2. Write clear commit messages.
  3. Regularly push changes to remote repositories.

2. Installing Git

  1. Windows / Mac / Linux: Download from git-scm.com
  2. Verify installation:

git --version

3. Configuring Git

Set your name and email (used for commits):


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

Check configuration:


git config --list

4. Basic Git Commands

Initialize a Repository


git init

Creates a local Git repository.

Check Status


git status

Add Files


git add filename.txt
git add .

Commit Changes


git commit -m "Add initial project files"

View Commit History


git log

Best Practices:

  1. Commit frequently to save progress.
  2. Use descriptive commit messages like "Fix login bug" or "Add data preprocessing script".

5. Branching & Merging

Branches allow multiple features to be developed simultaneously.

Create a Branch


git branch feature-login
git checkout feature-login

Or combine into one command:


git checkout -b feature-login

Merge Branches


git checkout main
git merge feature-login

Best Practices:

  1. Keep main branch stable.
  2. Work on separate branches for new features or bug fixes.
  3. Regularly merge branches to avoid conflicts.

6. GitHub Basics

GitHub hosts your Git repositories online for collaboration.

Create a Repository

  1. Sign up / log in to GitHub.
  2. Click New Repository, enter name, description, and visibility.

Push Local Repo to GitHub


git remote add origin https://github.com/username/repo.git
git branch -M main
git push -u origin main

Clone Repository


git clone https://github.com/username/repo.git

Pull Changes


git pull origin main

Best Practices:

  1. Use meaningful repository names.
  2. Add a README.md to describe the project.
  3. Use .gitignore to exclude unnecessary files.

7. Collaboration Workflow

  1. Fork a repository.
  2. Clone it locally.
  3. Create a branch for your changes.
  4. Commit and push changes.
  5. Open a Pull Request on GitHub.

Best Practices:

  1. Review pull requests carefully.
  2. Communicate changes clearly in PR descriptions.
  3. Always sync your fork with the main repository regularly.

8. Summary & Best Practices

  1. Git tracks code changes; GitHub hosts repositories online.
  2. Use branches for features and bug fixes.
  3. Commit frequently with meaningful messages.
  4. Push and pull changes to keep code synced with the team.
  5. Collaborate efficiently using forks and pull requests.

By mastering Git & GitHub, beginners can manage projects effectively, track changes, and collaborate with other developers in AI, software, and web projects.