Overview
Version-control
Version Control with Git: Basics and Best Practices
In this module, you’ll learn the fundamentals of version control using Git. We’ll cover what version control is, why Git is the most popular system for it, and how to use Git to manage your projects effectively. By the end of this module, you’ll be comfortable with initializing repositories, making commits, branching, and collaborating with others using Git and GitHub.
Estimated reading time: ~25 minutes
Table of Contents
- Introduction to Version Control
- What is Version Control?
- Why Use Git?
- Setting Up Git
- Installing Git
- Configuring Git
- Basic Git Commands
- Initializing a Repository
- Staging and Committing Changes
- Checking the Status
- Viewing Commit History
- Working with Branches
- Creating and Switching Branches
- Merging Branches
- Resolving Merge Conflicts
- Collaborating with Git and GitHub
- Setting Up a GitHub Repository
- Pushing and Pulling Changes
- Working with Pull Requests
- Best Practices
- Conclusion
- Additional Resources
Introduction to Version Control
What is Version Control?
Version control is a system that records changes to files over time so that you can recall specific versions later. It allows multiple people to work on a project simultaneously without overwriting each other’s changes.
Benefits of Version Control:
- History Tracking: Keep a history of changes, making it easy to revert to previous versions.
- Collaboration: Multiple developers can work on the same project simultaneously.
- Branching and Merging: Isolate changes in branches and merge them back when ready.
- Backup: Acts as a backup of your codebase.
Why Use Git?
Git is the most widely used modern version control system in the world today. Here are some reasons why Git is popular:
- Distributed System: Every developer has a full copy of the repository.
- Performance: Git is fast and efficient.
- Branching Model: Lightweight branching and merging.
- Open Source: Free to use and has a large community.
- Integration with Platforms: Works seamlessly with platforms like GitHub, GitLab, and Bitbucket.
Setting Up Git
Installing Git
For Windows:
- Download the Git installer from the official website.
- Run the installer and follow the on-screen instructions.
For macOS:
-
Install via Homebrew:
brew install git
-
Or download from the official website.
For Linux:
-
Use your distribution’s package manager. For example, on Ubuntu:
sudo apt-get update sudo apt-get install git
Configuring Git
After installing Git, you need to set up your username and email. These will be associated with your commits.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
You can verify your configuration:
git config --list
Basic Git Commands
Initializing a Repository
To start tracking a project with Git, you need to initialize a repository.
-
Navigate to your project directory:
cd path/to/your/project
-
Initialize Git:
git init
This command creates a hidden
.git
directory that tracks changes.
Staging and Committing Changes
Staging Changes:
-
To stage all changes:
git add .
-
To stage specific files:
git add filename.txt
Committing Changes:
-
Commit staged changes with a message:
git commit -m "Your commit message"
Commit Message Guidelines:
- Use the imperative mood (e.g., “Add feature” instead of “Added feature”).
- Keep it concise but descriptive.
- Example:
"Implement user authentication"
Checking the Status
To see the status of your repository:
git status
This command shows:
- Files that have been modified.
- Files staged for commit.
- Untracked files.
Viewing Commit History
To view the commit history:
git log
For a condensed view:
git log --oneline
Working with Branches
Creating and Switching Branches
Branches allow you to work on different versions of a project simultaneously.
Create a new branch:
git branch feature-branch
Switch to a branch:
git checkout feature-branch
Create and switch in one command:
git checkout -b feature-branch
Merging Branches
Once you’ve made changes in your feature branch and want to integrate them into the main branch:
-
Switch to the main branch:
git checkout main
-
Merge the feature branch:
git merge feature-branch
Resolving Merge Conflicts
A merge conflict occurs when changes in two branches conflict.
Steps to resolve:
-
Git will indicate which files have conflicts.
-
Open the conflicted files; you’ll see markers like:
<<<<<<< HEAD Current changes ======= Incoming changes >>>>>>> feature-branch
-
Manually edit the file to resolve conflicts.
-
After resolving, add the files:
git add conflicted-file.txt
-
Commit the merge:
git commit
Collaborating with Git and GitHub
Setting Up a GitHub Repository
-
Create a GitHub Account:
- Sign up at github.com.
-
Create a New Repository:
- Click on the “New” button.
- Enter a repository name and description.
- Choose to make it public or private.
-
Link Your Local Repository to GitHub:
In your local project directory:
git remote add origin <https://github.com/yourusername/your-repo.git>
Pushing and Pulling Changes
Push Local Changes to GitHub:
-
Push your commits to the remote repository:
git push -u origin main
The
-u
flag sets the remoteorigin
as the upstream for themain
branch.
Pull Changes from GitHub:
-
To update your local repository with changes from GitHub:
git pull origin main
Working with Pull Requests
A Pull Request (PR) is a way to propose changes to a repository.
Steps to create a PR:
-
Push Your Feature Branch to GitHub:
git push origin feature-branch
-
Create a Pull Request on GitHub:
- Navigate to your repository on GitHub.
- Click on “Compare & pull request”.
- Add a title and description.
- Assign reviewers if needed.
-
Review and Merge:
- Reviewers can comment on your code.
- After approval, merge the PR into the main branch.
Best Practices
- Commit Often:
- Make frequent, small commits.
- Helps in tracking changes and isolating issues.
- Write Meaningful Commit Messages:
- Clearly describe what changes the commit introduces.
- Use Branches:
- Keep the
main
branch stable. - Use feature branches for development.
- Keep the
- Pull Before Pushing:
- Always pull changes from the remote repository before pushing.
- Reduces the risk of conflicts.
- Resolve Conflicts Promptly:
- Address merge conflicts as soon as they arise.
- Collaborate Through Pull Requests:
- Use PRs for code reviews and discussions.
- Ignore Unnecessary Files:
-
Use a
.gitignore
file to exclude files likenode_modules
,.env
, and others.# .gitignore node_modules/ .env dist/
-
- Secure Your Repositories:
- Do not commit sensitive information.
- Use environment variables or configuration files that are not tracked.
Conclusion
Version control is an essential tool for any developer. Git, combined with platforms like GitHub, provides a powerful workflow for tracking changes, collaborating with others, and managing codebases effectively. By mastering the basics covered in this module, you’re well on your way to becoming proficient in using Git for your projects.
Additional Resources
- Git Documentation: git-scm.com/docs
- Pro Git Book: git-scm.com/book/en/v2
- GitHub Guides:
- Interactive Git Tutorials:
- Git Cheat Sheet: GitHub Git Cheat Sheet
🎉 Congratulations! You’ve completed the Version Control with Git module. Continue practicing by applying these concepts to your projects and collaborating with others.