Welcome to ChaiCode Cohort! As a developer, you'll often work collaboratively on projects that require seamless version control and efficient code management. This onboarding documentation is designed to help you quickly get started with Git and GitHub, two essential tools for modern software development.
So, first we’ll see what is Version Control System (VCS) and then understand what is Git and GitHub and why they are used.
What is Version Control System?
VCS which is short for Version Control System, is a tool that helps manage and track changes to source code or other files over time. VCS is essential for software development and otherprojects where maintaining a history of changes, collaboration, and versioning is critical.
Need of Version Control System?
Tracking Changes : It records changes to files over time, enabling developers to see who changed what and when.
Collaboration : Multiple people can work on the same project simultaneously without overwriting each other’s work.
Branching and Merging : Developers can create separate branches for different features or experiments and later merge them into the main project.
Version History : It keeps a history of all changes, making it easy to revert to previous versions if needed.
Conflict Resolution : Helps manage and resolve conflicts when multiple developers make changes to the same file.
Backup and Recovery : Acts as a backup for the project.
Now we have a good understanding on VCS so it’s time to get started on Git and GItHub.
What is Git?
Git is a free, open-source, distributed version control system used to track changes in files and coordinate work on projects among multiple people.
Why is Git Used?
Git is used for various purposes such as:
Version Tracking: Git keeps a history of all changes made to a project, allowing you to revisit or restore previous versions at any time.
Collaboration: Git allows multiple developers to work on the same project simultaneously.
Branching and Merging: Git provides a branching system that lets developers create isolated environments for working on new features, bug fixes, or experiments.
Error Recovery: With Git, you can easily undo changes, revert commits, or recover deleted files, making it an invaluable tool for development.
Open Source and Versatile: Git is free, open-source, and works on any operating system.
What is GitHub?
GitHub is a cloud-based platform for hosting Git repositories. It provides collaboration features, a web interface, and tools for managing projects.
Why is GitHub used?
Centralized Code Hosting: GitHub acts as a central repository where developers can store and access their code from anywhere.
Collaboration and Teamwork: GitHub simplifies teamwork by allowing developers to work on the same repository, track progress, and review each other's changes.
Project Management Tools: GitHub includes tools like Issues, Milestones, and Projects to help teams organize tasks, track bugs, and manage workflows efficiently.
Version Control in the Cloud: By integrating Git's version control with a web interface, GitHub makes it easier to manage changes in a collaborative environment.
Open Source Community: GitHub hosts millions of open-source projects, making it a hub for developers to learn, contribute, and collaborate.
Importance of Git and GitHub in team collaboration at ChaiCode Cohort?
At ChaiCode Cohort, effective team collaboration is essential for delivering high-quality software. Git and GitHub play a central role in streamlining this collaboration, ensuring that all team members can work efficiently and cohesively.
Get started with Git:
Installing Git
Windows:
-
Install GIt:
Run the downloaded .exe file.
Adjust the Path environment: select the option to add git to the system PATH
In cmd , type
git --version
to check if you have installed git.
macOS:
Open Terminal and type:
brew install git
Verify the installation:
git --version
Linux:
Use your package manager:
sudo apt-get install git # For Debian/Ubuntu sudo yum install git # For Fedora/Red Hat
Verify the installation:
git --version
Configure Git:
- After the installation, configure Git with your name and email.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
- Verify the Configuration:
git config --global --list
Get Started with GitHub
Creating GitHub Account: To create your account, you need to go to GitHub's website and fill out the registeration form.
Sign up to GitHub using email and create a strong password:
Once you sign up with your email then you are able to see Your github repository and also save your code in the github through pushing your code.
Let's Git
Creating Remote Repo:
Create a new repository on GitHub, Follow this link.
Give your repository a name, and description(optional), choose if you want to make it a public or a private repo(only you and the people you give access can see a private repo), and you can also add a readme and a license if you want. Otherwise, leave all that options as it is and click create repository.
Your repo is created. 🥳
Initializing Git in folder:
Open your Project directory in command prompt or in bash
You can use VSCode or any other Code Editor
Make sure your working directory is correct
Use "git init" command to initailize git into your project directory.
git init
Basic Git Commands.
Command | What it does |
git init | Initialise a new git repo (short form for repository) |
git add <file> | Staging a particular file for commit |
git add . | Staging all of the files in the current directory for commit |
git commit -m “message” | Committing the staged changes with a message |
git log | Checking the history of commits |
git diff | Comparing the before & after states of working file |
git push | Pushing commits in a remote repo |
git pull | Pulling / fetching & merging the changes made in a repo |
git branch | Lists down all the branches |
git branch <name> | Creates a new branch |
git checkout <branch> | Switch to a specific branch |
git merge <branch> | Merge a branch to a current branch |
git reset <file> | Unstage a file (file remains modified) |
git reset —hard <file> | Unstage a file (modifications are removed as well) (use cautiously) |
git stash | Saving changes without committing |
git stash pop | Reapply stashed changes |
git status | Shows the condition of the working directory |
git clone <url> | Clone a repo from a URL |
git fetch | retrieve latest changes from a remote repo (does not merge them) |
git remote add <name> <url> | Add a remote repo |
git rebase <branch> | Reapply commits on top of another branch |
Commonly Used Commands
Check repository status:
git status
Stage changes:
git add <file>
Commit changes:
git commit -m "Your message"
Push changes to GitHub:
git push
Pull updates from GitHub:
git pull
View commit history:
git log
Commit Message Rules:
Use the present tense ("Add feature" not "Added feature").
Capitalize the first letter.
Keep the message short (50 characters or less).
Use prefixes like
fix:
,feat:
,chore:
,docs:
for categorization.Example:
feat: Add tea selection feature
fix: Resolve login issue for tea enthusiasts
docs: Update README with chai varieties
Branching Workflow
Branching Strategy
Main Branch: Stable production-ready code.
Development Branch: Active development.
Feature Branches: Specific features or fixes.
Creating and Switching Branches
Create a new branch:
git branch feature/tea-menu
Switch to the branch:
git checkout feature/tea-menu
Merge changes back to the main branch:
git checkout main
git merge feature/tea-menu
4. Push the branch to GitHub: ```bash git push origin feature/tea-menu
Pull Requests (PR)
Creating a Pull Request
Push your branch to GitHub.
Go to the repository on GitHub and click Pull Requests.
Click New Pull Request.
Add a description and request reviewers.
Best Practices
Commit Regularly: Avoid large, infrequent commits.
Descriptive Messages: Write meaningful commit messages.
Pull Updates: Regularly pull changes to minimize conflicts.
Conclusion
That’s it for today’s introduction to Git and GitHub! These tools might seem a bit overwhelming at first, but with a little practice, they’ll become second nature. Start by installing Git, experimenting with the basic commands, and setting up your first repository on GitHub.
Remember, Git helps you manage your code locally, and GitHub lets you share and collaborate with others. Together, they make development smoother and more organized.
Link to Further Learning Resources
Provide links to resources like: