Version Control

Git & Version Control: The Time Machine for Code

Updated June 2026
Git and GitHub Development Concept
Git is the standard software developers use to save and share project code checkpoints worldwide.

Hello, future software creators! Today we are learning about a developer superpower: Git. Without Git, writing code with a team of developers would feel like trying to write a single essay together by emailing draft files back and forth. It would be absolute chaos!

Let's discover what Git is, why everyone uses it, and how it acts as a magical time machine for your code.

The Checkpoint Analogy: Saving Your Code Game

Imagine you are playing a difficult adventure video game. Before entering a dark castle to fight a giant monster, what do you do? You walk up to a save-point and save your game progress! If the monster beats you, you don't start the entire game from level 1. You just restart from your last saved checkpoint.

Writing code is exactly like that. When you write a new feature—for example, adding a dark-mode switch to a website—you might accidentally write a bug that crashes the entire homepage. If you didn't save your progress, you'd have to press undo 500 times or rewrite the whole site.

Git is the ultimate checkpoint system. It monitors your project folder and lets you freeze the code at any point in time. We call these checkpoints Commits. If you break the website, you can tell Git to instantly rewind the folder back to yesterday's commit, and everything is fixed!

The Google Docs Metaphor: Working Together

If you and your classmates write a school report together in a Google Doc, you can both type at the same time. But what if two developers are editing a complex server script? If they work in the same file, they could overwrite each other's code and cause crashes.

Git solves this by giving every developer their own private playground branch. You duplicate the code into a separate Branch, make your changes, and once you test it, you merge it back into the main branch. If two changes clash, Git points it out (a Merge Conflict) and asks you to choose which line is the correct one.

Core Git Terms Explained Simply

Repository (Repo)

This is just a project folder on your computer that has Git enabled inside it. It holds all your code and the history logs.

Commit

A saved snapshot of your code at a specific moment. Each commit gets a unique ID and a short message describing the changes.

Branch

A parallel version of your repository. The default main branch is where the live code lives; custom branches are for safe testing.

GitHub

A cloud library where developers upload their repositories so others can download, collaborate, and review the code online.

11 Everyday Git Commands Every Engineer Needs

To use Git, you open your terminal console inside your project folder and run commands. Here is a complete cheat sheet of the 11 commands you will use daily:

Purpose Git Command Real-World Metaphor & Example
Start Tracking git init Initializes Git inside a folder. Like installing a black box flight recorder in a plane.
Example: git init (Run once at the start of a project).
Download Project git clone <url> Downloads a copy of an existing cloud repository from GitHub to your computer.
Example: git clone https://github.com/user/my-app.git
Check Status git status Asks Git: "What files have I modified since the last checkpoint?"
Example: git status (Shows modified files in red or green).
Stage Changes git add <file> Packs the modified files into a shipping container, ready to be locked.
Example: git add index.html (Or git add . to stage everything).
Create Checkpoint git commit -m "msg" Locks the shipping container and saves it as a checkpoint with a descriptive message.
Example: git commit -m "Feat: Add dark mode toggle button"
Upload to Cloud git push Uploads your local checkpoints from your computer to the repository on GitHub.
Example: git push origin main
Download Updates git pull Fetches and merges the latest updates that other developers pushed to GitHub.
Example: git pull
List/Create Branches git branch Creates a new branch path or lists the existing branches in your repository.
Example: git branch feature-login-page
Switch Workspace git checkout <branch> Jumps your workspace folder from one branch to another.
Example: git checkout feature-login-page
Merge Code git merge <branch> Combines and blends the code checkpoints of another branch into your current branch.
Example: git merge feature-login-page (Run from the main branch).
View History git log Outputs a scrollable history log of every single commit checkpoint created in the project.
Example: git log --oneline (Shows short commit list).

Real-World Scenario: Safe Checkout Page Updates

Suppose you are working on the checkout page of a massive shopping website. The marketing team wants a new discount code field, but the checkout page is incredibly sensitive. If you break it, checkout transactions stop instantly and the company loses crores of rupees!

To implement this safely:

  1. You switch to the master branch and download latest updates: git checkout main; git pull.
  2. You create a secure test branch: git checkout -b feature-discount-code.
  3. You write code, test it locally, stage, and save your checkpoints: git add .; git commit -m "Feat: Add UPI discount code logic".
  4. You push the branch online to GitHub: git push origin feature-discount-code.
  5. You open a Pull Request on GitHub. The senior engineer reviews your changes, runs automated tests, approves it, and clicks Merge. Your code is safely blended into the main site!

Pro-Tip for Git Commits

Always write clean commit messages! Instead of typing messages like git commit -m "stuff done", write clear descriptions like git commit -m "Fix: Resolve card page button layout padding". This makes debugging much easier when you use the history logs later!

Next Steps on Your DevOps Journey

Now that you know how to save and cooperate on your codebase using Git, you are ready to packaging your application files alongside all of their runtime systems using Docker containers!

Test Your Knowledge

Answer these 35 questions to check your understanding of this module. Click on an option to reveal the correct answer instantly.

Question 1 of 35
Which command initializes a new Git repository?
A. git start
B. git init
C. git create
D. git new
Explanation: git init creates a new .git directory.
Question 2 of 35
How do you check the status of files in the working directory?
A. git info
B. git log
C. git status
D. git check
Explanation: git status shows working tree status.
Question 3 of 35
Which command stages changes?
A. git add
B. git stage
C. git commit
D. git push
Explanation: git add moves changes to the staging area.
Question 4 of 35
How do you commit staged changes?
A. git save
B. git commit
C. git push
D. git store
Explanation: git commit records changes to the repository.
Question 5 of 35
Which command lists the commit history?
A. git history
B. git log
C. git past
D. git list
Explanation: git log shows the commit logs.
Question 6 of 35
How do you create a new branch?
A. git checkout -b name
B. git branch -n name
C. git new name
D. git create name
Explanation: git checkout -b creates and switches to a new branch.
Question 7 of 35
Which command merges a branch into the current branch?
A. git join
B. git merge
C. git combine
D. git fuse
Explanation: git merge joins two or more development histories.
Question 8 of 35
What is the command to clone a remote repository?
A. git copy
B. git clone
C. git download
D. git pull
Explanation: git clone copies a repository into a new directory.
Question 9 of 35
How do you fetch and merge changes from the remote?
A. git fetch
B. git pull
C. git push
D. git sync
Explanation: git pull performs a fetch followed by a merge.
Question 10 of 35
What is "HEAD" in Git?
A. The first commit
B. The main branch
C. A pointer to the current commit
D. The remote repository
Explanation: HEAD is a pointer to the current snapshot you are working on.
Question 11 of 35
Which command temporarily saves dirty work without committing?
A. git stash
B. git hide
C. git store
D. git pause
Explanation: git stash saves uncommitted changes for later use.
Question 12 of 35
How do you unstage a file?
A. git remove
B. git reset
C. git delete
D. git undo
Explanation: git reset HEAD unstages a file.
Question 13 of 35
Which command shows the difference between working dir and staging?
A. git show
B. git diff
C. git compare
D. git status
Explanation: git diff shows changes between commits, commit and working tree, etc.
Question 14 of 35
How do you discard local changes to a specific file?
A. git checkout -- file
B. git remove file
C. git drop file
D. git clean file
Explanation: git checkout -- restores the file from the last commit/staging.
Question 15 of 35
What command handles remote URLs?
A. git url
B. git remote
C. git origin
D. git connect
Explanation: git remote manage set of tracked repositories.
Question 16 of 35
What is a "fork"?
A. A new branch
B. A copy of a repo in your own account
C. A git error
D. A merge conflict
Explanation: A fork is a personal copy of another users repository.
Question 17 of 35
Which file tells Git to ignore specific files?
A. .gitconfig
B. .gitignore
C. .gitkeep
D. .githide
Explanation: .gitignore specifies intentionally untracked files to ignore.
Question 18 of 35
How do you view the details of a specific commit?
A. git show
B. git view
C. git log
D. git inspect
Explanation: git show displays details of a commit object.
Question 19 of 35
What does "git rebase" do?
A. Merges two branches
B. Deletes a branch
C. Moves the base of a branch to another commit
D. Creates a backup
Explanation: Rebase reapplies commits on top of another base tip.
Question 20 of 35
How do you delete a local branch?
A. git branch -d
B. git remove -b
C. git delete
D. git erase
Explanation: git branch -d deletes a branch.
Question 21 of 35
Which command tags a specific commit?
A. git label
B. git tag
C. git mark
D. git sign
Explanation: git tag creates a tag object.
Question 22 of 35
How do you push tags to remote?
A. git push --tags
B. git send tags
C. git upload --tags
D. git sync tags
Explanation: git push --tags sends all local tags to the remote.
Question 23 of 35
What is "git blame" used for?
A. Finding errors
B. Showing who last modified each line
C. Deleting bad code
D. Merging conflicts
Explanation: git blame shows what revision and author last modified each line of a file.
Question 24 of 35
What is "origin" in Git?
A. The master branch
B. The local folder
C. The default alias for the remote repository
D. The first commit
Explanation: Origin is the default shorthand name for the remote repository.
Question 25 of 35
How do you revert a commit that has been pushed?
A. git undo
B. git revert
C. git remove
D. git back
Explanation: git revert creates a new commit that undoes the changes of a previous commit.
Question 26 of 35
Which Git command is most useful for recovering a commit or branch that was accidentally deleted?
A. git status
B. git reflog
C. git revert
D. git log
Explanation: git reflog records every reference change (checking out branches, resetting commits, etc.), allowing you to recover lost history.
Question 27 of 35
What is the golden rule of using "git rebase"?
A. Never rebase on a local feature branch.
B. Never rebase public branches that other developers are working on.
C. Always push rebased branches without using --force.
D. Always rebase before resolving merge conflicts.
Explanation: Rebasing rewrites history. Doing it on public branches breaks other developers' local clones and creates severe conflicts.
Question 28 of 35
What is the difference between "git reset --soft" and "git reset --hard"?
A. --soft moves HEAD only (keeps working dir and staging); --hard resets HEAD, staging, and working dir.
B. --soft resets staging only; --hard resets staging and working dir.
C. --soft deletes files from disk; --hard keeps files unstaged.
D. --soft moves staging only; --hard moves HEAD only.
Explanation: --soft leaves your staged index and working directory untouched, only moving HEAD. --hard resets everything, discarding all uncommitted changes.
Question 29 of 35
How do you stash your modifications including untracked files?
A. git stash
B. git stash -u (or --include-untracked)
C. git stash save --all-files
D. git stash --untracked-only
Explanation: By default, git stash only stashes tracked files. Using the -u flag includes untracked files.
Question 30 of 35
Which command is used to configure a graphic diff tool for conflict resolution?
A. git config diff.tool
B. git config merge.tool
C. git config conflict.resolver
D. git config diff.gui
Explanation: git config merge.tool [tool_name] sets up the external merge resolver (like meld, kdiff3, vimdiff).
Question 31 of 35
How do you apply a single specific commit from another branch to your current branch?
A. git merge [commit-hash]
B. git rebase [commit-hash]
C. git cherry-pick [commit-hash]
D. git checkout [commit-hash]
Explanation: git cherry-pick [hash] copies and applies a single specific commit from anywhere in the history to your current branch.
Question 32 of 35
What is the advantage of using "git switch" over "git checkout"?
A. It runs faster on large codebases.
B. It separates branch switching from file modification concerns (checkout is overloaded).
C. It automatically pushes changes to remote.
D. It deletes old local branches dynamically.
Explanation: Modern Git introduced git switch exclusively for changing branches, leaving file restoration tasks to git restore.
Question 33 of 35
Which command deletes untracked files and directories from your local working tree?
A. git clean -n
B. git clean -fd
C. git reset --hard
D. git clean -x
Explanation: git clean -f deletes untracked files, and -d removes untracked directories recursively.
Question 34 of 35
In an interactive rebase (git rebase -i), what does the "squash" command do?
A. Deletes the commit entirely.
B. Merges the commit into the previous one and prompts you to edit the commit message.
C. Temporarily stashes the commit code.
D. Skips resolving merge conflicts automatically.
Explanation: Squash combines the selected commit into its parent commit and lets you combine/edit their commit messages.
Question 35 of 35
Where are local git hooks scripts stored inside a repository?
A. .git/hooks/
B. .github/workflows/
C. .hooks/
D. config/git/hooks/
Explanation: Git hooks are standard shell or executable scripts residing in the hidden .git/hooks/ folder of the local repository.

Real-Time Interview Questions & Answers

1. What is the difference between Git Merge and Git Rebase?

Answer: `git merge` combines branches by creating a new merge commit, preserving the complete commit history. `git rebase` moves the entire feature branch history onto the tip of the target branch, rewriting history to keep it linear.

Example: “I use `git rebase main` on my local feature branch to resolve updates before pushing, keeping a clean linear commits history.”

2. What is the difference between `git reset --hard` and `git revert`?

Answer: `git reset --hard` rewrites history by moving the branch pointer back to a specific commit, deleting all intermediate commits and local modifications. `git revert` creates a new commit that applies the opposite changes of a target commit, preserving history.

Example: “On shared remote branches like `main`, I always use `git revert ` to avoid breaking the repositories for other developers.”

3. How do you resolve a merge conflict in Git?

Answer: I open the conflicted files, look for conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`), edit the code to keep the correct lines, remove the markers, run `git add` to stage the changes, and complete the merge with `git commit`.

Example: “When we had conflicts in our application configs, I used VS Code's merge editor to compare incoming and current changes before committing.”

4. What is Git Stash, and how do you use it?

Answer: `git stash` temporarily shelves local modifications (both staged and unstaged) so you can switch branches. You can restore them later using `git stash pop` or `git stash apply`.

Example: “I run `git stash save 'working on auth API'` to quickly switch to a hotfix branch without losing my current incomplete changes.”

5. What is `git cherry-pick`, and when should you use it?

Answer: `git cherry-pick` applies the changes introduced by a specific existing commit from another branch onto your current branch as a new commit.

Example: “We use `git cherry-pick ` to copy a critical bug fix commit from our development branch directly into the release branch.”

6. What are Git Tags, and when do you use them?

Answer: Tags are reference points pointing to specific commits in history, usually to mark release versions (e.g., v1.0.0). Annotated tags include metadata (author, date, message) and are preferred for release points.

Example: “I run `git tag -a v1.2.0 -m 'Release v1.2.0'` on the main branch to mark our production releases for the CI/CD pipeline.”

7. What git branching strategy do you use in your team?

Answer: We use a GitFlow branching strategy. Developers create `feature/` branches from `develop`, which are merged back via PRs. A `release/` branch is created from `develop` and eventually merged into `main` and `develop` with tags.

Example: “In our team workflow, no direct commits are allowed on `main` or `develop`; everything must go through a code review and Pull Request.”

8. How do you clean up local branches that have already been deleted on the remote repository?

Answer: I run `git fetch` with the `--prune` (or `-p`) flag, which deletes remote-tracking branch references that no longer exist on the remote server.

Example: “I run `git fetch -p` weekly to clean up my local references and keep my branch lists organized.”

9. What is the difference between `git pull` and `git fetch`?

Answer: `git fetch` only downloads remote changes and metadata to local remote-tracking branches but does not merge them. `git pull` runs both `git fetch` and then automatically merges the changes into your active branch.

Example: “I run `git fetch` to review what changes my team members pushed before merging them into my workspace manually.”

10. How do you amend the commit message of the last commit?

Answer: I run `git commit --amend -m 'New commit message'`. This updates the latest commit instead of creating a new one, rewriting the history of that commit.

Example: “If I notice a typo in my commit message before pushing, I run `git commit --amend` to fix it locally.”

11. How do you revert a commit that has already been pushed to a remote branch without losing subsequent commits?

Answer: I run `git revert `, which generates a new commit with the exact opposite changes of the target commit, leaving other commits intact.

Example: “I ran `git revert 4f5a3e1` to roll back an incorrect API endpoint configuration while leaving subsequent frontend updates untouched.”

12. What is a Git Hook, and how can it be used to improve code quality?

Answer: Git Hooks are scripts that run automatically at key points in the Git workflow (like pre-commit, pre-push). They are stored in `.git/hooks` and are used to enforce code standards.

Example: “We use a pre-commit hook containing Husky to automatically run ESLint and Prettier, preventing bad styling from being committed.”

13. How do you find the commit that introduced a bug in the codebase?

Answer: I use `git bisect`, which performs a binary search through the commit history between a known good commit and a known bad commit.

Example: “I run `git bisect start`, specify `git bisect bad` (current broken commit) and `git bisect good v1.0` (last working release) to locate the bug quickly.”

14. What do you do if you commit a secret (like an API key) to a Git repository?

Answer: First, I immediately rotate the leaked key. Second, I use tools like BFG Repo-Cleaner or `git-filter-repo` to delete the secret from all commit history, then force push.

Example: “When a secret was committed, we revoked the key immediately and used `git-filter-repo` to remove it from all git branches.”

15. How do you rename a local branch and push it to the remote?

Answer: I rename the local branch with `git branch -m `, delete the old remote branch, and push the new branch as upstream.

Example: “I ran `git branch -m feature-auth` then `git push origin -u feature-auth` to update our branch naming scheme.”
Live Sandbox

Don't Just Read. Code Live!

Practice what you just learned in our secure, zero-setup interactive labs. Boot up Linux containers, orchestrate AWS infrastructure, and run Docker right in your browser.

100% Free & Interactive for Growth School Community No Setup Required Real-time Terminal Feedback
Start Live Sandbox
ubuntu@growthschool:~

docker run -d -p 80:80 nginx

Unable to find image 'nginx:latest' locally...

latest: Pulling from library/nginx

Digest: sha256:4c087b3289aa6b185...

Status: Downloaded newer image for nginx:latest

Container running at http://localhost:80

_