Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 77 ยท DevOps, Tools and System Design

Git and Linux Interview Questions

Prepare Git and Linux interview questions: merge vs rebase, reset vs revert, cherry-pick, file permissions, processes and pipes, with a runnable commit-graph model.

Git and the Linux command line are the daily tools of almost every developer, so interviewers use them to see whether you understand what is going on underneath the commands. "What is the difference between merge and rebase?" is rarely about syntax. It is about whether you know that a commit is a snapshot with parents, and that a branch is only a movable label.

The real git and shell commands in this lesson are read-only (the interview runner has no repository or terminal). To make the ideas concrete, the runnable examples are stand-ins: a tiny JavaScript model of a commit graph, a permission calculator and a pipe built from functions. They are not Git, but they follow the same rules. For hands-on practice with the real tools, see the Git tutorial and the Linux tutorial.

What Git really stores

Commit A snapshot plus parent links.
Branch A label pointing at one commit.
HEAD Where you are right now.

Most Git questions are about moving labels around.

1 / 2

What is the difference between git merge and git rebase?

Short answer: Both combine work from two branches. Merge keeps history exactly as it happened and ties the branches together with a merge commit that has two parents. Rebase takes your commits and replays them on top of another branch, creating new commits with new IDs, so history stays a straight line.

Terminal
# Read-only
git switch feature
git merge main        # creates a merge commit on feature

git switch feature
git rebase main       # replays feature's commits on top of main

Trade-offs worth saying out loud:

  • Merge never rewrites history, so it is safe on shared branches. The graph can look busy.
  • Rebase gives a clean linear history, but it rewrites commits. Never rebase commits other people already pulled; if you must update a pushed branch, coordinate and use --force-with-lease.
  • A common team rule: rebase your own local feature branch onto main before opening a pull request, then merge (or squash-merge) into main.
  • A fast-forward merge happens when the target has no new commits: Git just moves the label, with no merge commit.

The model below shows commits as nodes with parent links. Watch what happens to the commit IDs.

JS A commit-graph model: merge vs rebase

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the output, the merge keeps F1 and F2 and adds a commit with two parents. The rebase produces F1' and F2': same changes, but different commits, because a commit's identity includes its parent.

git reset vs git revert: how do you undo a commit?

Short answer: reset moves the branch label backwards (rewriting history); revert adds a new commit that applies the opposite change (preserving history). Use revert for commits already pushed to a shared branch, reset for local cleanup.

git reset has three modes, named after what they touch:

Mode Moves branch Resets staging area Resets working files
--soft yes no no
--mixed (default) yes yes no
--hard yes yes yes, changes are lost
Terminal
# Read-only
git reset --soft HEAD~1     # undo the commit, keep changes staged
git reset --hard HEAD~1     # undo the commit AND discard the changes
git revert 3f2a1bc          # new commit that undoes 3f2a1bc
git reflog                  # the safety net: lists where HEAD has been

A lost commit after reset --hard is usually still recoverable with git reflog for a few weeks, because Git does not delete unreferenced commits right away.

git cherry-pick <commit> copies one commit onto your current branch as a new commit (new ID). It is the right tool for backporting a bug fix to a release branch. Overusing it creates duplicate commits and confusing merges later.

JS reset, revert and cherry-pick on the model

Press Run (or Ctrl + Enter) to see the console output here.

No output.

How do Linux file permissions work?

Every file has an owner, a group and three permission sets: user (owner), group and others. Each set has read (r = 4), write (w = 2) and execute (x = 1). The numbers add up, so 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--. So chmod 754 script.sh means owner rwx, group r-x, others r--.

Terminal
# Read-only
ls -l script.sh
# -rwxr-xr-- 1 maria dev 120 Nov  6 10:00 script.sh
chmod 754 script.sh      # numeric form
chmod u+x,g-w script.sh  # symbolic form
chown maria:dev script.sh

For a directory, r means you can list names, w means create or delete entries inside, and x means you can enter it (cd and access files). A frequent follow-up is the special bits: setuid runs a program as its owner, setgid keeps group inheritance in a directory, and the sticky bit (/tmp) lets only a file's owner delete it.

JS Permission calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is a process, and how do you inspect and stop one?

A process is a running program with its own PID, memory and open files. A thread lives inside a process and shares its memory. Linux starts every process from another (its parent); PID 1 is the init system.

Terminal
# Read-only
ps aux | grep nginx      # list processes, filter
top                      # live view (htop is friendlier)
kill 4242                # SIGTERM: ask politely to exit
kill -9 4242             # SIGKILL: cannot be caught, no cleanup
command &                # run in the background
jobs; fg %1              # shell job control

Know the signals: SIGTERM (15) lets a program clean up, SIGINT (2) is Ctrl+C, SIGKILL (9) is the last resort. A zombie is a finished process whose parent has not yet collected its exit status; an orphan is a running process whose parent died (it is adopted by PID 1).

What are pipes and redirection?

Every process has three standard streams: stdin (0), stdout (1) and stderr (2). A pipe | connects one program's stdout to the next program's stdin, so small tools combine into powerful ones. Redirection sends streams to files: > overwrites, >> appends, 2> redirects errors, 2>&1 merges errors into stdout.

Terminal
# Read-only: the ten most common words in a file
cat book.txt | tr 'A-Z' 'a-z' | tr -cs 'a-z' '\n' | sort | uniq -c | sort -rn | head -10

make build > build.log 2>&1     # everything to a file
grep -r "TODO" src/ 2>/dev/null  # hide permission errors

The stand-in below builds the same idea with functions: each stage takes the previous stage's lines and returns new lines.

JS A pipe is function composition

Press Run (or Ctrl + Enter) to see the console output here.

No output.

uniq only collapses adjacent duplicates, which is why sort comes first. Interviewers like asking that.

Quick answers for the follow-ups

  • git fetch vs git pull: fetch downloads new commits and moves remote-tracking branches only; pull is fetch plus a merge (or rebase with --rebase).
  • git stash: shelves uncommitted changes so you can switch branches.
  • Merge conflict: Git cannot combine two edits to the same lines; you edit the file, git add, then continue the merge or rebase.
  • git bisect: binary search through history to find the commit that introduced a bug.
  • Hard link vs symlink: a hard link is another name for the same inode; a symlink is a small file containing a path and can dangle.
  • grep, find, awk, sed: search text, find files, process columns, edit streams.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should you avoid rebasing commits that teammates have already pulled?

    Rebase replays commits as new ones. Teammates who built on the old commits end up with diverging history, and fixing that is painful.
  2. 2. You pushed a bad commit to a shared branch. What is the safest way to undo it?

    Revert preserves history, so everyone who already has the bad commit can simply pull the fix. Reset plus force-push rewrites shared history.
  3. 3. What does chmod 640 file.txt give to the group?

    6 = rw- for the owner, 4 = r-- for the group, 0 = --- for others.
  4. 4. In 'sort | uniq -c', why is sort needed first?

    uniq compares each line only with the previous one, so equal lines must be adjacent. Sorting groups them.
  5. 5. You ran git reset --hard and lost a commit. What is the first thing to try?

    Git keeps unreferenced commits for a while and the reflog records where HEAD pointed, so the work is usually recoverable (uncommitted changes are not).

Summary

  • A commit is a snapshot with parent links; a branch is a movable label; HEAD says where you are.
  • Merge preserves history with a two-parent commit; rebase replays commits as new ones for a linear history. Do not rebase shared commits.
  • reset moves the label (soft, mixed, hard); revert adds an undoing commit; reflog rescues mistakes; cherry-pick copies one commit.
  • Permissions are three rwx triplets (owner, group, others) written as octal digits 4, 2, 1.
  • Processes have PIDs and signals (SIGTERM before SIGKILL); pipes connect stdout to stdin and redirection sends streams to files.

Next: Docker, CI/CD and Cloud Questions.