A comprehensive guide to Git commands for version control.
- Getting Started
- Basic Commands
- Branching and Merging
- Remote Repository Operations
- Inspection and Comparison
- Configuration
- Advanced Operations
- Git Workflows
# Create a new local repository
git init
# Clone an existing repository
git clone <repository-url>
git clone <repository-url> <directory-name># Add a specific file
git add <filename>
# Add all files
git add .
# Add all files with a specific extension
git add *.<extension>
# Add files interactively
git add -p# Commit with message
git commit -m "commit message"
# Add and commit tracked files
git commit -am "commit message"
# Amend last commit
git commit --amend# Show repository status
git status
# Show commit history
git log
# Show commit history with changes
git log -p
# Show compact history
git log --oneline
# Show branching history
git log --graph --oneline --decorate# List branches
git branch
# Create new branch
git branch <branch-name>
# Switch to branch
git checkout <branch-name>
# Create and switch to new branch
git checkout -b <branch-name>
# Delete branch
git branch -d <branch-name>
# Force delete branch
git branch -D <branch-name># Merge branch into current branch
git merge <branch-name>
# Abort merge
git merge --abort
# Continue merge after resolving conflicts
git merge --continue# Show remote repositories
git remote -v
# Add remote repository
git remote add <name> <url>
# Remove remote
git remote remove <name>
# Rename remote
git remote rename <old-name> <new-name># Fetch changes from remote
git fetch <remote>
# Pull changes from remote
git pull <remote> <branch>
# Push changes to remote
git push <remote> <branch>
# Push new branch to remote
git push -u <remote> <branch># Show unstaged changes
git diff
# Show staged changes
git diff --staged
# Show changes between branches
git diff <branch1> <branch2>
# Show changes in specific file
git diff <filename># Show file history
git log <filename>
# Show who changed what and when
git blame <filename>
# Show commit details
git show <commit-hash># Set global user name
git config --global user.name "Your Name"
# Set global email
git config --global user.email "your.email@example.com"
# Set repository-specific user name
git config user.name "Your Name"
# Set repository-specific email
git config user.email "your.email@example.com"# Set default editor
git config --global core.editor "editor-name"
# Set default branch name
git config --global init.defaultBranch main
# List all configurations
git config --list# Stash changes
git stash
# List stashes
git stash list
# Apply stash
git stash apply
# Pop stash
git stash pop
# Drop stash
git stash drop# Rebase current branch
git rebase <branch>
# Interactive rebase
git rebase -i <commit-hash>
# Continue rebase after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort# Soft reset (keep changes staged)
git reset --soft <commit>
# Mixed reset (keep changes unstaged)
git reset --mixed <commit>
# Hard reset (discard changes)
git reset --hard <commit>
# Restore file to last commit
git restore <filename>
# Restore staged file
git restore --staged <filename>- Create feature branch
git checkout -b feature/new-feature- Make changes and commit
git add .
git commit -m "Add new feature"- Push feature branch
git push -u origin feature/new-feature-
Create pull request (through web interface)
-
After approval, merge and cleanup
git checkout main
git pull
git branch -d feature/new-feature- Create hotfix branch
git checkout -b hotfix/bug-fix main- Fix bug and commit
git add .
git commit -m "Fix critical bug"- Merge to main and production
git checkout main
git merge hotfix/bug-fix
git push origin main
git checkout production
git merge hotfix/bug-fix
git push origin production- Cleanup
git branch -d hotfix/bug-fix- Commit messages should be clear and descriptive
- Pull before pushing to avoid conflicts
- Use feature branches for new development
- Review changes before committing
- Keep commits atomic and focused
- Regularly sync with remote repository
- Document significant changes
- Get command help:
git help <command> - Get quick command reference:
git <command> -h - View manual page:
man git-<command>