Git and Version Control

Version control is an essential tool in software development that allows developers to track changes, collaborate, and manage code efficiently. Git is the most widely used version control system, providing powerful features for tracking and managing code changes.


What is Version Control?

Version control systems (VCS) help developers:


What is Git?

Git is a distributed version control system that tracks changes in code, enabling teams to collaborate efficiently. It provides:


Installing Git

On Windows:

  1. Download and install Git from git-scm.com.
  2. Use Git Bash or the Command Prompt to run Git commands.

On macOS:

brew install git

On Linux:

sudo apt install git  # Debian/Ubuntu
sudo yum install git  # CentOS/RedHat

Check if Git is installed:

git --version

Basic Git Commands

Command Description
git init Initialize a new Git repository
git clone <repo_url> Clone an existing repository
git status Check the status of your working directory
git add <file> Stage changes for commit
git commit -m "message" Commit changes with a message
git log View commit history
git branch List, create, or delete branches
git checkout <branch> Switch to a different branch
git merge <branch> Merge a branch into the current branch
git push origin <branch> Push changes to a remote repository
git pull origin <branch> Pull updates from a remote repository

Setting Up Git

Configure Git with your details:

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

To verify configuration:

git config --list

Initializing a Git Repository

To start tracking a project with Git:

git init

This command creates a .git directory inside your project, where Git stores all version history.


Tracking Changes

  1. Add files to staging:
    git add filename.py
    
  2. Commit changes:
    git commit -m "Initial commit"
    
  3. Check status:
    git status
    

Cloning a Repository

To copy an existing repository from a remote location (like GitHub):

git clone https://github.com/user/repository.git

Branching and Merging

Branches allow you to work on different features without affecting the main codebase.

Create a new branch:

git branch feature-branch

Switch to the new branch:

git checkout feature-branch

Merge a branch into main:

git checkout main
git merge feature-branch

Delete a branch:

git branch -d feature-branch

Working with Remote Repositories

To connect to a remote repository:

git remote add origin https://github.com/user/repository.git

Push changes to remote:

git push origin main

Pull latest changes from remote:

git pull origin main

Resolving Merge Conflicts

When merging branches, conflicts may arise if the same part of a file was modified. Git will highlight conflicts in the affected files.

Steps to resolve:

  1. Open the conflicted file and resolve differences.
  2. Stage the resolved file:
    git add conflicted_file.py
    
  3. Commit the resolution:
    git commit -m "Resolved merge conflict"
    

Undoing Changes

Undo changes in a file:

git checkout -- filename.py

Unstage a file:

git reset filename.py

Undo the last commit:

git reset --soft HEAD~1

Git Ignore

A .gitignore file tells Git which files to ignore and not track.

Example .gitignore file:

__pycache__/
*.log
.env
node_modules/

Add the ignore file to Git:

git add .gitignore
git commit -m "Add gitignore"

GitHub Workflow

  1. Create a repository on GitHub.
  2. Clone the repository to your local machine.
  3. Make changes and commit them.
  4. Push changes to GitHub.
  5. Create pull requests for code review.

Best Practices for Using Git

  1. Commit often with meaningful messages.
  2. Use branches to isolate features and bug fixes.
  3. Pull changes before pushing to avoid conflicts.
  4. Avoid committing sensitive information (use .gitignore).
  5. Regularly backup your repository.

Practice Exercises

  1. Initialize a new Git repository and track changes to a Python file.
  2. Create a branch, make changes, and merge it into the main branch.
  3. Set up a remote repository and push code to GitHub.
  4. Resolve a merge conflict in a test project.

Summary

By mastering Git, you can collaborate effectively and maintain a clean, organized codebase.