Git Key Concepts¶
Git is a distributed version control system that tracks changes in your files and enables collaboration. Understanding its core concepts is essential before using it effectively.
๐ Repository (Repo)¶
A repository is a directory that Git tracks. It stores the full history of all changes made to the project.
- Local repository โ exists on your machine.
- Remote repository โ hosted on a server (e.g., GitHub, GitLab, Bitbucket).
# Initialize a new local repository
git init
# Clone an existing remote repository
git clone <repository_url>
๐ธ Commits¶
A commit is a snapshot of your changes at a specific point in time. Every commit has:
- A unique hash (e.g.,
a3f8b1c) - An author and timestamp
- A commit message describing the change
# Stage changes and commit with a message
git add .
git commit -m "Add user authentication feature"
Best Practice: Write clear, concise commit messages that explain what changed and why.
๐๏ธ Staging Area (Index)¶
The staging area (also called the index) is a preparation zone between your working directory and the repository. You explicitly choose which changes to include in the next commit.
# Stage a specific file
git add <filename>
# Stage all changes
git add .
# View staged and unstaged changes
git status
# List all tracked files in the staging area
git ls-files
# Unstage a file (keep changes in working directory)
git reset HEAD <filename>
๐ฟ Branches¶
A branch is an independent line of development. Branches let you work on features or fixes in isolation without affecting the main codebase.
- The default branch is usually called
main(ormaster). - Create a new branch for every feature or bug fix.
# Create and switch to a new branch
git checkout -b feature/login
# List all branches
git branch -a
# Switch to an existing branch
git checkout main
๐ Merging¶
Merging integrates changes from one branch into another. Git creates a merge commit to preserve the history of both branches.
# Merge a feature branch into main
git checkout main
git merge feature/login
๐ Rebase¶
Rebasing re-applies commits from one branch on top of another, producing a linear history without merge commit bubbles.
Scenario: Sync your feature branch with the latest main before opening a pull request.
git checkout feature
git rebase main
When to use: Prefer
rebasefor a clean history; prefermergewhen you want to preserve the full branch history.
๐ Cherry-Pick¶
Cherry-picking applies the changes from a specific commit to the current branch โ without merging the entire branch.
Scenario: A bug fix commit on feature needs to go to main immediately.
git checkout main
git cherry-pick <commit-hash>
๐ Commit History¶
Navigate and inspect the history of commits.
# Visual graph of all commits
git log --oneline --graph --decorate
# Commits from the last 3 days
git log --since="3 days ago"
# Commits that changed a specific file
git log -- <filename>
# View the full diff of a specific commit
git show <commit-hash>
๐ Ignoring Files (.gitignore)¶
The .gitignore file tells Git which files and directories to never track โ such as build artifacts, secrets, and dependency folders.
Create a .gitignore file and add patterns:
node_modules/
*.log
.env
secret.json
dist/
Tip: GitHub provides ready-made
.gitignoretemplates for popular languages and frameworks.
โ๏ธ Configuration & Aliases¶
Set Your Identity¶
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Create Aliases (Shortcuts)¶
Aliases let you define shortcuts for frequently used commands.
# Create a 'loggraph' alias
git config --global alias.loggraph "log --all --oneline --graph --decorate"
# Use the alias
git loggraph
๐ Getting Help¶
# Open the manual for any Git command
git help <command>
# Examples
git help commit
git help stash
๐ง Quick Quiz โ Git Key Concepts¶
Which Git concept lets you work on a new feature in isolation without affecting the main codebase?
๐ Want More Practice?¶
๐ Test your knowledge - Take the Git Basics Quiz
๐ฌ DevopsPilot Weekly โ Learn DevOps, Cloud & Gen AI the simple way.
๐ Subscribe here