Git
28 questions · 5 output questions · 5 challenges · 3 mocks
Git Fundamentals
Initial Git setup
How do you set your name and email in Git, and what are they for?
File states in Git
What states can a file have in Git?
git init and git clone
What is the difference between `git init` and `git clone`?
Git vs GitHub
What is the difference between Git and GitHub?
What is Git
What is Git and what is it for?
The three areas of Git
What are the three areas of Git and how does a change move between them?
Commits & Changes
Undoing changes in Git
How do you undo changes in Git, and how do you choose the right command?
git add and git commit
What do `git add` and `git commit` do, and why are they two separate steps?
git log and history
How do you review a repository's commit history?
git status and git diff
What are `git status` and `git diff` for, and how do they differ?
The .gitignore file
What is `.gitignore` and what is it for?
Branches
Creating and switching branches
How do you create a branch and how do you switch to it?
Merge vs Rebase
What is the difference between merge and rebase, and when should you use each one?
Merge (combining branches)
What is a merge and what is the difference between a fast-forward and a merge commit?
What is a branch
What is a branch in Git and what is it for?
Rebase
What does `git rebase` do and what is it used for?
Resolving merge conflicts
Why do conflicts happen and how do you resolve them?
Remote Work
Clone vs Fork
What's the difference between cloning and forking a repository?
Pull Requests
What is a Pull Request and what is it for?
push, pull, and fetch
What's the difference between `git fetch`, `git pull`, and `git push`?
Tracking branches (upstream)
What is a tracking branch, and what is the `-u` flag for when pushing?
Remotes and origin
What is a remote in Git, and what is `origin`?
Workflows & Best Practices
Good commit messages
What makes a commit message good?
git cherry-pick
What does `git cherry-pick` do and when would you use it?
Git Flow vs GitHub Flow
What is a branching workflow, and how do Git Flow and GitHub Flow differ?
git stash
What is `git stash`, and when would you use it?
reset vs revert vs checkout
What is the difference between `git reset`, `git revert`, and `git checkout`?
Tags and versions
What are tags in Git, and what is the difference between an annotated tag and a lightweight one?
Coding challenges
Command questions
What's the difference between these two commands?
You have a commit made locally and you run one or the other:
# Option A
git reset --soft HEAD~1
# Option B
git reset --hard HEAD~1
Predict: in each case, what happens to the commit and, above all, what happens to your changes?
Ambos deshacen el último commit (mueven la rama a HEAD~1), pero difieren en qué pasa con los cambios:
git reset --soft HEAD~1: deshace el commit y conserva los cambios en el staging area. No se pierde nada; quedan listos para recommitear.git reset --hard HEAD~1: deshace el commit y borra los cambios del staging y del working directory. Se pierde ese trabajo.
reset moves the branch pointer to another commit. What changes is what it does with the content: --soft leaves it in staging, --mixed (the default) leaves it unstaged in the working directory, and --hard removes it. That's why --hard is dangerous: it's the only variant that deletes work. Also, reset rewrites history, so it should only be used on local commits that haven't been published.
What does each one do and how do they differ?
# Option A
git fetch origin
# Option B
git pull origin main
Predict: which of the two modifies your current working branch and which one doesn't?
git fetch origin: descarga los cambios del remoto y actualiza las ramas remotas (origin/main), pero NO modifica tu rama local ni tu working directory.git pull origin main: hacefetchy además integra los cambios en tu rama actual (por defecto con merge), así que sí modifica tu trabajo.
fetch only brings down information and updates the remote references: it's the safe option to review before integrating. pull is roughly fetch + merge: it downloads and integrates in a single step, so it can produce a merge commit or conflicts in your branch. The key difference: fetch only looks, pull looks and applies.
How does the history end up with each one?
You're on the feature branch, which branched off from an old commit of main, and meanwhile main received new commits. You run one or the other:
# Option A
git switch feature
git rebase main
# Option B
git switch main
git merge feature
Predict: how does the history end up in each case? Does either create an extra commit? Does either rewrite history?
git rebase main(desdefeature): reaplica los commits defeatureencima demain, dejando un historial lineal sin merge commit, pero reescribiendo la historia (los commits obtienen hashes nuevos).git merge feature(desdemain): crea un merge commit con dos padres, dejando un historial ramificado que conserva la historia real y no cambia los hashes existentes.
Rebase moves your commits onto a new base, which is why the history ends up straight but the identifiers change; it's only safe on local branches. Merge joins the histories with a special two-parent commit, preserving what actually happened; it's safe on shared branches. The golden rule: don't rebase public history.
What do these commands do and how are they related?
You edited estilos.css but you haven't run git add. You run one or the other:
# Option A (classic command)
git checkout -- estilos.css
# Option B (modern command)
git restore estilos.css
Predict: what happens to your changes in estilos.css? Do they do the same thing?
Ambos comandos hacen lo mismo: descartan los cambios sin commitear de estilos.css, devolviéndolo al estado del último commit. La diferencia es de sintaxis/claridad:
git checkout -- estilos.css: la forma clásica;checkouthacía demasiadas cosas.git restore estilos.css: la forma moderna, específica para restaurar archivos (recomendada).
Since Git 2.23, the old checkout was split into git switch (for changing branches) and git restore (for restoring files), because checkout did so many things that it was confusing. In this case both discard changes irreversibly, so you need to be sure you don't want those changes.
What does each reference show and what does it point to?
# Command 1
git log --oneline
# Command 2
git show HEAD~2
# Command 3
git diff HEAD~2 HEAD
Predict: what does git log --oneline show? Which commit does HEAD~2 refer to? What does the third command compare?
git log --oneline: muestra el historial de commits, uno por línea, con el hash corto y el mensaje, del más nuevo al más viejo.HEAD~2: referencia relativa al commit que está dos posiciones antes del actual (HEADes el actual,HEAD~1su padre,HEAD~2dos atrás).git show HEAD~2muestra ese commit.git diff HEAD~2 HEAD: muestra todos los cambios acumulados entre el commit de hace dos posiciones y el commit actual.
Git lets you refer to commits not only by their hash, but also relatively with ~: HEAD~1, HEAD~2, etc. (HEAD^ is equivalent to HEAD~1). This avoids having to copy long hashes. --oneline compacts the log so you can read it quickly, and relative references combine with commands like show, diff, or reset.