Git Topic
What is Git?
Git is a distributed version control system that tracks changes in source code during software development. It allows multiple developers to work on the same project simultaneously without interfering with each other's work.
Key Concepts
1. Repository (Repo)
-
A storage location for your project files and their history
-
Can be local (on your machine) or remote (on servers like GitHub, GitLab)
2. Commits
-
Snapshots of your project at a specific point in time
-
Each commit has a unique hash ID
3. Branches
-
Independent lines of development
-
main/masteris the default branch
4. Staging Area
-
Intermediate area where you prepare changes before committing
Basic Git Workflow
Working Directory → Staging Area → Repository
(modified) (staged) (committed)
Essential Git Commands
1. Setup and Configuration
# Set your username
git config --global user.name "Your Name"
# Set your email
git config --global user.email "[email protected]"
# Check configuration
git config --list
# Set default branch name
git config --global init.defaultBranch main
2. Starting a Repository
# Initialize a new Git repository
git init
# Clone an existing repository
git clone https://github.com/username/repository.git
# Clone with specific name
git clone https://github.com/username/repository.git my-project
3. Basic Workflow Commands
# Check repository status
git status
# Add files to staging area
git add filename.txt # Add specific file
git add . # Add all changes
git add *.js # Add all JS files
git add src/ # Add entire directory
# Commit changes
git commit -m "Commit message describing changes"
# Add and commit in one step (only for modified files)
git commit -am "Commit message"
4. Viewing History and Changes
# View commit history
git log
git log --oneline # Compact view
git log --graph # With branch visualization
git log -p # With changes shown
git log --since="2023-01-01" # Filter by date
# See what files have changed
git diff # Unstaged changes
git diff --staged # Staged changes
git diff commit1 commit2 # Between two commits
# Show specific commit
git show commit-hash
5. Branching and Merging
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create new branch
git branch feature-branch
# Switch to branch
git checkout feature-branch
# Create and switch to new branch
git checkout -b feature-branch
# Merge branches
git checkout main
git merge feature-branch
# Delete branch
git branch -d feature-branch # Safe delete
git branch -D feature-branch # Force delete
6. Remote Repositories
# Add remote repository
git remote add origin https://github.com/username/repo.git
# View remotes
git remote -v
# Push to remote
git push origin main
git push -u origin main # Set upstream branch
# Pull from remote
git pull origin main
# Fetch changes (without merging)
git fetch origin
# Clone specific branch
git clone -b branch-name https://github.com/username/repo.git
7. Undoing Changes
# Unstage file (keep changes)
git reset filename.txt
# Discard changes in working directory
git checkout -- filename.txt
# Revert to previous commit (creates new commit)
git revert commit-hash
# Reset to previous commit (dangerous - loses history)
git reset --hard commit-hash
git reset --soft commit-hash # Keep changes staged
# Clean untracked files
git clean -fd # Remove untracked files/directories
8. Stashing
# Save changes temporarily
git stash
git stash save "Message"
# List stashes
git stash list
# Apply last stash
git stash apply
# Apply specific stash
git stash apply stash@{1}
# Apply and remove from stash
git stash pop
# Delete stash
git stash drop stash@{1}
git stash clear # Remove all stashes
9. Tagging
# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0 release"
# List tags
git tag
# Push tags to remote
git push origin --tags
git push origin v1.0.0 # Specific tag
Common Workflows
1. Feature Branch Workflow
# Start new feature
git checkout -b new-feature
# Make changes and commit
git add .
git commit -m "Implement new feature"
# Push to remote
git push -u origin new-feature
# Merge to main
git checkout main
git pull origin main
git merge new-feature
git push origin main
# Clean up
git branch -d new-feature
2. Fixing Mistakes
# If you forgot to add files to last commit
git add missing-file.txt
git commit --amend --no-edit
# If you want to change commit message
git commit --amend -m "New commit message"
# If you committed to wrong branch
git reset --soft HEAD~1 # Undo commit but keep changes
git stash # Save changes
git checkout correct-branch
git stash pop
git add .
git commit -m "Your message"
Advanced Commands
1. Rebasing
# Rebase current branch onto main
git checkout feature-branch
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
2. Cherry-picking
# Apply specific commit to current branch
git cherry-pick commit-hash
3. Submodules
# Add submodule
git submodule add https://github.com/username/repo.git
# Initialize submodules
git submodule init
git submodule update
Git Practices
1. Commit Messages
-
Use imperative mood ("Add feature" not "Added feature")
-
First line should be short (50 chars max)
-
Include detailed description if needed
2. Branch Naming
-
Use descriptive names:
feature/user-authentication -
Conventional prefixes:
feature/,bugfix/,hotfix/,release/
3. Regular Workflow
# Daily routine
git pull origin main # Get latest changes
# Make changes
git add .
git commit -m "Meaningful message"
git push origin current-branch
Common Issues and Solutions
1. Merge Conflicts
-
Edit conflicted files
-
Remove conflict markers (
<<<<<<<,=======,>>>>>>>) -
git addresolved files -
git committo complete merge
2. Detached HEAD
# If you see "You are in 'detached HEAD' state"
git checkout branch-name # Return to a branch
3. Recovering Lost Commits
# Find lost commits
git reflog
# Reset to specific reflog entry
git reset --hard HEAD@{1}
Git Ignore File (.gitignore)
Create a .gitignore file to exclude files from tracking:
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.exe
# Environment variables
.env
.env.local
# OS files
.DS_Store
Thumbs.db
# IDE files
.vscode/
.idea/
*.swp
This extensive guide covers the majority of Git usage scenarios. Remember that Git has a steep learning curve, so practice regularly and don't hesitate to experiment in a test repository!
Comments (1)
Very nice