Git Basics Course

52

questions

Git Basics

How does a distributed VCS differ from a centralized one?

In a centralized VCS (SVN), history is stored on a single server: without a connection to it, you can't commit or view history. In a distributed VCS (Git), every clone is a full copy of the repository with its entire history. That's why most operations are local and fast, and losing the server doesn't mean losing the history.

How is Git different from GitHub?

Git is a version control system that runs locally on your computer. GitHub is a cloud service that hosts Git repositories and adds team tools: pull requests, code review, issues, GitHub Actions. Git works without GitHub; alternatives to GitHub include GitLab and Bitbucket.

What does Git store: snapshots or diffs?

Snapshots. Every commit is a snapshot of the state of all project files; unchanged files aren't copied but point to the already stored version. Git computes diffs on the fly when you ask for them. Delta compression in pack files is just an on-disk storage detail, not the data model.

What states can a file have in Git?

Untracked — Git doesn't track the file yet. A tracked file can be unmodified (matches the last commit), modified (changed but not added) or staged (added to the staging area). After git commit, staged changes go into the repository and the file is unmodified again.

What's the difference between git init and git clone?

git init creates a new empty repository in the current folder. git clone <url> copies an existing repository with its entire history and branches and sets up the origin remote right away.

Where is Git configuration stored?

At three levels: --system — for all users on the machine (/etc/gitconfig), --global — for your user (~/.gitconfig), --local — for one repository (.git/config). The closer level overrides the farther one: local → global → system. To see everything with its source: git config --list --show-origin.

Why do you need the staging area?

To build the next commit deliberately: add only part of your changes (even part of a file with git add -p) and leave the rest for later. That keeps commits small and logical.

Why does a WordPress developer need Git if wordpress.org uses SVN?

SVN on wordpress.org is only the release channel for a plugin. The actual development, team work, code review and CI happen in Git (mostly on GitHub), and deploying to SVN is automated, for example with GitHub Actions.

Daily Workflow

How do you find who changed a line and when?

git blame file.php shows, for every line, the commit, author and date of its last change; -L 10,20 limits the range. Then git show <hash> opens the whole commit with context. To find the commit that added or removed specific text — git log -S "text".

How do you unstage a file without losing changes?

git restore --staged file.php (in older versions — git reset HEAD file.php). The file goes back to modified, and the changes in the working directory stay.

What do you do if a secret has already been committed?

First, treat the secret as compromised and rotate it (password, token, key): it stays in the history and in every clone. Then stop tracking the file and add it to .gitignore. History can be cleaned (git filter-repo, BFG), but that rewrites it for the whole team — rotating the secret matters more.

What does git add -p do?

It shows changes in pieces (hunks) and asks for each one whether to stage it: y — yes, n — no, s — split into smaller ones. That way you can make several logical commits from one file and avoid committing stray debug code.

What makes a good commit message?

A short subject line of up to ~50 characters in the imperative mood that says what the commit does: Fix price rounding in cart, not fix or changes. If needed, a blank line and a body: why the change was made and what its consequences are. One commit — one logical change, and the message comes out precise.

What's the difference between git diff and git diff –staged?

git diff shows changes in the working directory that aren't staged yet. git diff --staged (synonym --cached) shows what's already staged and will go into the next commit. Before committing, --staged is the one to check.

When shouldn't you use –amend?

When the commit has already been pushed to a shared branch. Amend creates a new commit with a different hash: the history on the remote and on your teammates' machines diverges, and pushing requires force. Amend is only for local commits or your own branch nobody else works on.

Why is a file listed in .gitignore still tracked?

.gitignore only affects untracked files. If the file was already committed, Git keeps tracking it. You need to remove it from the index while keeping it on disk: git rm --cached file, and commit that change.

Branching

How do you name branches?

By type and task: feature/cart-update, fix/price-rounding, hotfix/1.2.1, often with a ticket number — feature/123-cart-update. The prefix is visible right in the branch list and can be used in CI and branch protection rules.

How do you resolve conflicts?

git status shows the conflicted files. In each one I work through the <<<<<<<, =======, >>>>>>> markers: what each branch changed and why, and keep the right version — often a combination of both. I remove the markers, run the tests, then git add and git commit. For complex cases — the merge tool in the IDE.

How do you undo a merge that went wrong?

While the merge isn't finished — git merge --abort restores the starting state. If the merge commit is already created but not pushed — git reset --hard ORIG_HEAD. If it's already pushed — git revert -m 1 <merge-hash>.

How is git switch different from git checkout?

git checkout does too much: it both switches branches and restores files, so it's easy to make a mistake. Git 2.23 split it into git switch (branches only) and git restore (files only). Same result, unambiguous commands.

What is a detached HEAD?

A state where HEAD points to a specific commit rather than a branch — for example after git checkout <hash> or checking out a tag. New commits in this state don't belong to any branch and are easy to "lose" after switching. To keep the work — git switch -c <name>.

What is a Git branch technically?

A file with a commit hash in .git/refs/heads/ — a lightweight movable pointer. After a new commit, Git simply writes that commit's hash into it. That's why creating a branch is instant and copies nothing.

What's the difference between fast-forward and a merge commit?

Fast-forward is possible when the target branch has no new commits: Git just moves the pointer forward, the history is linear, no new commit. If the branches have diverged, Git creates a merge commit with two parents that joins both histories.

When should you use –no-ff?

When it matters to see in history that a set of commits was a separate feature: the merge commit groups them and lets you revert the whole feature with a single git revert -m 1 <hash>. Feature branches are often merged into main or develop this way.

Working with Remotes

How do you version releases?

With SemVer: vMAJOR.MINOR.PATCH. MAJOR — breaking changes, MINOR — new functionality without breaking, PATCH — fixes. I mark a release with an annotated tag on a commit in main, and the tag triggers the deploy in CI.

How is origin/main different from main?

main is your local branch that you commit to. origin/main is a remote-tracking branch: a local copy of the server's main as of the last fetch. You don't change it directly; it's updated by fetch and pull.

What does the -u flag do?

git push -u origin <branch> (--set-upstream) remembers the link between the local and the remote branch. After that, git push, git pull and git status (ahead / behind) work without arguments. To check the links — git branch -vv.

What is origin?

The default name git clone gives to the repository you cloned from. It's just a name for a URL: you can rename it or add other remotes, for example upstream for the original repository of a fork.

What's the difference between a lightweight and an annotated tag?

A lightweight tag is just a name pointing to a commit. An annotated tag (git tag -a) is a separate object with an author, date and message, and it can be signed. Releases use annotated tags: git describe and most tools rely on them.

What's the difference between fetch and pull?

git fetch only downloads new commits and updates origin/*; your branches stay as they are — you can look at what arrived first. git pull = fetch + integrating into the current branch (merge, or rebase with --rebase).

Why is –force-with-lease safer than –force?

--force overwrites the remote branch no matter what, even if someone else's commits have appeared there. --force-with-lease first checks that the remote branch is where you last saw it and refuses if someone pushed in the meantime.

Team Workflows

Describe GitHub Flow.

main is always deployable. For each task — a short branch from main, regular commits and pushes, a Pull Request with review and green CI, merge into main and deploy. The branch is deleted after the merge.

How is Git Flow different from GitHub Flow?

GitHub Flow has one permanent branch, main, and short feature branches — built for continuous deploy. Git Flow has two permanent branches (main for releases, develop for integration) plus release/* and hotfix/* — built for planned, versioned releases. It's more complex but gives control over what goes into a release.

What do you check during code review?

That the change does what it claims and doesn't break anything nearby; readability and standards (WPCS); security — sanitizing, escaping, nonces, capability checks; query performance; tests. I write comments concretely, with a suggestion, and mark nitpicks as optional.

When should you use squash merge?

When the branch's intermediate commits ("fix typo", "wip") have no value for history: squash turns the PR into one meaningful commit in main. Not when the PR contains several separate logical changes that are useful to see and revert individually.

Which model would you pick for an online store with frequent releases, and why?

GitHub Flow: frequent releases need a short path from task to production, and develop plus release branches only add merges and delays. Risks are covered by CI, review, feature flags and quick rollback.

Why must main always be deployable?

Because it's deployed at any moment, and new branches and hotfixes start from it. If main is broken, the whole team is blocked. So only verified code gets into main — through PR, review and CI.

Rewriting History

How do you recover a commit after git reset –hard?

git reflog shows every move of HEAD with hashes, even for commits no longer in the branch. I find the right entry and go back: git reset --hard <hash>, or create a branch git branch rescue <hash>. The reflog is local and kept for about 90 days by default.

How do you roll back a bad release in main?

git revert the problematic commit (for a merge commit — git revert -m 1 <hash>), push, and ship a new release. In parallel — a quick deploy rollback to the previous tag. No reset and force push on main.

What is cherry-pick for?

To move one specific commit into another branch without merging the whole branch — for example a hotfix from main into a release branch. It creates a new commit with the same changes and a different hash. For many commits, merge or rebase is better.

What's the difference between merge and rebase?

Merge joins branches with a merge commit and keeps history as it was. Rebase moves your commits on top of another branch, creating new commits with new hashes — history becomes linear. The resulting code is the same; the difference is the history.

What's the difference between reset and revert?

git reset moves the branch back and removes commits from history — it rewrites it, for local changes. git revert creates a new commit that undoes an old one; history isn't rewritten, so it's safe for shared branches.

When do you use git stash?

When I need to switch to another task urgently and the current changes aren't ready to commit: git stash push -m "description", switch, come back, git stash pop. Also before pull if local changes get in the way.

When must you not rebase?

On branches others work on, and for commits already in a shared branch (main, develop). Rebase changes hashes, and your teammates' history diverges. Rebase is for your own branch before merging.

Which operations must not be done on shared branches?

Anything that rewrites history: rebase, reset of pushed commits, commit --amend, push --force. On shared branches fix things with git revert and new commits.

CI/CD on GitHub

How do you protect main from direct pushes?

A branch protection rule (or ruleset) for main: Require a pull request before merging, a required approval, Require status checks to pass, no force pushes. If needed, the rule applies to admins too.

How do you roll back a bad release?

Quickly — by redeploying the previous tag: re-running the workflow for the old release or switching to the previous release on the server. Then the fix: git revert in main and a new patch release.

How do you store deploy keys?

In GitHub Secrets — at the repository or Environment level (production with its own secrets and rules). In the workflow — via ${{ secrets.NAME }}; values are masked in logs. Never in code or in a committed .env; for deploys — a separate SSH key with minimal permissions.

How is your deploy set up?

A merge into main deploys to staging automatically. An annotated v* tag starts a workflow with environment: production that waits for manual approval, builds the artifact and ships it to the server. After the deploy — a smoke test.

What does a GitHub Actions workflow consist of?

A YAML file in .github/workflows/ with triggers (on) and one or more jobs. Each job runs on its own runner (runs-on) and consists of steps: commands (run) or ready-made actions (uses). Jobs run in parallel by default; dependencies are set with needs.

Which checks do you run on PRs?

For a WordPress project: PHPCS with WordPress Coding Standards, PHPUnit, ESLint and Stylelint for the frontend, the build (npm run build), and PHPStan and multiple PHP versions when needed. All of them are required status checks.

Which triggers have you used?

pull_request — checks on every PR; push to main — deploy to staging; push of v* tags — release; workflow_dispatch — manual run; schedule — nightly jobs on cron.