Git vs. GitHub Analogy #
- Git: A local version control tool that tracks changes to files (who, what, when, where). Think of it as the "coffee."
- GitHub: A cloud-based platform for hosting Git repositories and collaborating with others. Think of it as the "coffee shop."
- Alternatives: GitLab and Bitbucket are other popular remote hosting services.
Core Architecture & Workflow #
- Local Level:
- Working Directory: The folder where you actively create and edit files.
- Staging Area (Index): A middle ground where you prepare changes before saving them permanently.
- Local Repository: A digital cabinet on your computer (the hidden
.gitfolder) where version history is stored.
- Remote Level:
- Remote Repository: The cloud-hosted version (e.g., on GitHub) used for backup and team collaboration.
Getting Started: Installation & Setup #
- Installation: Available for Windows (Git Bash), macOS, and Linux via the official Git website.
- Verification: Use
git --versionto confirm successful installation. - Configuration: Mandatory first-time setup using
git config --global user.email "email@example.com"andgit config --global user.name "Name". This identifies the author of every commit.
Initializing and Cloning #
git init: Turns an existing local folder into a Git repository, creating the hidden.gitmetadata folder.git clone [URL]: Copies an existing remote repository from the cloud to your local machine.
The Staging Process (git add) #
git status: Shows which files are modified, deleted, or untracked.git add .: Stages all changes in the current directory and subdirectories.git add -A/git add --all: Stages all changes across the entire project.git add [filename]: Stages a specific file.git reset: Moves files from the staging area back to the working directory (undoes the "add").
Committing Changes #
git commit -m "message": Permanently saves staged changes into the local repository with a descriptive note.git log: Displays the history of commits including IDs (hashes), authors, and timestamps. Use--onelinefor a compact view.
Deleting and Restoring Files #
git rm [file]: Deletes the file and stages the deletion simultaneously.git rm --cached [file]: Removes the file from Git tracking but keeps the physical file in your folder.git restore [file]: Discards uncommitted changes in the working directory, returning the file to its last committed state.git reset --hard: Forcefully reverts the entire project to the last commit, discarding all uncommitted changes and restored deleted files.
Branching and Merging #
- Concepts: Branches allow for isolated development (e.g., "feature" or "development" branches) without affecting the "main" stable code.
git branch [name]: Creates a new branch.git checkout [name]: Switches to a different branch or a specific commit ID.git merge [branch]: Combines changes from the target branch into your current branch.- Merge Conflicts: Occur when the same line of a file is modified differently in two branches. These must be resolved manually by the developer before finalizing the merge.
Remote Operations #
git push origin [branch]: Uploads local commits to the remote server.git fetch: Downloads updates from the remote repository but does not merge them into your local files.git pull: A combination offetchandmerge. It brings remote changes directly into your active working directory.
Advanced Git Utilities #
git stash: Temporarily "hides" uncommitted changes so you can switch branches without committing unfinished work.git stash pop: Restores stashed changes and removes them from the stash list.git stash apply: Restores changes but keeps them in the stash list for future use.
git diff [ID1] [ID2]: Shows the line-by-line differences between two specific commits.git revert [ID]: Undoes a specific previous commit by creating a new commit that is the exact opposite (best for public/shared branches).git rebase [branch]: Rewrites commit history by moving your current branch's changes to the "tip" of the target branch, resulting in a cleaner, linear history.
GitHub Workflows & Pull Requests #
- Pull Request (PR): A request to the project owner to review and merge your branch's changes into the main codebase.
- Process: Push your branch -> Create PR on GitHub -> Peer Review/Discussion -> Merge PR -> Delete feature branch.
Summary #
This course covers the full lifecycle of a Git project: from initializing a repository and managing the local three-tier architecture (Working Directory, Staging, Repository) to advanced collaboration on GitHub. Key takeaways include mastering branching strategies to avoid breaking production code, handling merge conflicts, and using professional tools like Rebase, Stash, and Pull Requests to maintain a clean and efficient development workflow.
last updated: