Understanding Basic Commands
Git Configuration
Before diving into commands, set up your identity:
1git config --global user.name "Your Name" 2git config --global user.email "your.email@example.com"
Essential Commands Explained
- Repository Initialization
1git init
This creates a .git directory that stores all the version control information. After initialization, Git starts tracking all files in the directory.
- Staging Changes
1git add filename.txt # Stage specific file 2git add . # Stage all changes 3git add *.js # Stage all JavaScript files 4git add src/ # Stage entire directory
The staging area (or index) is like a preparation zone where you organize changes before committing.
- Checking Status
1git status 2git status -s # Short format status
Status flags:
- M: Modified
- A: Added
- D: Deleted
- ??: Untracked
- R: Renamed
- Committing Changes
1git commit -m "Add login feature" 2git commit -am "Update styles" # Automatically stage tracked files and commit
- Viewing History
1git log 2git log --oneline # Compact view 3git log --graph # Visual representation 4git log --author="name" # Filter by author 5git log -p # Show patches
Advanced Branching Strategies
GitFlow Model
A robust branching strategy following Vincent Driessen's model:
-
Main Branches
main: Production code onlydevelop: Integration branch
-
Supporting Branches
- Feature branches:
feature/user-authentication - Release branches:
release/1.0.0 - Hotfix branches:
hotfix/login-bug
- Feature branches:
Branch Management Commands
1# Create and switch to new branch 2git checkout -b feature/new-feature 3 4# List branches 5git branch # Local branches 6git branch -r # Remote branches 7git branch -a # All branches 8 9# Delete branch 10git branch -d branch-name # Safe delete 11git branch -D branch-name # Force delete 12 13# Rename branch 14git branch -m old-name new-name
Merging Strategies
- Fast-forward Merge
1git merge feature-branch
- No-fast-forward Merge
1git merge --no-ff feature-branch
- Squash Merge
1git merge --squash feature-branch
Comprehensive Commit Message Guidelines
Structure
<type>(<scope>): <subject>
<body>
<footer>
Types
- feat: New feature
- fix: Bug fix
- docs: Documentation
- style: Formatting
- refactor: Code restructuring
- test: Adding tests
- chore: Maintenance
Example
feat(auth): implement OAuth2 authentication
- Add OAuth2 client configuration
- Implement token refresh mechanism
- Add user session management
Closes #123
Best Practices
- Separate subject from body with blank line
- Limit subject line to 50 characters
- Capitalize the subject line
- Don't end subject line with period
- Use imperative mood in subject line
- Wrap body at 72 characters
- Use body to explain what and why vs. how
Working with Remotes in Detail
Remote Management
1# Add remote 2git remote add origin https://github.com/user/repo.git 3 4# View remotes 5git remote -v 6 7# Change remote URL 8git remote set-url origin new-url 9 10# Remove remote 11git remote remove origin
Push Operations
1# Push to remote 2git push origin branch-name 3 4# Push all branches 5git push --all origin 6 7# Force push (use carefully!) 8git push -f origin branch-name 9 10# Set upstream branch 11git push -u origin feature-branch
Pull Operations
1# Fetch remote changes 2git fetch origin 3 4# Fetch and merge 5git pull origin main 6 7# Pull with rebase 8git pull --rebase origin main
Advanced Git Operations
Stashing
1# Basic stash 2git stash 3 4# Stash with message 5git stash save "WIP: implementing login" 6 7# List stashes 8git stash list 9 10# Apply stash 11git stash apply stash@{0} 12 13# Pop stash 14git stash pop 15 16# Drop stash 17git stash drop stash@{0}
Recovery and Undo
1# Discard changes in working directory 2git checkout -- filename 3 4# Undo last commit (keep changes) 5git reset HEAD~1 6 7# Revert commit 8git revert commit-hash 9 10# Amend last commit 11git commit --amend
.gitignore Patterns
# Ignore specific file
secret.key
# Ignore directory
node_modules/
# Ignore by pattern
*.log
**/temp
Troubleshooting Common Issues
- Merge Conflicts
1# Abort merge 2git merge --abort 3 4# After resolving conflicts 5git add . 6git commit
- Detached HEAD State
1git checkout main 2git checkout -b new-branch # If you need to save changes
- Large File Issues
1# Clean repository 2git gc 3git prune 4 5# Remove file from history 6git filter-branch --force --index-filter \ 7 'git rm --cached --ignore-unmatch PATH-TO-FILE' \ 8 --prune-empty --tag-name-filter cat -- --all
#git#version-control#programming#developer-tools#software-development#coding-basics#git-commands#git-workflow#branching-strategy#commit-messages#remote-repository#technical-guide#development-basics#code-management#software-engineering