About Git
Git is a distributed version control system that tracks versions of files. It is often used to control source code by programmers who are developing software collaboratively. Design goals of Git include speed, data integrity, and support for distributed, non-linear workflows — thousands of parallel branches running on different computers.
Table of Contents
Basic Commands
Repository Setup
git init # Initialize a new repository
git clone <url> # Clone a repository
git status # Check repository status
Staging Changes
git add <file> # Add file to staging area
git add . # Add all changes to staging area
git add -p # Interactive staging
Committing
git commit -m " message " # Commit with message
git commit -am " message " # Add and commit in one command
git commit --amend # Modify the last commit
Branch Management
git branch # List branches
git branch <name> # Create new branch
git checkout <branch> # Switch to branch
git checkout -b <branch> # Create and switch to new branch
git switch <branch> # Switch to branch (new syntax)
git switch -c <branch> # Create and switch to new branch (new syntax)
git merge <branch> # Merge branch into current branch
git rebase <branch> # Rebase current branch onto another
git merge --abort # Abort merge in case of conflicts
git rebase --abort # Abort rebase
Remote Operations
git remote add origin <url> # Add remote repository
git remote -v # List remote repositories
git push origin <branch> # Push changes to remote
git pull origin <branch> # Pull changes from remote
git fetch origin # Fetch changes without merging
Undoing Changes
Working Directory
git checkout -- <file> # Discard changes in working directory
git clean -fd # Remove untracked files and directories
Staged Changes
git reset <file> # Unstage changes
git reset # Unstage all changes
Commits
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --hard HEAD~1 # Undo last commit and discard changes
git revert <commit> # Create new commit that undoes changes
Git Configuration
git config --global user.name " Your Name "
git config --global user.email " your@email.com "
git config --global core.editor " code --wait " # Set VS Code as default editor
git config --list # List all configurations
Advanced Operations
git stash # Stash changes
git stash list # List stashes
git stash pop # Apply and remove latest stash
git stash apply # Apply latest stash without removing it
git log # View commit history
git log --oneline # Compact commit history
git blame <file> # Show who changed what in a file
git reflog # View reference logs
git tag -a v1.0 -m " Version 1.0 " # Create annotated tag
git push origin --tags # Push tags to remote
Best Practices
Write clear, descriptive commit messages
Commit early and often
Keep branches focused and short-lived
Pull before pushing to avoid conflicts
Use .gitignore
for files that shouldn’t be tracked
Regularly fetch and merge from upstream
Use meaningful branch names
Common Workflows
Feature Branch Workflow
Create feature branch
Make changes and commit
Pull latest main/develop
Merge or rebase
Push and create pull request
Hotfix Workflow
Branch from production/main
Fix issue and commit
Merge to both main and develop
Tag release if necessary