Beginner 4 min read December 10, 2025

Getting Started with Git: Version Control Fundamentals

Learn the essentials of Git version control system for effective codebase management

What is Git?

Git is a distributed version control system that tracks changes in files and coordinates work among multiple developers. It allows you to save snapshots of your project, revert to previous versions, create branches for parallel development, and merge changes from different contributors.

Unlike centralized version control systems, Git stores a complete copy of the repository on each developer's machine, enabling offline work and faster operations. Git has become the standard for version control in software development. For DevOps integration, see our DevOps & Infrastructure guide.

Core Concepts

Repository

A repository (repo) is a directory containing your project files and Git's version history. It tracks all changes and maintains the complete history.

Commit

A commit is a snapshot of your project at a specific point in time. Each commit has a unique hash and includes a message describing the changes.

Branch

A branch is an independent line of development. The default branch is usually called "main" or "master". Branches allow parallel work without conflicts.

Remote

A remote is a version of your repository hosted on a server (like GitHub, GitLab, or Bitbucket). Remotes enable collaboration and backup.

Basic Git Commands

Initialization and Configuration

# Initialize a new repository
git init

# Configure user information
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Check configuration
git config --list

Making Changes

# Check status of files
git status

# Stage files for commit
git add filename.txt
git add .                    # Stage all changes

# Commit changes
git commit -m "Add new feature"

# View commit history
git log
git log --oneline           # Compact view

Working with Branches

# List branches
git branch

# Create a new branch
git branch feature-branch

# Switch to a branch
git checkout feature-branch
git switch feature-branch   # Modern alternative

# Create and switch in one command
git checkout -b feature-branch

# Merge a branch
git checkout main
git merge feature-branch

# Delete a branch
git branch -d feature-branch

Working with Remotes

# Add a remote repository
git remote add origin https://github.com/user/repo.git

# View remotes
git remote -v

# Push changes to remote
git push origin main

# Pull changes from remote
git pull origin main

# Clone a repository
git clone https://github.com/user/repo.git

Common Workflows

Feature Branch Workflow

Create a branch for each feature, work on it independently, then merge back to main. This keeps the main branch stable and allows parallel development.

# Create feature branch
git checkout -b feature/new-login

# Make changes and commit
git add .
git commit -m "Implement new login feature"

# Push branch to remote
git push origin feature/new-login

# After code review, merge to main
git checkout main
git merge feature/new-login
git push origin main

Git Flow

A branching model with main branches (main, develop) and supporting branches (feature, release, hotfix). Suitable for projects with scheduled releases.

Main branch contains production-ready code. Develop branch is for integration of features. Feature branches branch off develop. Release branches prepare new releases. Hotfix branches fix production issues. For CI/CD integration, see our DevOps & Infrastructure guide.

Best Practices

Commit Messages

Write clear, descriptive commit messages. Use imperative mood ("Add feature" not "Added feature"). Include context about what changed and why. Follow conventions like Conventional Commits for consistency.

# Good commit messages
git commit -m "Add user authentication"
git commit -m "Fix memory leak in image processing"
git commit -m "Refactor database connection handling"

# Bad commit messages
git commit -m "fix"
git commit -m "changes"
git commit -m "WIP"

Frequent Commits

Commit often with logical, atomic changes. Each commit should represent a complete, working change. This makes history easier to understand and allows easier rollback if needed.

Branch Naming

Use descriptive branch names that indicate purpose: feature/user-authentication, bugfix/login-error, hotfix/security-patch. Follow team conventions for consistency.

.gitignore

Use .gitignore to exclude files that shouldn't be versioned: dependencies (node_modules), build outputs, IDE files, environment variables, and sensitive data.

# Example .gitignore
node_modules/
dist/
*.log
.env
.DS_Store
*.swp

Pull Before Push

Always pull latest changes before pushing. This prevents conflicts and keeps your local repository synchronized with the remote. Use `git pull --rebase` for cleaner history.

Advanced Topics

Stashing

Stash temporarily saves uncommitted changes, allowing you to switch branches or pull updates without committing incomplete work.

# Stash changes
git stash

# List stashes
git stash list

# Apply stash
git stash apply
git stash pop              # Apply and remove

# Drop stash
git stash drop

Undoing Changes

# Discard changes in working directory
git checkout -- filename
git restore filename        # Modern alternative

# Unstage files
git reset HEAD filename
git restore --staged filename

# Amend last commit
git commit --amend

# Revert a commit (creates new commit)
git revert commit-hash

# Reset to previous commit (dangerous)
git reset --soft HEAD~1    # Keep changes staged
git reset --hard HEAD~1    # Discard all changes

Rebasing

Rebasing replays commits from one branch onto another, creating a linear history. Useful for keeping feature branches up-to-date with main.

# Rebase current branch onto main
git checkout feature-branch
git rebase main

# Interactive rebase (edit commits)
git rebase -i HEAD~3

# Continue after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

Collaboration

Git enables effective collaboration through pull requests (or merge requests), code reviews, and conflict resolution. Use pull requests to propose changes, discuss code, and ensure quality before merging.

When conflicts occur, Git marks them in files. Resolve conflicts manually, stage resolved files, and complete the merge or rebase. For team workflows, establish branching strategies and code review processes. For CI/CD integration, see our DevOps & Infrastructure documentation.

Related Topics