Git is a distributed version control system that helps developers track changes in their code, collaborate with others, and manage software projects efficiently. Whether you are a beginner or just brushing up on your skills, this tutorial will walk you through everything you need to know to master Git.
Table of Contents
- What is Git?
- Installing Git
- Initial Configuration
- Creating and Cloning Repositories
- Basic Git Workflow
- Branching and Merging
- Working with Remote Repositories
- Undoing Changes
- Using .gitignore
- Advanced Git Commands
- Git GUI Tools
- Conclusion
What is Git?
Git is a version control system used for tracking changes in source code during software development. It allows multiple developers to work on a project simultaneously, without overwriting each other's work.
Installing Git
Windows / macOS / Linux:
- Download and install Git from git-scm.com.
- Verify installation:
git --version
Initial Configuration
Set your identity:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
View your configuration:
git config --list
Creating and Cloning Repositories
Create a new local repository:
mkdir my-project
cd my-project
git init
Clone an existing repository:
git clone https://github.com/username/repo-name.git
Basic Git Workflow
Check status:
git status
Stage files:
git add filename # Add specific file
git add . # Add all changes
Commit changes:
git commit -m "Meaningful commit message"
View commit history:
git log
git log --oneline
Branching and Merging
Create a new branch:
git branch new-branch
Switch to a branch:
git checkout new-branch
Create and switch in one step:
git checkout -b new-branch
Merge a branch:
git checkout main
git merge new-branch
Delete a branch:
git branch -d new-branch
Working with Remote Repositories
Add remote:
git remote add origin https://github.com/username/repo.git
Push changes:
git push -u origin branch-name
Pull changes:
git pull origin branch-name
Undoing Changes
Unstage a file:
git reset filename
Undo last commit (keep changes):
git reset --soft HEAD~1
Discard all local changes:
git checkout -- .
Using .gitignore
Create a .gitignore
file to exclude files/folders from version control:
node_modules/
.env
dist/
*.log
Advanced Git Commands
Stash changes:
git stash
Apply stashed changes:
git stash apply
Rebase:
git rebase branch-name
Cherry-pick a commit:
git cherry-pick commit-id
Git GUI Tools
- GitHub Desktop
- Sourcetree
- GitKraken
- VS Code Source Control Panel
Conclusion
Git is an essential tool for modern software development. With this step-by-step guide, you can start using Git confidently for your projects. Keep practicing, explore advanced commands, and soon you'll be managing branches, resolving merge conflicts, and collaborating like a pro.
Happy coding! 🚀