Blog Init

Git 101

Essential Git Commands for Repository Management

Git is the backbone of modern software development, enabling developers to track changes, collaborate seamlessly, and maintain project history. Understanding the core Git commands and workflow is crucial for any developer working with version control.

Understanding Git's Architecture

Before diving into commands, it's important to understand Git's four-stage architecture:

Git Workflow

Essential Git Commands

1. git add

Adds changes from the working directory into the staging area.

git add filename.txt        # Add specific file
git add .                   # Add all changes
git add *.js               # Add all JavaScript files

2. git commit

Saves a snapshot of currently staged changes in the local repository with a descriptive message.

git commit -m "Add user authentication feature"
git commit -am "Fix login bug"  # Add and commit in one step

3. git push

Uploads committed changes from the local repository to a remote repository.

git push origin main        # Push to main branch
git push origin feature-branch  # Push to specific branch
git push -u origin main     # Set upstream and push

4. git fetch

Downloads changes from a remote repository without applying them locally.

git fetch origin           # Fetch from origin
git fetch --all           # Fetch from all remotes

5. git merge

Combines changes from one branch into another.

git merge feature-branch   # Merge feature-branch into current branch
git merge origin/main      # Merge remote main into current branch

6. git pull

Fetches and then merges changes from a remote repository into the local branch.

git pull origin main       # Pull from remote main
git pull                   # Pull from tracked branch

7. git diff

Shows changes not staged or committed yet.

git diff                   # Show unstaged changes
git diff --cached          # Show staged changes
git diff filename.txt      # Show changes in specific file

8. git diff HEAD

Shows changes between the current working directory and the latest commit.

git diff HEAD              # Compare with latest commit
git diff HEAD~1            # Compare with previous commit

9. git status

Shows the current state of the working directory and staging area.

git status                 # Full status
git status -s              # Short status format

10. git branch

Lists all local branches and shows the current branch.

git branch                 # List local branches
git branch -a              # List all branches (local and remote)
git branch new-feature     # Create new branch
git branch -d old-branch   # Delete branch

11. git checkout

Creates a branch or switches between branches.

git checkout main          # Switch to main branch
git checkout -b new-feature  # Create and switch to new branch
git checkout -- filename.txt  # Discard changes in file

12. git log

Shows commits in the current branch with detailed information.

git log                    # Full commit history
git log --oneline          # Condensed format
git log --graph            # Show branch structure
git log -5                 # Show last 5 commits

13. git stash

Temporarily saves uncommitted changes and applies them later.

git stash                  # Stash current changes
git stash pop              # Apply and remove latest stash
git stash list             # List all stashes
git stash apply stash@{0}  # Apply specific stash

14. git rebase

Applies commits from one branch to another, creating a linear history.

git rebase main            # Rebase current branch onto main
git rebase -i HEAD~3       # Interactive rebase last 3 commits

15. git reset

Undoes changes in the working directory and moves back to a specific commit.

git reset HEAD~1           # Undo last commit, keep changes
git reset --hard HEAD~1    # Undo last commit, discard changes
git reset filename.txt     # Unstage specific file

16. git revert

Undoes changes by creating a new commit that reverses previous changes.

git revert HEAD            # Revert last commit
git revert abc123          # Revert specific commit

17. git cherry-pick

Applies specific commits from one branch to another.

git cherry-pick abc123     # Apply specific commit
git cherry-pick abc123..def456  # Apply range of commits

Common Workflows

Basic Development Workflow

# 1. Check current status
git status

# 2. Add changes to staging
git add .

# 3. Commit changes
git commit -m "Implement new feature"

# 4. Push to remote
git push origin main

Feature Branch Workflow

# 1. Create and switch to feature branch
git checkout -b feature/user-auth

# 2. Make changes and commit
git add .
git commit -m "Add user authentication"

# 3. Push feature branch
git push origin feature/user-auth

# 4. Switch back to main and merge
git checkout main
git merge feature/user-auth

# 5. Clean up
git branch -d feature/user-auth

Collaboration Workflow

# 1. Fetch latest changes
git fetch origin

# 2. Update local main
git checkout main
git pull origin main

# 3. Create feature branch from updated main
git checkout -b feature/new-functionality

# 4. Work and push
git add .
git commit -m "Add new functionality"
git push origin feature/new-functionality

Best Practices

Conclusion

Mastering these essential Git commands will significantly improve your development workflow and collaboration capabilities. Start with the basic commands (add, commit, push, pull) and gradually incorporate more advanced features like branching, rebasing, and stashing as your projects grow in complexity.

Remember, Git is a powerful tool that becomes more valuable the more you understand its underlying concepts. Practice these commands regularly, and you'll soon find yourself navigating version control with confidence.