Git is a distributed version control system that helps developers track changes, collaborate, and manage code efficiently. Below is a detailed explanation of all important Git commands, their meaning, and real-world problem solutions π
π° 1. Git Configuration (Setup)
πΉ git config
Used to configure user information for commits.
- Stores your identity (name + email)
- Can be set globally or per project
π Example:git config --global user.name "Your Name"git config --global user.email "you@email.com"
π Problem: Wrong name/email showing in commits
β
Solution:git config --global --edit
π 2. Repository Commands
πΉ git init
Creates a new Git repository in your project folder.
- Initializes
.gitdirectory - Starts version tracking
πΉ git clone <url>
Downloads a remote repository to your system.
- Includes full history
- Automatically connects to remote
π Problem: Clone not working
β
Check repository URL, internet, and permissions
π 3. File Tracking & Staging
πΉ git status
Shows current project state:
- Modified files
- Untracked files
- Staged files
πΉ git add <file>
Moves files to staging area (ready to commit).
git add .β all filesgit add file.txtβ specific file
π Problem: File not getting added
β
Check if itβs ignored in .gitignore
πΎ 4. Commit (Saving Changes)
πΉ git commit -m "message"
Creates a snapshot of staged changes.
- Message describes what changed
- Each commit has a unique ID
π Problem: Forgot to add file
β
Use:git add filegit commit --amend
π Problem: Bad commit message
β
git commit --amend -m "new message"
πΏ 5. Branching (Parallel Work)
πΉ git branch
Lists all branches
πΉ git branch <name>
Creates a new branch
πΉ git checkout <branch>
Switches branch
πΉ git checkout -b <branch>
Create + switch
π Problem: Lost changes while switching
β
Always commit or use stash
π 6. Merging Branches
πΉ git merge <branch>
Combines another branch into current branch
- Used after feature completion
π Problem: Merge conflict
β
Steps:
- Open conflicted file
- Fix manually
git add .git commit
π 7. Remote Repository (GitHub/GitLab)
πΉ git remote add origin <url>
Connect local repo to remote
πΉ git push origin main
Uploads code to remote
πΉ git pull
Fetch + merge changes
πΉ git fetch
Only downloads changes (no merge)
π Problem: Push rejected
β
git pull origin main --rebase
Then push again
π 8. Undo Changes (Fix Mistakes)
πΉ git reset
Undo staging or commits
πΉ git reset --hard β οΈ
Deletes all changes permanently
πΉ git revert
Safely undo commit by creating new commit
π Problem: Accidentally deleted work
β
Use:git reflog β recover commit
π 9. Logs & Differences
πΉ git log
Shows commit history
- Author
- Date
- Message
πΉ git log --oneline
Short version of history
πΉ git diff
Shows differences between files/commits
π Problem: Hard to track changes
β
Use:git diff before commit
π¦ 10. Stash (Temporary Save Work)
πΉ git stash
Saves changes temporarily without commit
πΉ git stash pop
Restores saved work
πΉ git stash list
Shows saved stashes
π Problem: Need to switch branch quickly
β
Use stash before switching
β‘ 11. Advanced Git Commands
πΉ git rebase
Reapplies commits for cleaner history
- Avoids unnecessary merge commits
π Problem: Messy history
β
Use rebase instead of merge
πΉ git cherry-pick <commit>
Apply specific commit to another branch
π Problem: Need one feature only
β
Cherry-pick that commit
πΉ git tag
Marks versions/releases
Example:git tag v1.0
π₯ 12. Debugging & Recovery Tools
πΉ git reflog
Shows all actions (even deleted commits)
π Used to recover lost work
πΉ git blame <file>
Shows who modified each line
β οΈ Common Problems + Quick Fixes
β Merge Conflict
β Fix manually β add β commit
β Detached HEAD
β git checkout main
β Deleted branch
β Recover using git reflog
β Wrong commit
β git revert or --amend
β Lost changes
β Use stash or reflog
π‘ Best Practices
β Commit frequently
β Use branches for features
β Pull before push
β Write clear commit messages
β Avoid reset --hard unless sure
π― Final Thought
Git is not just commandsβitβs your complete project history and safety system. Once you master these commands with real problem-solving, you become a confident developer ready for real-world projects.
#Git #GitCommands #LearnGit #Programming #WebDevelopment #Developers #CodingLife #SoftwareEngineering #TechSkills #GitHub π
