Git & Version Control - bounswe/bounswe2025group8 GitHub Wiki

πŸ“Œ Table of Contents

  1. What is Version Control?
  2. What is Git?
  3. How Does Git Work?
  4. Fetching and Merging Updates
  5. Merge Conflicts and Resolutions
  6. Git Workflows
  7. Best Practices
  8. Common Git Commands
  9. References

What is Version Control?

Version control, also known as source control, is the practice of tracking and managing changes to software code. It allows developers to:
βœ”οΈ Keep a history of changes
βœ”οΈ Collaborate effectively
βœ”οΈ Revert to previous versions if needed

Centralized vs. Distributed Version Control

πŸ“ Centralized Version Control Systems (CVCS)

In CVCS (e.g., CVS, Subversion, Perforce):

  • A single server stores all the versioned files.
  • Clients check out files from the central server.
  • Easy to administer but has a single point of failure.

βœ… Pros: Simpler to manage
❌ Cons: If the server crashes, you lose everything without proper backups

πŸ“ Distributed Version Control Systems (DVCS)

In DVCS (e.g., Git, Mercurial, Darcs):

  • Every developer has a complete copy of the repository, including history.
  • No reliance on a central server for history tracking.
  • Any client can restore a lost server.

βœ… Pros: No single point of failure, faster local operations
❌ Cons: Slightly higher learning curve


What is Git?

Git is the most widely used modern version control system, originally developed by Linus Torvalds in 2005.
βœ”οΈ Fast and distributed version control
βœ”οΈ Used for managing collaborative development
βœ”οΈ Tracks code history efficiently


How Does Git Work?

πŸ“Œ Snapshots, Not Differences

Unlike older VCS (like SVN), Git:

  • Stores snapshots of the entire project at each commit instead of just differences.
  • Saves space by referencing unchanged files rather than duplicating them.
  • Makes history tracking and branching more efficient.

πŸ“Œ The Three States of Git

Git tracks files in three states:

State Description
Modified Changes made but not saved yet
Staged Marked for the next commit
Committed Permanently stored in Git

πŸ”Ή Three Key Areas in a Git Project:

  • Working Tree: Your local files (modified state)
  • Staging Area: Prepares selected changes for commit
  • Git Directory: Stores metadata and history

πŸ“Œ Basic Git Workflow

  1. Modify files in your working tree.
  2. Stage specific changes (git add).
  3. Commit the changes (git commit).
  4. Push them to a remote repository (git push).

Fetching and Merging Updates

When collaborating, you need to keep your branch updated with the latest remote changes.

πŸ“Œ Fetching Updates

git fetch downloads changes from the remote repository without merging them:

git fetch origin

This updates your remote-tracking branches but does not affect your working branch.

πŸ“Œ Merging Updates

To merge fetched updates into your local branch:

git merge origin/main

Alternatively, use git pull to fetch and merge in one step:

git pull origin main

Merge Conflicts and Resolutions

A merge conflict happens when two branches modify the same part of a file. Git cannot automatically merge these changes.

πŸ“Œ Identifying Merge Conflicts

When a conflict occurs, Git stops the merge and shows a message like this:

Auto-merging file.txt  
CONFLICT (content): Merge conflict in file.txt  
Automatic merge failed; fix conflicts and then commit the result.  

πŸ“Œ Understanding Conflict Markers

When you open the conflicted file, Git marks the conflicting sections like this:

<<<<<<< HEAD
Your version of the file.
=======
Other branch's version of the file.
>>>>>>> feature-branch
  • <<<<<<< HEAD β†’ This section contains the changes from your current branch (the one you’re on).
  • ======= β†’ This separates your changes from the incoming changes.
  • >>>>>>> feature-branch β†’ This section contains the changes from the branch you are merging in.

πŸ“Œ Resolving Merge Conflicts

  1. Open the conflicted file and decide which changes to keep.
  2. Manually edit the file, keeping the correct version.
  3. Mark it as resolved with:
git add file.txt
  1. Complete the merge with:
git commit -m "Resolved merge conflict in file.txt"

πŸ“Œ Aborting a Merge

If you want to cancel the merge and revert to the last known good state:

git merge --abort

Git Workflows

Different teams follow different Git workflows based on project needs:

  1. Centralized Workflow:

    • Everyone commits directly to main.
    • Simple but can cause conflicts.
  2. Feature Branch Workflow:

    • Developers create branches for features.
    • Merge via Pull Requests (PRs).
  3. Git Flow (Advanced Workflow):

    • Uses multiple branches: main, develop, feature, release, hotfix.
    • Useful for large teams and structured development.
  4. Forking Workflow:

    • Used in open-source projects.
    • Developers fork a repo, make changes, and submit Pull Requests.

Best Practices

βœ”οΈ Commit often – Small, frequent commits help track changes.
βœ”οΈ Pull latest changes before working – Prevents conflicts.
βœ”οΈ Write clear commit messages – Explain why the change was made.
βœ”οΈ Use branches – Keep main clean and use feature branches.
βœ”οΈ Agree on a workflow – Follow team conventions.


Common Git Commands

For details and examples of commands, visit our Git Commands Wiki Page.

Command Description
git init Initialize a new repository
git clone <repo> Clone a repository
git add <file> Stage changes
git commit -m "message" Commit staged changes
git push Push commits to a remote repository
git pull Fetch and merge updates
git status Show file status
git branch List or create branches
git checkout <branch> Switch branches
git merge <branch> Merge branches

References

  1. Git Documentation
  2. Pro Git Book
  3. Atlassian Git Tutorials

⚠️ **GitHub.com Fallback** ⚠️