Git and GitHub

🔧 Git & GitHub • Complete Guide

Git & GitHub: Complete Beginner's Guide

Learn Git and GitHub from scratch with simple explanations, practical commands and real-world development examples.

If you are learning programming, software development, Python, web development, automation or any other technology, sooner or later you will come across two names: Git and GitHub.

Many beginners initially think Git and GitHub are the same thing. They are related, but they are actually different.

In this guide, we will understand what Git is, what GitHub is, why developers use them, how version control works, how repositories work, how branches and merges work, and how developers collaborate on real-world projects.

📌 Getting Started

What is Git?

Git is a distributed version control system used to track changes in files and source code.

In simple words, Git keeps a history of the changes you make to your project.

💡 Real-World Example:

Imagine you are working on a Python project. Today your program works perfectly. Tomorrow you make several changes and suddenly something breaks.

Without version control, finding exactly what changed can be difficult. With Git, you can see the history of your project and go back to an earlier version when required.

☁️ GitHub

What is GitHub?

GitHub is an online platform that hosts Git repositories. It provides tools that help developers store, share and collaborate on software projects.

Git itself can work on your computer without GitHub. GitHub adds an online collaboration layer around Git.

💡 Real-World Example:

Think of Git as the system that records your project's history on your computer, while GitHub is an online place where you can store that repository and collaborate with other developers.

⚖️ Git vs GitHub

Git vs GitHub: What is the Difference?

Git GitHub
Version control system Online platform for Git repositories
Runs locally on your computer Runs primarily as an online service
Tracks project history Hosts and manages repositories
Can work without GitHub Uses Git repositories for source-code collaboration
🕒 Version Control

What is Version Control?

Version control is a system that records changes made to files over time.

Instead of keeping files such as:

project_final.py project_final_v2.py project_final_v3.py project_final_latest.py project_final_latest_REAL.py

Git allows you to maintain a proper history of your project.

💡 Real-World Example:

In a team of developers, several people may work on the same application. Version control helps track who changed what and when, making collaboration much safer.

💻 Installation

Installing Git

Before using Git commands, Git needs to be installed on your computer.

After installation, you can verify it from the terminal.

git --version

If Git is installed correctly, the command will display the installed Git version.

⚙️ Configuration

Configure Git

Git records information about commits. It is common to configure your name and email before starting development.

git config --global user.name "Your Name" git config --global user.email "you@example.com"
💡 Important:

Use the identity you want associated with your Git commits. In professional projects, developers normally configure their work identity according to their team's requirements.

📁 Repository

What is a Git Repository?

A repository, commonly called a repo, is a project managed by Git.

A repository contains your project files along with Git's information about the project's history.

Create a Repository

mkdir my-project cd my-project git init

The git init command initializes Git in the current directory.

💡 Real-World Example:

Suppose you have created a Python project called ExpenseTracker. Running git init turns that project directory into a Git repository.

🔄 Git Workflow

Understanding the Git Working Flow

One of the most important things to understand as a beginner is how changes move through Git.

Working Directory Staging Area Repository

You modify files in your working directory, select the changes you want to include using the staging area, and then save those staged changes into Git's history with a commit.

🔍 Check Changes

git status

The git status command is one of the most useful Git commands for beginners.

git status

It tells you about the current state of your working directory and staging area.

💡 Real-World Example:

Imagine you changed three files in your project but forgot which files you modified. Running git status quickly shows you the current changes.

➕ Staging

git add

The git add command places changes into the staging area.

git add filename.py

You can also stage multiple files:

git add .
💡 Simple Example:

Suppose you changed login.py, database.py and README.md. You may decide which of those changes should be included in your next commit.

💾 Save History

git commit

A commit records a set of staged changes in Git's history.

git commit -m "Add login functionality"

A good commit message should describe what the commit changed.

💡 Real-World Example:

Think of a commit as a meaningful checkpoint in your project. For example:

  • Add user login
  • Fix payment calculation
  • Update database connection
  • Add product search
📜 History

Viewing Git History

Git keeps a history of commits. You can view that history using:

git log

A shorter version can be useful when you want a compact view:

git log --oneline
💡 Real-World Example:

If a bug appears today, a developer can inspect the commit history to understand what changed recently.

🔎 Compare Changes

git diff

The git diff command helps you see changes made to files.

git diff

This is especially useful before committing changes because it allows you to review what you actually modified.

🌿 Branching

What is a Git Branch?

A branch provides an independent line of development within a Git repository.

git branch feature-login git switch feature-login

You can also create and switch to a new branch using:

git switch -c feature-login
💡 Real-World Example:

Suppose your production application is working correctly and you want to add a new payment feature.

Instead of directly changing the main development line, you can create a separate feature branch and develop the payment functionality there.

🌳 Main Branch

What is the main branch?

The main branch is commonly used as the primary branch of a repository. The exact branching strategy can vary between teams and projects.

Developers often create feature branches from the main development line, work on their changes and then merge the completed work back after review and testing.

🔀 Merge

What is Git Merge?

Merging combines changes from one branch into another branch.

git switch main git merge feature-login
💡 Real-World Example:

Imagine three developers are working on an application. One developer creates the login feature while another works on the dashboard. Once their work is complete and reviewed, their changes can be integrated into the appropriate branch.

⚠️ Conflicts

What is a Merge Conflict?

A merge conflict happens when Git cannot automatically determine which version of a change should be kept.

💡 Real-World Example:

Imagine two developers modify the same line of code in different ways. Git cannot safely decide which change is correct.

The developer must review the conflicting section, choose or combine the correct changes and then complete the merge.

git status # Resolve the conflict in the file git add . git commit
☁️ Remote Repository

What is a Remote Repository?

A remote repository is a version of your Git repository stored somewhere other than your local computer, such as a GitHub repository.

This allows developers to share and synchronize their work.

📥 Clone

git clone

The git clone command creates a local copy of an existing remote repository.

git clone https://github.com/username/project.git
💡 Real-World Example:

Suppose your company already has a project on GitHub. Instead of creating the project again from scratch, you clone the repository and start working on your local machine.

🔗 Connect Repositories

Connecting a Local Repository to GitHub

After creating a local Git repository, you can connect it to a GitHub repository using a remote.

git remote add origin https://github.com/username/project.git git remote -v

Here, origin is a commonly used name for the remote repository.

📤 Upload Changes

git push

The git push command sends your local commits to a remote repository.

git push origin main
💡 Real-World Example:

You have completed a feature on your computer and committed it locally. Pushing sends those commits to the remote repository so your team can access them.

📥 Download Changes

git pull

The git pull command retrieves changes from a remote repository and integrates them into your current branch.

git pull origin main
💡 Real-World Example:

If another developer has pushed new code to the shared repository, you can pull those changes to update your local project.

🔄 Synchronization

git fetch

git fetch downloads information about changes from a remote repository without automatically merging those changes into your current branch.

git fetch origin

This can be useful when you want to inspect remote changes before deciding how to integrate them.

🌐 GitHub Repository

Creating a GitHub Repository

A GitHub repository provides an online home for your project.

A typical repository can contain:

  • Source code
  • README documentation
  • Issues
  • Pull requests
  • Branches
  • Project configuration files
  • License information
💡 Real-World Example:

You might create a GitHub repository for your Python automation project and use it to store the source code, documentation and development history.

📖 Documentation

What is a README.md File?

A README file is usually the first document people see when they visit a repository.

It can explain what the project does, how to install it, how to use it and how other developers can contribute.

# Expense Tracker A Python application for tracking personal expenses. ## Installation pip install -r requirements.txt ## Usage python main.py
💡 Real-World Example:

If someone discovers your project on GitHub, a good README can help them understand and run your project without needing to contact you first.

🚫 Ignore Files

What is .gitignore?

The .gitignore file tells Git which files or directories should not normally be tracked.

__pycache__/ *.pyc .env .venv/ .vscode/
💡 Real-World Example:

Python projects often generate temporary files and developers may use local environment files. These files generally do not belong in the source repository.

🔐 Security Reminder:

Never commit passwords, API keys, access tokens or other secrets to a public repository. A .gitignore file can help prevent accidental tracking of some local files, but it is not a substitute for proper secret management.

🐛 Issue Tracking

GitHub Issues

GitHub Issues can be used to track bugs, feature requests, tasks and other work related to a project.

💡 Real-World Example:

Suppose users report that the login button does not work on mobile. A developer can create an issue describing the problem, assign it to a team member and track its progress.

🔍 Code Review

What is a Pull Request?

A pull request, commonly called a PR, is a way to propose changes from one branch or repository to another so that those changes can be reviewed and discussed before being integrated.

💡 Real-World Example:

Imagine you are a developer in a company. You create a branch called feature-payment, implement the payment functionality, push the branch to GitHub and open a pull request.

Other developers can review your code, leave comments, request improvements and eventually approve the changes.

👥 Collaboration

Code Review

Code review allows developers to examine proposed changes before they become part of an important branch.

Reviewers may look for:

  • Logic problems
  • Security issues
  • Performance problems
  • Code readability
  • Testing coverage
  • Consistency with project standards
🍴 Open Source

What is a GitHub Fork?

A fork is your own copy of another GitHub repository under your GitHub account.

Forks are especially common when contributing to open-source projects where you may not have direct permission to push branches to the original repository.

💡 Real-World Example:

You find an open-source project and notice that its documentation has a mistake. You can fork the repository, make the correction in your copy and propose the change to the original project.

⚖️ Important Difference

Clone vs Fork

Clone Fork
Creates a local copy on your computer Creates a GitHub copy under your account
Used for local development Commonly used for contributing to other repositories
Performed using Git Performed through GitHub
⚙️ Automation

GitHub Actions

GitHub Actions provides automation capabilities that can run workflows based on repository events.

For example, a project can automatically run tests whenever new code is pushed or a pull request is opened.

💡 Real-World Example:

Imagine a developer pushes a new feature. Instead of manually running hundreds of tests, an automated workflow can run the test suite and report whether the changes passed.

🏷️ Releases

Git Tags and Releases

Tags can be used to mark specific points in a repository's history.

git tag v1.0.0 git push origin v1.0.0
💡 Real-World Example:

A software project may use tags such as v1.0.0, v1.1.0 and v2.0.0 to identify important versions.

↩️ Undo Changes

Undoing Changes in Git

Git provides several ways to undo or modify changes. The correct command depends on whether the change is unstaged, staged or already committed.

Discard changes in a working file

git restore filename.py

Unstage a file

git restore --staged filename.py
⚠️ Be Careful:

Commands that rewrite or discard Git history can have significant consequences, especially when working with shared branches. Always understand what a command will do before using it on important work.

📦 Temporary Changes

git stash

Sometimes you are working on something but need to temporarily switch to another branch or handle an urgent task.

Git stash can temporarily store your local changes so that your working directory can be cleaned up.

git stash git stash pop
💡 Real-World Example:

You are halfway through a new feature when your team asks you to immediately fix a production issue. You can temporarily stash your unfinished work, switch branches, fix the urgent issue and later restore your changes.

🔄 Advanced Git

What is Git Rebase?

Rebase is an advanced Git operation that moves or reapplies commits on top of another base commit.

It can help maintain a cleaner project history, but it requires more care because rebase can rewrite commit history.

git switch feature-login git rebase main
💡 Beginner Tip:

Understand branches, commits, merging and conflicts first. Rebase is an important advanced Git concept, but it is not something you need to use immediately as a beginner.

👨‍💻 Team Workflow

A Typical GitHub Development Workflow

A simplified workflow used by many development teams can look like this:

Clone Create Branch Write Code git add git commit git push Pull Request Code Review Merge
🚀 Practical Example

Real-World Example: Building a Python Project with Git

Let's imagine you are creating a Python expense-tracking application.

Step 1: Create the project

mkdir expense-tracker cd expense-tracker git init

Step 2: Create your Python files

main.py expense.py README.md

Step 3: Check the repository

git status

Step 4: Stage the files

git add .

Step 5: Create your first commit

git commit -m "Initial project setup"

Step 6: Create a feature branch

git switch -c add-expense-feature

Step 7: Develop the feature

Add your expense functionality and test it locally.

Step 8: Commit your changes

git add . git commit -m "Add expense functionality"

Step 9: Push the branch

git push -u origin add-expense-feature

Step 10: Create a Pull Request

On GitHub, open a pull request so the changes can be reviewed and integrated into the appropriate branch.

📋 Quick Reference

Important Git Commands

Command Purpose
git init Create a Git repository
git status Check repository status
git add Stage changes
git commit Record staged changes
git log View commit history
git diff View changes
git branch Manage branches
git switch Switch branches
git merge Combine branch changes
git clone Copy a remote repository locally
git pull Get and integrate remote changes
git push Send local commits to a remote
git fetch Download remote information
git stash Temporarily store local changes
git restore Restore or unstage changes
🧠 Key Concepts

Important Git & GitHub Concepts to Remember

📁 Repository

A project managed by Git.

💾 Commit

A recorded set of changes in Git history.

🌿 Branch

An independent line of development.

🔀 Merge

Combines changes from different branches.

☁️ Remote

A repository hosted somewhere outside your local machine.

📤 Push

Sends local commits to a remote repository.

📥 Pull

Gets and integrates remote changes.

🔍 Pull Request

A proposal for reviewing and integrating changes.

⚠️ Beginner Mistakes

Common Git & GitHub Mistakes

  • Committing secrets: Never commit passwords, API keys or private credentials.
  • Using unclear commit messages: Prefer messages that explain what changed.
  • Working directly on an important shared branch: Follow your team's branching and review process.
  • Ignoring pull and fetch: Make sure you understand how remote changes are synchronized.
  • Using Git commands without understanding them: Especially be careful with commands that can rewrite or discard history.
  • Uploading unnecessary files: Use an appropriate .gitignore file.
💼 Career

Why Git & GitHub Are Important for Your Career

Git and GitHub are much more than tools you learn for an interview. They are part of the everyday workflow of many software development teams.

💻 Software Development

Track and collaborate on application source code.

🐍 Python Development

Manage Python scripts, applications and packages.

🤖 Automation

Version-control automation scripts and workflows.

🌐 Web Development

Collaborate on frontend and backend applications.

🧠 AI & ML

Manage machine-learning code, experiments and project files.

👥 Team Collaboration

Review code and coordinate development work.

🧭 Learning Roadmap

Git & GitHub Learning Roadmap

If you are completely new to Git and GitHub, you can learn them in the following order:

What is Git? Version Control Repository git add git commit Branches Merge GitHub Push & Pull Pull Requests Code Review GitHub Actions

🎯 Final Thoughts

Git and GitHub may seem confusing when you first see commands such as git add, git commit, git push, git pull and git merge.

But once you understand the basic workflow, everything starts making sense.

The most important idea is simple: Git keeps track of your project's history, while GitHub provides a platform where Git repositories can be hosted and collaboratively developed.

Start with a small project. Create a repository, make a few changes, commit them, create a branch, merge it and push your project to GitHub. Once you practice the workflow yourself, Git becomes much easier to understand.

Learn Git. Learn GitHub. Build projects. Collaborate. Keep your code organized. 🚀

🚀 Ready to start using Git and GitHub?

Create a small project, initialize Git and start practicing the workflow step by step. The more you use Git in real projects, the more natural these commands will become.