1. Introduction to Git
Definition: Git is a distributed version control system that tracks changes in files and allows multiple people to collaborate on a project.
-
Distributed: Each developer has a full copy of the repository, including its history.
-
Benefits: Keeps track of code history, enables collaboration, allows branching and merging, can revert changes safely.
-
Alternatives: SVN, Mercurial, CVS
2. Git Installation and Setup
Windows: Git for Windows
Mac: brew install git
Linux: sudo apt-get install git OR sudo yum install git
Configure Git:git config --global user.name "Your Name"git config --global user.email "you@example.com"
Check Configuration:git config --list
3. Basic Git Concepts
-
Repository (Repo): A project folder tracked by Git.
-
Working Directory: Your current project files.
-
Staging Area (Index): Temporary area to prepare files for commit.
-
Commit: Snapshot of changes.
-
Branch: Independent line of development.
4. Git Basic Commands (Beginner)
git init — Initialize repositorygit clone <repo_url> — Clone repositorygit status — Check statusgit add <file_name> — Add filegit add . — Add all filesgit commit -m "Commit message" — Commit changesgit log — View historygit log --oneline --graph --all — Compact history graphgit diff — Show unstaged changesgit diff --staged — Show staged changes
5. Branching and Merging (Intermediate)
git branch <branch_name> — Create branchgit checkout <branch_name> OR git switch <branch_name> — Switch branchgit checkout -b <branch_name> OR git switch -c <branch_name> — Create + Switch branchgit merge <branch_name> — Merge branchgit branch -d <branch_name> — Delete branchgit branch -D <branch_name> — Force delete branch
6. Remote Repositories
git remote add origin <repo_url> — Add remotegit push -u origin <branch_name> — Push branchgit pull — Pull changesgit fetch — Fetch changesgit remote -v — View remotes
7. Undoing Changes
git reset HEAD <file> — Unstage filegit checkout -- <file> — Discard changes in working directorygit revert <commit_id> — Revert commitgit reset --hard <commit_id> — Reset to previous commit
8. Git Tags
git tag <tag_name> — Create taggit tag — List tagsgit push origin <tag_name> — Push taggit push origin --tags — Push all tags
9. Advanced Git Concepts
git stash — Save changesgit stash list — List stashesgit stash apply — Apply stashgit stash pop — Apply + remove stashgit cherry-pick <commit_id> — Apply specific commitgit rebase <branch_name> — Rebase commitsgit rebase -i HEAD~<n> — Interactive rebase
Resolving Conflicts: Edit conflicting files → git add <file> → commit or continue rebase
10. Git Workflows
Centralized Workflow : Single shared branch (like main).
Feature Branch Workflow : Develop features in separate branches.
Gitflow Workflow: main → production-ready, develop → development (feature, release, hotfix branches).
Forking Workflow: Fork repo, clone, make PRs for collaboration.