Git Introduction¶
A comprehensive overview of Git concepts, workflows, and fundamental operations.
How Git Works¶
Git tracks changes through a series of snapshots. Here's a visual representation of how data flows through Git's core components:
flowchart LR
subgraph "Local Environment"
A[Working Directory]
B[Staging Area Index]
C[Local Repository .git directory]
end
subgraph "Remote Environment"
D[Remote Repository e.g. GitHub/GitLab]
end
A -->|git add| B
B -->|git commit| C
C -->|git push| D
D -->|git pull| C
C -->|git checkout| A
How to Use This Guide¶
- Browse by Topic: Use the Table of Contents to find specific git operations
- Quick Copy: All commands are formatted for easy copy-pasting
- Detailed Pages: Click section links for comprehensive guides on each topic
- Workflow Reference: Refer to the workflow diagrams for common git patterns
Getting Started: Core Basics¶
Repository Initialization¶
Initialize a new git repository:
Clone an existing repository:
Set user identity (global):
Ignoring & Keeping Files¶
Add a .gitignore file:
Defines intentionally untracked files to ignore.
Track an otherwise empty directory (with .gitkeep):
Create directory:
Create .gitkeep file:
Note:
.gitkeepis not built-in but is a common convention.
Basic Git Workflow¶
Stage changes:
Stage specific file:
Stage all changes:
Commit changes:
Commit with message:
Add and commit tracked files in one step:
Check status:
Full status:
Short format:
View changes:
Unstaged changes:
Staged changes:
Common Git Workflows¶
Feature Branch Workflow¶
Common in team environments where features are developed in isolation.
gitGraph
commit id: "main"
branch feature
checkout feature
commit id: "work"
commit id: "more work"
checkout main
merge feature
commit id: "release"
Gitflow Workflow¶
A structured branching model with dedicated branches for development, features, releases, and hotfixes.
gitGraph
commit id: "initial"
branch develop
checkout develop
commit id: "dev work"
branch feature
checkout feature
commit id: "feature work"
checkout develop
merge feature
branch release
checkout release
commit id: "release prep"
checkout main
merge release
checkout develop
merge release
branch hotfix
checkout hotfix
commit id: "urgent fix"
checkout main
merge hotfix
checkout develop
merge hotfix
Forking Workflow¶
Popular in open source projects, where contributors fork the main repository.
gitGraph
commit id: "initial commit"
branch upstream/main
checkout upstream/main
commit id: "upstream work"
branch fork/main
checkout fork/main
commit id: "fork created"
commit id: "clone locally"
branch feature
checkout feature
commit id: "work"
commit id: "more work"
checkout fork/main
merge feature id: "merge to fork"
checkout upstream/main
commit id: "upstream changes"
merge fork/main id: "PR merged"