Basic Git Concepts

Understanding these fundamental Git concepts is essential for developers working in any modern software development environment. Git’s distributed version control system powers collaboration across teams of all sizes.

  1. Repository - A project’s tracked codebase with full version history.

  2. Commit - A snapshot of your code at a specific point in time.

  3. Branch - A parallel version of your codebase for isolated development.

  4. Merge - Combines changes from one branch into another.

  5. Rebase - Rewrites commit history to create a linear project history.

  6. HEAD - A reference to the current checked-out commit in your repo.

  7. Detached HEAD - When you’re no longer on a branch, just viewing a specific commit.

  8. Staging Area (Index) - Where changes are prepared before committing.

  9. Reset - Moves HEAD and optionally unstages or deletes changes.

  10. Revert - Creates a new commit that undoes the changes of a previous commit.

  11. Cherry-Pick - Applies a specific commit from one branch onto another.

  12. Stash - Temporarily saves uncommitted changes to switch context.

  13. Conflict - Happens when Git can’t automatically merge two changes.

  14. .gitignore - Lists files and directories that Git should not track.

  15. Tag - A named pointer to a specific commit (often used for releases).

  16. Remote - A link to a repository hosted elsewhere (e.g., GitHub).

  17. Fetch vs Pull - fetch downloads changes, pull does fetch + merge.

  18. Push - Uploads your local commits to a remote repo.

  19. Fork - Creates a personal copy of someone else’s repo (common in open source).

  20. Upstream vs Origin - origin is your remote, upstream is the original source (if forked).

  21. Blame - Shows who last modified each line of a file.

  22. Bisect - Finds the exact commit that introduced a bug via binary search.

  23. Worktree - Allows multiple working directories from a single Git repo.

  24. Hooks - Scripts that run automatically on Git actions (e.g., pre-commit).

  25. Reflog - A history of where your HEAD and branches have pointed - a lifesaver for recovering lost work.

Common Git Workflows

Feature Branch Workflow

  • Main branch contains stable code
  • New features developed in dedicated branches
  • Pull/Merge requests used for code review
  • Features merged back to main once approved

Gitflow Workflow

  • Two main branches: main (production) and develop
  • Feature branches created from develop
  • Release branches for preparing new releases
  • Hotfix branches for emergency production fixes

Forking Workflow

  • Common in open source projects
  • Contributors fork the main repository
  • Changes made in feature branches on the fork
  • Pull requests submitted to the original repository

Best Practices for Git

Commit Practices

  • Write clear, descriptive commit messages
  • Make atomic commits (one logical change per commit)
  • Commit early and often
  • Use conventional commit formats (e.g., “feat: add user login”)

Branch Management

  • Keep branches short-lived when possible
  • Regularly pull from the main branch to stay current
  • Delete branches after merging to reduce clutter
  • Use descriptive branch names (e.g., feature/user-authentication)

Collaboration Etiquette

  • Pull before you push to avoid unnecessary conflicts
  • Communicate before major refactoring
  • Review your changes before committing
  • Squash trivial commits before sharing

Managing Large Files

  • Avoid committing large binary files to Git
  • Consider Git LFS (Large File Storage) for binary assets
  • Use appropriate .gitignore rules for build artifacts

Advanced Git Techniques

Interactive Rebase

  • Rewrites history by editing, squashing, or reordering commits
  • Useful for cleaning up commit history before sharing
  • Example: git rebase -i HEAD~3 to modify last 3 commits

Submodules and Subtrees

  • Include other Git repositories within your project
  • Useful for managing dependencies or sharing code across projects
  • Submodules link to a specific commit in the external repo
  • Subtrees copy the external repo’s content into your project

Git Aliases

  • Create shortcuts for common Git commands
  • Stored in Git config
  • Increases productivity for frequent operations
  • Example: git config --global alias.st status

Conclusion

A solid understanding of these Git concepts forms the foundation for effective version control and collaboration. As you grow more comfortable with these concepts, you’ll be able to recover from mistakes, maintain a clean project history, and work efficiently with teammates. Git’s flexibility allows for many different workflows, so finding what works best for your team is key to productive development.