Β 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 repository
πΆ git clone <repo_url> β Clone repository
πΆ git status β Check status
πΆ git add <file_name> β Add file
πΆ git add . β Add all files
πΆ git commit -m "Commit message" β Commit changes
πΆ git log β View history
πΆ git log --oneline --graph --all β Compact history graph
πΆ git diff β Show unstaged changes
πΆ git diff --staged β Show staged changes
5. Branching and Merging (Intermediate)
πΆ git branch <branch_name> β Create branch
πΆ git checkout <branch_name> OR git switch <branch_name> β Switch branch
πΆ git checkout -b <branch_name> OR git switch -c <branch_name> β Create + Switch branch
πΆ git merge <branch_name> β Merge branch
πΆ git branch -d <branch_name> β Delete branch
πΆ git branch -D <branch_name> β Force delete branch
6. Remote Repositories
πΆ git remote add origin <repo_url> β Add remote
πΆ git push -u origin <branch_name> β Push branch
πΆ git pull β Pull changes
πΆ git fetch β Fetch changes
πΆ git remote -v β View remotes
7. Undoing Changes
πΆ git reset HEAD <file> β Unstage file
πΆ git checkout -- <file> β Discard changes in working directory
πΆ git revert <commit_id> β Revert commit
πΆ git reset --hard <commit_id> β Reset to previous commit
8. Git Tags
πΆ git tag <tag_name> β Create tag
πΆ git tag β List tags
πΆ git push origin <tag_name> β Push tag
πΆ git push origin --tags β Push all tags
9. Advanced Git Concepts
πΆ git stash β Save changes
πΆ git stash list β List stashes
πΆ git stash apply β Apply stash
πΆ git stash pop β Apply + remove stash
πΆ git cherry-pick <commit_id> β Apply specific commit
πΆ git rebase <branch_name> β Rebase commits
πΆ git 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.