Git
1. What is Git?
- Git is a distributed version control system for tracking changes in code, enabling collaboration, and managing project history.
- Every user has a full local copy of the repository.
2. Key Concepts
- Repository (Repo): Where your project files and history live (local or remote).
- Commit: A snapshot of changes with a unique hash.
- Branch: A separate line of development (default is often
main
).
- Merge: Combines changes from one branch into another.
- Rebase: Moves or “replays” commits from one branch onto another, creating a linear history.
- Staging Area: Prepares changes for a commit.
- Clone: Copies a remote repo locally.
- Push/Pull: Syncs changes with a remote repo.
3. Basic Git Workflow
git init: Starts a new repo.
git status: Checks the current state.
git add <file> or git add .: Stages changes.
git commit -m "message": Commits staged changes.
git push :Syncs changes with a remote repo.
git log: Views commit history.
4. Branching and Merging
Create Branch: git branch <branch-name>
Switch Branch: git checkout <branch-name> (or git switch <branch-name>)
Merge: git merge <branch-name>
Delete Branch: git branch -d <branch-name>
5. Rebasing
- What is Rebasing? Rebasing rewrites commit history by moving your branch’s commits onto the tip of another branch. It avoids merge commits for a cleaner history.
- Command:
git rebase <branch-name>
(e.g., git rebase main
applies your current branch’s commits on top of main
).
- Interactive Rebasing:
git rebase -i <commit-hash>
lets you edit, squash, or reorder commits.
- Resolve Conflicts: If conflicts arise, fix them, then
git rebase --continue
.
6. Remote Repositories
git clone <url>: Copies a remote repo.
git remote add origin <url>: Links to a remote.
git push origin <branch-name>: Uploads changes.
git pull origin <branch-name>: Downloads updates.
7. Undoing Changes
git restore <file>: Discards unstaged changes.
git reset --soft HEAD^: Undoes last commit, keeps changes.
git revert <commit-hash>: Creates a new commit undoing a previous one.
8. Common Commands
git diff: Shows changes.
git fetch: Downloads remote updates without merging.
git stash: Shelves uncommitted changes.
Post Views: 25