Git and Version Control
Version control is an essential tool in software development that allows developers to track changes, collaborate, and manage code efficiently. Git is the most widely used version control system, providing powerful features for tracking and managing code changes.
What is Version Control?
Version control systems (VCS) help developers:
- Track changes to code over time.
- Collaborate with team members efficiently.
- Revert to previous versions if needed.
- Work on different features simultaneously using branches.
What is Git?
Git is a distributed version control system that tracks changes in code, enabling teams to collaborate efficiently. It provides:
- A complete history of changes.
- The ability to work offline.
- Branching and merging features.
- Collaboration via platforms like GitHub, GitLab, and Bitbucket.
Installing Git
On Windows:
- Download and install Git from git-scm.com.
- Use Git Bash or the Command Prompt to run Git commands.
On macOS:
brew install git
On Linux:
sudo apt install git # Debian/Ubuntu
sudo yum install git # CentOS/RedHat
Check if Git is installed:
git --version
Basic Git Commands
Command | Description |
---|---|
git init |
Initialize a new Git repository |
git clone <repo_url> |
Clone an existing repository |
git status |
Check the status of your working directory |
git add <file> |
Stage changes for commit |
git commit -m "message" |
Commit changes with a message |
git log |
View commit history |
git branch |
List, create, or delete branches |
git checkout <branch> |
Switch to a different branch |
git merge <branch> |
Merge a branch into the current branch |
git push origin <branch> |
Push changes to a remote repository |
git pull origin <branch> |
Pull updates from a remote repository |
Setting Up Git
Configure Git with your details:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
To verify configuration:
git config --list
Initializing a Git Repository
To start tracking a project with Git:
git init
This command creates a .git
directory inside your project, where Git stores all version history.
Tracking Changes
- Add files to staging:
git add filename.py
- Commit changes:
git commit -m "Initial commit"
- Check status:
git status
Cloning a Repository
To copy an existing repository from a remote location (like GitHub):
git clone https://github.com/user/repository.git
Branching and Merging
Branches allow you to work on different features without affecting the main codebase.
Create a new branch:
git branch feature-branch
Switch to the new branch:
git checkout feature-branch
Merge a branch into main:
git checkout main
git merge feature-branch
Delete a branch:
git branch -d feature-branch
Working with Remote Repositories
To connect to a remote repository:
git remote add origin https://github.com/user/repository.git
Push changes to remote:
git push origin main
Pull latest changes from remote:
git pull origin main
Resolving Merge Conflicts
When merging branches, conflicts may arise if the same part of a file was modified. Git will highlight conflicts in the affected files.
Steps to resolve:
- Open the conflicted file and resolve differences.
- Stage the resolved file:
git add conflicted_file.py
- Commit the resolution:
git commit -m "Resolved merge conflict"
Undoing Changes
Undo changes in a file:
git checkout -- filename.py
Unstage a file:
git reset filename.py
Undo the last commit:
git reset --soft HEAD~1
Git Ignore
A .gitignore
file tells Git which files to ignore and not track.
Example .gitignore
file:
__pycache__/
*.log
.env
node_modules/
Add the ignore file to Git:
git add .gitignore
git commit -m "Add gitignore"
GitHub Workflow
- Create a repository on GitHub.
- Clone the repository to your local machine.
- Make changes and commit them.
- Push changes to GitHub.
- Create pull requests for code review.
Best Practices for Using Git
- Commit often with meaningful messages.
- Use branches to isolate features and bug fixes.
- Pull changes before pushing to avoid conflicts.
- Avoid committing sensitive information (use
.gitignore
). - Regularly backup your repository.
Practice Exercises
- Initialize a new Git repository and track changes to a Python file.
- Create a branch, make changes, and merge it into the main branch.
- Set up a remote repository and push code to GitHub.
- Resolve a merge conflict in a test project.
Summary
- Git is a powerful version control tool used to track changes and collaborate on code.
- Basic Git commands include
init
,clone
,add
,commit
,push
, andpull
. - Branching and merging allow for parallel development.
- Best practices include committing frequently, using branches, and avoiding sensitive data in repositories.
By mastering Git, you can collaborate effectively and maintain a clean, organized codebase.