What is Git?
Git is a Version Control System (VCS) that helps developers track changes in their code, collaborate efficiently, and maintain a history of modifications. It ensures that teams can work on different parts of a project without overriding each other’s work.
Example:
Imagine a team of developers working on a web application. One developer is building the login feature, while another is designing the user profile. With Git, they can work in parallel on their respective features without interfering with each other's work.
What is GitHub?
GitHub is a hosting service for Git repositories. While Git provides the functionality for version control, GitHub offers a platform to host, share, and collaborate on Git repositories.
Key Differences:
Feature | Git | GitHub |
Functionality | Version control system | Hosting and collaboration platform |
Hosting | Works locally | Cloud-based |
Usage | Command-line interface (CLI) | Web and CLI |
CVCS vs. DVCS
Feature | Centralized VCS (CVCS) | Distributed VCS (DVCS) |
Storage | Central server | Local copies for all users |
Dependency | Needs constant server connection | Works offline |
Example | Subversion (SVN) | Git |
Repositories: Local vs. Remote
Local Repository: A Git repository stored on your local machine.
Example: Code stored in the
.git
folder of your project.Command:
git init
Remote Repository: A shared repository hosted on platforms like GitHub.
Example: GitHub repository URL
https://github.com/user/repo.git
Command:
git remote add origin <repo_url>
Git Branches
Branches allow parallel development paths in a project.
Branch | Purpose |
main | Production-ready code |
dev | Ongoing development work |
feature | Specific features |
QA | Quality Assurance testing |
PPD | Pre-production |
DR | Disaster Recovery |
Common Git Commands
Stashing Changes:
Temporarily saves changes:
git stash
Retrieve stashed changes:
git stash apply
Merging Branches:
- Combine branches:
git merge <branch_name>
- Combine branches:
Viewing Logs:
- Compact view:
git log --oneline
- Compact view:
Switching Branches:
- Change branch:
git checkout <branch_name>
- Change branch:
Branch Management:
- Create branch:
git branch <branch_name>
- Create branch:
Resetting Changes:
Soft reset:
git reset --soft HEAD~1
Mixed reset:
git reset --mixed HEAD~1
Hard reset:
git reset --hard HEAD~1
Reverting Changes:
- Undo specific commits:
git revert <commit_hash>
- Undo specific commits:
Resolving Conflicts:
Manually edit conflicting files.
Add resolved files:
git add <file_name>
Commit changes:
git commit
Handling HTTP Post Buffer Error
If pushing large files triggers an error, increase the buffer size:
git -c http.postBuffer=524288000 push origin main
git config --global http.postBuffer 524288000
Conflict Resolution: An Example
Two developers edit the same line in a file.
Upon merging, Git raises a conflict.
Open the conflicting file, resolve the differences, and save.
Stage and commit the resolved file:
git add <file> git commit -m "Resolved merge conflict"
Why Git and GitHub?
Collaboration: Multiple developers can contribute simultaneously.
Version Control: Keep a history of changes.
Branching: Work on multiple features independently.
Global Access: Share projects via GitHub.
Final Thoughts
Git and GitHub form the backbone of modern development practices. By mastering Git commands, understanding branching strategies, and using GitHub for collaboration, developers can streamline workflows and deliver robust software.
Ready to get started? Push your first repository to GitHub today!