The Importance of Version Control
Have you ever worked on a project and ended up with files like final_code.py, final_code_v2.py, or final_code_really_final.py? Version control solves this chaos. It tracks every single line change, allows you to roll back to previous versions, and enables hundreds of developers to work on the same codebase simultaneously without overwriting each other's work.
Git vs. GitHub: What's the Difference?
- Git: A local command-line tool that records history and tracks code modifications on your local computer.
- GitHub: A web-based platform that hosts Git repositories in the cloud, offering collaboration tools, bug tracking, and code review workflows.
The Standard Git Workflow
Working with Git involves three main areas: the working directory (where you make edits), the staging area (where you choose which changes to include in the next commit), and the repository (the permanent record of changes):
# Initialize a new local Git repository
git init
# Add files to the staging area
git add main.py
# Commit the changes with a descriptive message
git commit -m "feat: implement user login endpoint"
# Link to a remote GitHub repository and push your code
git remote add origin https://github.com/user/repo.git
git branch -M main
git push -u origin main
Branching Strategy: How Teams Collaborate
In a team setting, you should never write code directly to the main production branch. Instead, teams use a branching strategy:
- Create a feature branch:
git checkout -b feature/login. - Write and commit your code locally.
- Push the branch to GitHub:
git push origin feature/login. - Open a **Pull Request (PR)** on GitHub to invite teammates to review and approve your changes.
- Merge the PR into the main branch after tests pass.
Resolving Merge Conflicts
A merge conflict occurs when two developers modify the exact same line of the same file in different ways. Git doesn't know which version is correct, so it highlights the conflict in the code using separators:
<<<<<<< HEAD
print("Welcome back, member!")
=======
print("Welcome to MLSC Portal")
>>>>>>> main
To resolve it, simply edit the file to keep the correct version, delete the separators, stage the file, and commit.
Conclusion
Git and GitHub are standard industry tools required for any developer job. By incorporating these commands into your daily workflow, you will build cleaner code histories and collaborate seamlessly with technical teams.