Git conflicts occur when multiple developers make changes to the same parts of a file, and Git cannot automatically determine which changes to keep. This guide will help you understand and resolve common conflict scenarios in your Python/FastAPI backend and Vite frontend projects.
- Two or more developers modify the same line(s) in a file
- One developer deletes a file while another modifies it
- Changes are made to files that have been moved or renamed
- Merging branches that have diverged significantly
When a conflict occurs, Git marks the file with conflict markers:
<<<<<<< HEAD
Your current branch's version
=======
The incoming branch's version
>>>>>>> branch-name
Situation: Two developers add different imports to the same file.
Example:
<<<<<<< HEAD
from fastapi import FastAPI, HTTPException
from datetime import datetime
=======
from fastapi import FastAPI, Depends
from typing import Optional
>>>>>>> feature-branchResolution Strategy:
- Keep both sets of imports if they're all needed
- Remove duplicates
- Order imports according to PEP 8 (standard library, third-party, local)
Fixed Version:
from datetime import datetime
from typing import Optional
from fastapi import FastAPI, HTTPException, DependsSituation: Two developers add different endpoints with the same route.
Example:
<<<<<<< HEAD
@app.get("/users/{user_id}")
def get_user(user_id: int):
return {"user_id": user_id, "name": "John"}
=======
@app.get("/users/{user_id}")
def fetch_user_details(user_id: str):
return {"id": user_id, "username": "johndoe"}
>>>>>>> feature-branchResolution Strategy:
- Discuss with your team which implementation to keep
- Consider if both functionalities are needed (maybe create different endpoints)
- Ensure consistent data types and response formats
Situation: Multiple developers modify the same SQLAlchemy model.
Example:
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
<<<<<<< HEAD
email = Column(String, unique=True, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
=======
username = Column(String, unique=True, nullable=False)
is_active = Column(Boolean, default=True)
>>>>>>> feature-branchResolution Strategy:
- Usually, you'll want to keep all fields if they serve different purposes
- Check for migration conflicts if using Alembic
- Ensure the combined model makes logical sense
Situation: Different developers add different dependencies.
Example:
{
"dependencies": {
"react": "^18.2.0",
<<<<<<< HEAD
"axios": "^1.4.0",
"react-router-dom": "^6.11.0"
=======
"react-query": "^3.39.0",
"styled-components": "^5.3.0"
>>>>>>> feature-branch
}
}Resolution Strategy:
- Keep all dependencies unless they conflict
- Run
npm installoryarn installafter resolving - Check for version compatibility
Situation: Different component imports or structures.
Example:
<<<<<<< HEAD
import Header from './components/Header'
import Footer from './components/Footer'
import Dashboard from './pages/Dashboard'
=======
import Navigation from './components/Navigation'
import Sidebar from './components/Sidebar'
import Home from './pages/Home'
>>>>>>> feature-branchResolution Strategy:
- Keep all imports if components are different
- Check component usage in the file
- Ensure no naming conflicts
Situation: Style modifications to the same element.
Example:
.container {
<<<<<<< HEAD
max-width: 1200px;
padding: 20px;
background-color: #f0f0f0;
=======
max-width: 1400px;
margin: 0 auto;
background-color: #ffffff;
>>>>>>> feature-branch
}Resolution Strategy:
- Discuss design decisions with the team
- Consider if different styles are for different breakpoints
- Use CSS variables for consistent theming
Situation: The most common and intimidating conflict for new developers - when package-lock.json has conflicts after different developers install packages.
Example:
{
"name": "frontend",
"version": "0.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
<<<<<<< HEAD
"node_modules/axios": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz",
=======
"node_modules/react-query": {
"version": "3.39.0",
"resolved": "https://registry.npmjs.org/react-query/-/react-query-3.39.0.tgz",
>>>>>>> feature-branchResolution Strategy:
- NEVER manually edit package-lock.json to resolve conflicts
- Delete the package-lock.json file
- Re-run the install command:
rm package-lock.json npm install
- This regenerates a fresh lock file with all dependencies
- Commit the new package-lock.json
Situation: Different environment variables added.
Example:
<<<<<<< HEAD
DATABASE_URL=postgresql://user:pass@localhost/mydb
REDIS_URL=redis://localhost:6379
=======
DATABASE_URL=postgresql://user:pass@localhost/testdb
JWT_SECRET=my-secret-key
>>>>>>> feature-branch
Resolution Strategy:
- Keep all variables if they serve different purposes
- Ensure DATABASE_URL points to the correct database
- Never commit sensitive data; use .env.example as a template
Situation: Python cache files were accidentally committed and now cause conflicts.
Signs of this problem:
Untracked files:
backend/app/__pycache__/main.cpython-39.pyc
backend/app/api/__pycache__/users.cpython-39.pyc
Resolution:
-
Remove all pycache directories:
# From your project root find . -type d -name __pycache__ -exec rm -rf {} + # Or on Windows for /d /r . %d in (__pycache__) do @if exist "%d" rd /s /q "%d"
-
Add to .gitignore if not already there:
# Python __pycache__/ *.py[cod] *$py.class *.so .Python
-
Remove from Git tracking:
git rm -r --cached __pycache__ git rm -r --cached "*.pyc" git commit -m "Remove Python cache files"
Situation: Someone accidentally committed the entire virtual environment folder.
Why this is bad:
- Virtual environments can be hundreds of MB
- They're platform-specific
- They should be recreated locally
Resolution:
-
Remove the virtual environment:
# If it's called .venv git rm -r --cached .venv # If it's called venv git rm -r --cached venv
-
Update .gitignore:
# Virtual environments venv/ .venv/ env/ ENV/ virtualenv/
-
Commit the changes:
git commit -m "Remove virtual environment from tracking" -
Tell team members to recreate their environments:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate pip install -r requirements.txt
Before committing, always run:
git status
# Look for files that shouldn't be there
git diff --staged
# Review what you're actually committinggit status
# Shows files with conflicts in redLook for conflict markers (<<<<<<<, =======, >>>>>>>)
- Read the code in both sections
- Understand the intent of each change
- Consult with team members if needed
Choose one of these approaches:
- Keep HEAD (your changes): Delete the incoming changes and markers
- Keep incoming changes: Delete your changes and markers
- Keep both: Combine both changes logically
- Rewrite: Create a new solution that incorporates both intents
Ensure all <<<<<<<, =======, and >>>>>>> markers are removed
- Run your FastAPI backend:
uvicorn main:app --reload - Run your Vite frontend:
npm run dev - Ensure both applications work correctly
git add <resolved-files>
git commit -m "Resolve merge conflicts in [describe what was resolved]"- Inform team members when working on shared files
- Use feature branches for isolated development
- Have clear code ownership areas
# Update your branch regularly
git fetch origin
git merge origin/main # or git rebase origin/main- Make commits that change one thing at a time
- Write clear commit messages
- Avoid large, sweeping changes
- Use formatters (Black for Python, Prettier for JS)
- Follow agreed-upon conventions
- Use linters to catch issues early
# Abort a merge if things go wrong
git merge --abort
# View the conflict in a three-way diff
git diff --ours # Your version
git diff --theirs # Their version
# After resolving, continue with rebase
git rebase --continue
# Check what files have conflicts
git diff --name-only --diff-filter=U
# Use a merge tool
git mergetool# Fix the files, then
git add <fixed-files>
git commit --amendNote, only do this if you have not yet pushed your changes.
# Start over
git merge --abort# See the commit history
git log --oneline --graph --all
# See what a specific commit changed
git show <commit-hash>Git conflicts are a normal part of collaborative development. With practice, resolving them becomes routine. Remember:
- Don't panic when you see conflicts
- Take time to understand both changes
- Communicate with your team
- Test thoroughly after resolution
- Learn from conflicts to prevent future ones
The key is to stay calm, be methodical, and remember that Git always keeps your code safe - you can always recover from mistakes.