Git & Version Control - bounswe/bounswe2025group8 GitHub Wiki
- What is Version Control?
- What is Git?
- How Does Git Work?
- Fetching and Merging Updates
- Merge Conflicts and Resolutions
- Git Workflows
- Best Practices
- Common Git Commands
- References
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
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
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
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
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.
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
- Modify files in your working tree.
- Stage specific changes (
git add
). - Commit the changes (
git commit
). - Push them to a remote repository (
git push
).
When collaborating, you need to keep your branch updated with the latest remote changes.
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.
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
A merge conflict happens when two branches modify the same part of a file. Git cannot automatically merge these changes.
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.
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.
- Open the conflicted file and decide which changes to keep.
- Manually edit the file, keeping the correct version.
- Mark it as resolved with:
git add file.txt
- Complete the merge with:
git commit -m "Resolved merge conflict in file.txt"
If you want to cancel the merge and revert to the last known good state:
git merge --abort
Different teams follow different Git workflows based on project needs:
-
Centralized Workflow:
- Everyone commits directly to
main
. - Simple but can cause conflicts.
- Everyone commits directly to
-
Feature Branch Workflow:
- Developers create branches for features.
- Merge via Pull Requests (PRs).
-
Git Flow (Advanced Workflow):
- Uses multiple branches:
main
,develop
,feature
,release
,hotfix
. - Useful for large teams and structured development.
- Uses multiple branches:
-
Forking Workflow:
- Used in open-source projects.
- Developers fork a repo, make changes, and submit Pull Requests.
βοΈ 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.
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 |