Tell me how you use git and what main commands you use?

👨‍💻 Backend Developer 🟠 May come up 🎚️ Medium
#Git

Brief Answer

Git is a version control system I use to track changes in code:

  1. git init/clone — create/clone repository 📂
  2. git add/commit — save changes 💾
  3. git push/pull — sync with remote repository 🔄
  4. git branch/checkout — work with branches 🌿
  5. git merge/rebase — combine changes 🔀
  6. git status/log — view status and history 📊
  7. git stash — temporarily store changes 📝
# Main Git commands
git add .
git commit -m "Added new feature"
git push origin main
git pull origin main
git checkout -b feature/new-feature

Full Answer

Git is a distributed version control system that allows tracking code changes, working in teams, and managing project versions. 🚀

Getting Started with Git

To start working with Git, you need to either create a new repository or clone an existing one:

# Create a new repository
git init
 
# Clone an existing repository
git clone https://github.com/username/repository.git
 
# Configure name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Basic Workflow

The daily Git workflow includes these commands:

# Check repository status
git status
 
# Add files to staging
git add file.txt
git add .  # Add all changes
 
# Create a commit
git commit -m "Brief description of changes"
 
# Send changes to remote repository
git push origin main
 
# Get changes from remote repository
git pull origin main

Working with Branches

Branches allow developing features in isolation:

# View all branches
git branch
 
# Create a new branch
git branch feature/new-feature
 
# Create and switch to a new branch
git checkout -b feature/new-feature
 
# Switch between branches
git checkout main
 
# Delete a branch
git branch -d feature/new-feature

Combining Changes

To combine changes from different branches, use merge and rebase:

# Merge feature branch into current branch
git merge feature/new-feature
 
# Rebase current branch onto feature
git rebase feature/new-feature
 
# Resolve conflicts
# After manually resolving conflicts:
git add .
git merge --continue  # or git rebase --continue

Viewing History

For analyzing change history:

# View commit history
git log
 
# Compact history view
git log --oneline
 
# Graphical history representation
git log --graph --oneline --all
 
# View changes in specific commit
git show <commit-hash>

Temporary Storage

Stash allows temporarily saving unfinished changes:

# Save current changes
git stash
 
# Apply saved changes
git stash apply
 
# Apply and remove from stash
git stash pop
 
# View stash list
git stash list

Undoing Changes

For fixing mistakes:

# Discard changes in working directory
git checkout -- file.txt
 
# Unstage a file
git reset HEAD file.txt
 
# Modify the last commit
git commit --amend
 
# Undo last commit (keeping changes)
git reset --soft HEAD~1
 
# Completely undo last commit
git reset --hard HEAD~1

Working with Remote Repositories

For synchronizing with remote repositories:

# View remote repositories
git remote -v
 
# Add a remote repository
git remote add origin https://github.com/username/repository.git
 
# Fetch changes without merging
git fetch origin
 
# Fetch and merge changes
git pull origin main

Practical Examples

Typical Feature Workflow

# Create branch for new feature
git checkout -b feature/user-authentication
 
# Development and commits
git add .
git commit -m "Added login form"
git add .
git commit -m "Added password validation"
 
# Sync with main branch
git checkout main
git pull origin main
git checkout feature/user-authentication
git rebase main
 
# Push branch to remote repository
git push origin feature/user-authentication
 
# After code review and approval
git checkout main
git merge feature/user-authentication
git push origin main

Fixing a Production Bug

# Create hotfix branch from main
git checkout main
git checkout -b hotfix/critical-bug
 
# Fix the bug
git add .
git commit -m "Fixed critical bug"
 
# Push the fix
git push origin hotfix/critical-bug
 
# Apply fix to main
git checkout main
git merge hotfix/critical-bug
git push origin main

Limitations and Challenges

  1. Merge conflicts — require manual resolution
  2. Large binary files — Git isn’t optimized for them
  3. Learning curve — some operations are complex for beginners
  4. Monorepos — can become slow with large size

Best Practices

  1. Frequent commits — make small atomic commits 📝
  2. Meaningful messages — write clear commit messages 📌
  3. Feature branches — develop each feature in a separate branch 🌿
  4. Pull before Push — always sync before sending changes 🔄
  5. Code Review — use pull/merge requests for code verification 👁️

Common Mistakes

Wrong:

# Commit all changes without review
git add .
git commit -m "Changes"

Right:

# Review changes before commit
git status
git diff
git add file1.js file2.js
git commit -m "Added form validation and error handling"

Conclusion

Git is a powerful tool for code version management:

  • Basic commands — add, commit, push, pull
  • Branching — branch, checkout, merge, rebase
  • Analysis — status, log, diff
  • Fixes — stash, reset, revert

Use Git for effective teamwork and project history management! 🚀