Title: Git Version Control — LearnCoachAssist

Q: What is Git?
A: Git is a distributed version control system that tracks changes in source code during software development, allowing multiple developers to collaborate efficiently.
It stores snapshots of files over time, enabling branching, merging, and reverting changes.

Q: How do you initialize a new Git repository?
A: Use the command git init in the root directory of your project.
This creates a hidden .git subdirectory containing all repository data.

Q: What does git clone do?
A: git clone <url> creates a copy of an existing remote repository on your local machine.
It includes all branches, tags, and history.

Q: What is the purpose of git add?
A: git add <file> stages changes in the working directory for the next commit.
You can stage all changes with git add . or specific files.

Q: What does git commit accomplish?
A: git commit -m "message" saves the staged changes to the local repository as a snapshot.
The commit message should describe the changes concisely.

Q: How do you check the status of your repository?
A: git status shows the state of the working directory and staging area.
It lists modified, untracked, and staged files.

Q: What information does git log provide?
A: git log displays the commit history, including commit hashes, authors, dates, and messages.
Use options like --oneline or --graph for compact views.

Q: What is the Git working directory?
A: The working directory is the filesystem where you edit files and where Git tracks changes.
It contains files checked out from the repository.

Q: What is the Git staging area (index)?
A: The staging area holds a snapshot of changes ready to be committed.
Changes must be added here with git add before committing.

Q: What is a commit in Git?
A: A commit is a snapshot of the entire repository at a point in time, identified by a unique SHA-1 hash.
It includes metadata like author, date, and message.

Q: How do you create a new branch?
A: git branch <branch-name> creates a new branch pointing to the current commit.
Use git checkout -b <branch-name> to create and switch to it.

Q: What does git checkout do?
A: git checkout <branch> switches to the specified branch.
For files, it restores them from the index or a commit.

Q: How do you list all branches?
A: git branch lists local branches; git branch -r for remote; git branch -a for all.
The current branch is marked with an asterisk.

Q: What is git merge?
A: git merge <branch> integrates changes from the specified branch into the current one.
It creates a merge commit if there's a divergence.

Q: What is a fast-forward merge?
A: A fast-forward merge occurs when the current branch has no diverging commits, moving the branch pointer forward.
No merge commit is created; use --no-ff to force one.

Q: How do you delete a branch?
A: git branch -d <branch> deletes a merged local branch safely.
Use -D to force deletion of unmerged branches.

Q: What is git rebase?
A: git rebase <branch> reapplies commits from the current branch onto another, creating a linear history.
It rewrites commit history, so avoid on shared branches.

Q: What is the difference between merge and rebase?
A: Merge preserves the exact history with a merge commit; rebase rewrites history for a linear timeline.
Rebase is cleaner but alters commit hashes.

Q: What does git stash do?
A: git stash temporarily saves uncommitted changes and reverts the working directory to match the last commit.
Retrieve with git stash pop.

Q: How do you view stashes?
A: git stash list shows all stashes with indices.
Apply a specific one with git stash apply stash@{n}.

Q: What is a remote repository?
A: A remote repository is a Git repo hosted on a server, like GitHub, for collaboration.
Locals push/pull changes to/from it.

Q: How do you add a remote?
A: git remote add <name> <url> links your local repo to a remote.
Common name is origin.

Q: What does git push do?
A: git push <remote> <branch> uploads local branch commits to the remote repository.
First push often uses git push -u origin <branch>.

Q: What is git pull?
A: git pull fetches changes from remote and merges them into the current branch.
Equivalent to git fetch + git merge.

Q: Difference between git fetch and git pull?
A: git fetch downloads remote changes without merging; git pull fetches and merges.
Fetch is safer for review before integrating.

Q: How do you view remote repositories?
A: git remote -v lists remotes with URLs.
git remote show <name> details branches and tracking.

Q: What is a tracking branch?
A: A local branch tracking a remote branch automatically sets up git pull and git push.
Set with git branch --set-upstream-to.

Q: What does git fetch --all do?
A: It updates tracking branches for all remotes by downloading latest changes.
Does not modify working directory.

Q: How do you rename a remote?
A: git remote rename <old> <new> changes the remote name and updates references.
URLs remain the same.

Q: What is git remote prune?
A: git remote prune <remote> removes local tracking branches for deleted remote branches.
Keeps local repo clean.

Q: What is git cherry-pick?
A: git cherry-pick <commit> applies a specific commit from another branch to the current one.
Creates a new commit with the same changes.

Q: What does git reset do?
A: git reset <commit> moves the branch pointer; with --soft, keeps staging; --mixed unstages; --hard discards changes.
Dangerous for shared history.

Q: Difference between git reset and git revert?
A: Reset rewrites history by moving branch pointer; revert creates a new commit undoing changes.
Revert is safe for shared branches.

Q: What is git revert?
A: git revert <commit> creates a new commit that undoes the specified commit's changes.
Preserves history.

Q: What is the reflog?
A: The reflog (git reflog) records all reference updates, like checkouts and resets, for recovery.
Shows recent history even if rewritten.

Q: How do you recover a lost commit?
A: Use git reflog to find the commit hash, then git cherry-pick or reset to it.
Valid for 30-90 days by default.

Q: What is git bisect?
A: git bisect performs binary search on commit history to find a bug-introducing commit.
Mark good/bad commits to narrow down.

Q: What are Git tags?
A: Tags mark specific commits, like releases, with a name.
Annotated tags (git tag -a) include metadata; push with git push --tags.

Q: How do you create a lightweight tag?
A: git tag <name> <commit> creates a lightweight tag as a branch pointer.
No additional metadata.

Q: What is git stash drop?
A: git stash drop <stash> permanently deletes a stash entry.
Use index like stash@{0}.

Q: What are Git submodules?
A: Submodules allow embedding one repo inside another as a subdirectory.
Cloned with git submodule update --init; track specific commits.

Q: How do you add a submodule?
A: git submodule add <url> <path> clones the repo and adds it to .gitmodules.
Commit the changes afterward.

Q: What is git worktree?
A: git worktree add <path> <branch> creates additional working directories for different branches.
Allows parallel work without checkout.

Q: What are Git hooks?
A: Hooks are scripts run on Git events like commit or push, in .git/hooks/.
Used for validation, like pre-commit linting.

Q: What is an interactive rebase?
A: git rebase -i <commit> allows editing, squashing, or reordering commits interactively.
Opens editor for commands like 'pick' or 'squash'.

Q: How do you amend a commit?
A: git commit --amend replaces the last commit with new staged changes and/or message.
Rewrites history; don't push first.

Q: What is git clean?
A: git clean -f removes untracked files; -d for directories; -n for dry-run.
Careful, as it's permanent.

Q: What are Git aliases?
A: Aliases shorten commands, e.g., git config --global alias.st status makes git st run status.
Customizes workflow.

Q: How do you ignore files in Git?
A: Add patterns to .gitignore file, like *.log or node_modules/.
Commit the file to share ignores.

Q: What is a bare repository?
A: A bare repo (git init --bare) has no working directory, only .git contents.
Used for central servers.

Q: What is git archive?
A: git archive --format=zip HEAD creates a zip/tar of the working tree without .git.
Useful for releases.