- Git is a distributed version control system (DVCS), meaning every developer has a full copy of the repository locally.
- Subversion (SVN) is centralized, meaning all changes must go through a central server.
- With Git, users can work offline, commit locally, and later sync with a remote repository.
graph TD;
subgraph Centralized Repository
A[Central Server] -->|commit| B[Developer 1];
A -->|commit| C[Developer 2];
A -->|commit| D[Developer 3];
B -->|push/pull| A;
C -->|push/pull| A;
D -->|push/pull| A;
end
subgraph Distributed Repository
E[Developer 1] -->|commit| F[Local Repo 1];
G[Developer 2] -->|commit| H[Local Repo 2];
I[Developer 3] -->|commit| J[Local Repo 3];
F -->|push/pull| K[Remote Repo];
H -->|push/pull| K;
J -->|push/pull| K;
K -->|push/pull| F;
K -->|push/pull| H;
K -->|push/pull| J;
end
- SVN stores differences (diffs) between file versions.
- Git stores snapshots of the entire project at each commit, making branching and reverting more efficient.
- This model makes Git much faster for operations like checking history and switching branches.
- In Git, each developer has a local repository, a copy of the entire project with all history.
- A remote repository (hosted on GitHub, GitLab, Azure DevOps, etc.) allows collaboration.
- Developers sync changes via
git push(uploading) andgit pull(downloading).
- Branches allow parallel development without affecting the main code.
- SVN branches are copies of directories in the central repo, making them heavyweight.
- Git branches are lightweight pointers to commits, making switching between branches nearly instant.
- Merging integrates branches back into the main code, resolving conflicts if needed.
- Working directory: The files as they currently exist on disk.
- Staging area (index): A place to prepare changes before committing.
- Commit: A saved snapshot of staged changes.
- This extra step (
git add) allows selective commits, unlike SVN, which commits everything immediately.
- Developers don’t need constant internet access.
- Every user has a full repository, making Git faster and more resilient.
- SVN commits go directly to the central server.
- In Git, commits are local first (
git commit), and developers push them later (git push).
- SVN branches require copying files on the server.
- Git branches are just pointers to commits, making them nearly instant to create.
- SVN merges are often manual and may require extra conflict resolution.
- Git supports fast-forward merges when no diverging commits exist.
git rebasemoves commits onto a new base, helping maintain a linear history.
- SVN handles binary files well since it stores diffs.
- Git struggles with binary files due to its snapshot model.
- Git LFS (Large File Storage) allows handling large files efficiently.
- Copies a remote repository to a local machine.
- Example:
git clone https://github.com/user/repo.git
- Unlike
svn checkout,git cloneincludes the full history.
- Fetches and integrates changes from the remote repository.
- Example:
git pull origin main
- Shows modified, staged, and untracked files.
- Example:
git status
- Moves files from the working directory to the staging area.
- Example:
git add file.txt git add .
- Records a snapshot of staged changes.
- Example:
git commit -m "Fixed bug in login form"
- Uploads local commits to a remote repository.
- Example:
git push origin main
- Shows commit history.
- Example:
git log --oneline --graph
- Shows changes between commits or working directory and staging.
- Example:
git diff
- Creates a new branch:
git branch feature-branch
- Switches to a branch:
(or)
git switch feature-branch # Newer commandgit checkout feature-branch # Older method - Creates and switches:
git switch -c feature-branch
- Merges a branch into the current branch:
git merge feature-branch
- If conflicts occur, Git highlights them in files.
- Moves commits onto another branch:
git rebase main
- Interactive rebase allows editing history:
git rebase -i HEAD~3
- Saves changes temporarily:
git stash
- Restores the stashed changes:
git stash pop
- Moves HEAD and uncommits:
git reset --soft HEAD~1 # Keep changes git reset --hard HEAD~1 # Remove changes
- Reverts a commit without modifying history:
git revert <commit-hash>
- Applies a specific commit from another branch:
git cherry-pick <commit-hash>
- Modifies the last commit:
git commit --amend -m "Updated commit message" - Rewrites multiple commits:
git rebase -i HEAD~3
- Lists remotes:
git remote -v
- Fetches without merging:
git fetch
- Safe force push:
git push --force-with-lease
- Follow the convention:
type(scope): messagefeat(auth): Add login feature fix(cart): Correct checkout bug
- Merge feature branches quickly to avoid conflicts.
- Only use
--force-with-leaseto prevent data loss.
- Ignore unnecessary files (e.g., logs, temp files):
node_modules/ .env
- Use
git mergeorgit rebaseand resolve conflicts in files.