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 --versionInitial Configuration
Set your identity:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"View your configuration:
git config --listCreating and Cloning Repositories
Create a new local repository:
mkdir my-project
cd my-project
git initClone an existing repository:
git clone https://github.com/username/repo-name.gitBasic Git Workflow
Check status:
git statusStage files:
git add filename # Add specific file
git add . # Add all changesCommit changes:
git commit -m "Meaningful commit message"View commit history:
git log
git log --onelineBranching and Merging
Create a new branch:
git branch new-branchSwitch to a branch:
git checkout new-branchCreate and switch in one step:
git checkout -b new-branchMerge a branch:
git checkout main
git merge new-branchDelete a branch:
git branch -d new-branchWorking with Remote Repositories
Add remote:
git remote add origin https://github.com/username/repo.gitPush changes:
git push -u origin branch-namePull changes:
git pull origin branch-nameUndoing Changes
Unstage a file:
git reset filenameUndo last commit (keep changes):
git reset --soft HEAD~1Discard all local changes:
git checkout -- .Using .gitignore
Create a .gitignore file to exclude files/folders from version control:
node_modules/
.env
dist/
*.logAdvanced Git Commands
Stash changes:
git stashApply stashed changes:
git stash applyRebase:
git rebase branch-nameCherry-pick a commit:
git cherry-pick commit-idGit 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! 🚀