Docsity

Git & GitHub Professional Guide: Core Workflows with Interview Questions, Summaries of Computer Engineering and Programming

Partial preview of the text

Download Git & GitHub Professional Guide: Core Workflows with Interview Questions and more Summaries Computer Engineering and Programming in PDF only on Docsity!

A W O R K I N G G U I D E

Git & GitHub

Version control for developers, explained from first principles

One clear model^  Branch with confidence^ ļƒ‚ Collaborate on GitHub

ļ„” Commit^  Inspect^  Branch^ Merge

ļ‡š Undo^ ļƒ‚ Remotes^ ļƒ€ Collaborate^  Interview Q&A

9 chapters Ā· modern commands Ā· diagrams Ā· interview preparation

ļ‡š

CHAPTER 01

Why Version Control? The real cost of 'final_v2_FINAL.zip' — and how a version-control system removes it for good.

Every project accumulates history: drafts, fixes, experiments, and the occasional mistake.

ļ‡š A version-control system records that history for you, so you can see exactly what

changed, when, and why — and return to any earlier state in seconds. Git is the tool the

whole industry has standardised on.

 THE CORE IDEA

Version control saves a snapshot of your whole project each time you ask.  Instead of keeping report_v2_final_FINAL.zip, you keep one folder and an unlimited, labelled timeline of every save. You can compare snapshots, undo safely, and see who changed what — all without losing a thing.

ļ‡š One folder, full history  Never lose work  See every change  Work fearlessly

ļƒ€ Team-ready

100M+ developers use Git worldwide

~ year Git was created

# most-used VCS by far

1.2 (^) How Git thinks: snapshots, not files ļ„”

Many older tools stored a list of per-file changes. Git instead stores a snapshot of your entire project at each save (a commit ). Unchanged files are simply referenced again, so snapshots are small and fast. 

(^1) Edit change files in your working folder

(^2) Stage pick which changes go in the next save

(^3) Commit record a labelled snapshot forever

(^4) Repeat build an unbroken timeline

The everyday loop you'll repeat thousands of times.

ļ„© GOOD TO KNOW

Git vs GitHub. (^) ļ„© Git is the tool that runs on your computer and stores history. GitHub is a website that hosts Git repositories online so teams can share them. You can use Git alone; GitHub adds collaboration.

1.3 (^) Chapter recap 

 KEY TAKEAWAYS

ļ€Œ (^) Version control records a full timeline of your project you can revisit anytime. ļ‡š ļ€Œ (^) Git stores snapshots (commits), not fragile per-file diffs. ļ„” ļ€Œ You can undo safely , compare any two points, and see who changed what. (^)  ļ€Œ Git = the tool on your machine; GitHub = a site that hosts Git repos. (^) ļƒ‚ ļ€Œ The daily loop is edit → stage → commit , repeated. (^) 

 ON THE JOB

On the job  , version control is non-negotiable — it underpins code review, automated testing, and safe releases. Every serious team expects fluency with Git, which is exactly why it appears in almost every developer interview. ļ‚‘



CHAPTER 02

Install & Configure Get Git on your machine, set your identity, and initialise a repository in any folder.

Setup takes about two minutes and you only do it once per machine.  You confirm Git

is installed, tell Git who you are so it can stamp every commit, and then turn any folder

into a repository with a single command. 

 THREE ONE-TIME STEPS

Three one-time steps. (^)  1) Confirm the install with git --version. 2) Set your identity with git config --global user.name and git config --global user.email. 3) Inside a project folder, run git init to start tracking your work. 

ļ€Œ git --version  set user.name  set user.email  git init  the .git folder

2.2 (^) Chapter recap 

 KEY TAKEAWAYS

ļ€Œ (^) git --version confirms Git is installed and shows the version. ļ€Œ ļ€Œ (^) git config --global sets your name and email once for every project.  ļ€Œ git init creates a hidden .git folder that stores your history. (^)  ļ€Œ Run git init once per project — not every session. (^)  ļ€Œ Identity is stamped on every commit permanently — set it correctly first. (^) 

 ON THE JOB

On the job ļƒ€, teams standardise this configuration so everyone's commits look consistent and are correctly attributed. Because your identity becomes part of the permanent history (^)  , getting the email right the first time avoids messy rewrites later. 

ļ„”

CHAPTER 03

The Commit Model The three areas every change moves through — working tree, index, and repository.

A commit is a labelled snapshot of your project. ļ„” Before a change becomes a commit

it passes through three areas Git keeps track of. Understanding these three areas is the

single most useful mental model in all of Git. 

 THE THREE AREAS

Working tree → index (staging) → repository. (^)  You edit files in the working tree, stage the ones you want with git add, then commit them into the repository with git commit. Staging lets you save a clean, deliberate set of changes — not just everything at once.

 Working tree  git add  Staging / index ļ„” git commit  Repository

 Working directory files you edit

 Staging area changes lined up

 Repository saved snapshots

git add

git commit

Every change moves left to right: edit, stage, commit.

3.3 (^) Chapter recap 

 KEY TAKEAWAYS

ļ€Œ (^) Changes flow working tree → staging → repository.  ļ€Œ (^) git add stages; git commit -m records a labelled snapshot. ļ„” ļ€Œ Staging lets you commit a deliberate subset of your changes. (^)  ļ€Œ Write commit messages in the imperative : "Add", "Fix", "Update". (^)  ļ€Œ .gitignore keeps junk and secrets out of history. (^) 

 ON THE JOB

On the job  , reviewers read your commits one at a time, so small focused commits with clear messages make you look professional and make bugs far easier to trace later with tools like git blame and git bisect. 



CHAPTER 04

Inspecting History Read a project's story with status, diff, and log — the three commands you'll run daily.

Git can always tell you the story of your project — what has changed, what is saved, and

what happened before.  Three commands cover almost everything you need day to

day, and you'll run them constantly.

 THREE COMMANDS YOU'LL RUN DAILY

git status shows what's changed and staged right now ; git diff shows the exact line-by-line edits; git log shows the history of past commits. (^)  Add git log --oneline for a tidy one-line-per-commit summary.

 git status ļ…› git diff ļ‡š git log  --oneline  read the story

4.3 (^) Chapter recap 

 KEY TAKEAWAYS

ļ€Œ (^) git status = what's changed and staged right now.  ļ€Œ (^) git diff = the exact edits before staging; --staged = what's about to commit. ļ…› ļ€Œ git log = your history ; --oneline and --graph make it readable. (^) ļ‡š ļ€Œ Every commit has a unique hash ; HEAD marks your current position. (^)  ļ€Œ Check git status reflexively — it's your safety net. (^) 

 ON THE JOB

On the job  , engineers run git status before every add or commit — it's muscle memory that prevents mistakes. Reading git diff before committing catches typos, stray debug lines, and secrets before they enter history. 



CHAPTER 05

Branching Isolate work on a named line of development without disturbing the mainline.

A branch is a separate line of development.  You split off from the mainline, build a

feature or try an idea in isolation, and the original stays untouched and shippable the

whole time. Branching is cheap and instant in Git — it's the feature that changes how you

work.

 THE CORE IDEA

A branch is just a movable pointer to a commit.  Creating one doesn't copy your files — Git only writes a tiny reference. That's why branching is instant. git switch -c feature creates a branch and moves onto it; git switch main takes you back.

 (^) Isolate your work  (^) Instant & cheap  (^) main stays safe ļ„” (^) switch -c

(^) Many in parallel

5.2 (^) switch and restore (the modern commands) ļ„”

Older tutorials use git checkout for everything. Modern Git splits that overloaded command into two clearer ones — prefer these: 

Task Modern Old

Move to a branch git switch git checkout

Create + move git switch -c git checkout -b

Discard file edits git restore git checkout --

switch changes branches; restore touches files. Two verbs, two jobs.

ļ„  TRY IT YOURSELF

Try it: git switch -c experiment, make a change, then git switch main. Your experiment's commits stay safely on the other branch. (^) ļ€Œ

5.3 (^) Chapter recap 

 KEY TAKEAWAYS

ļ€Œ (^) A branch is a lightweight movable pointer , not a copy of your files.  ļ€Œ (^) git switch -c name creates a branch and moves onto it. ļ„” ļ€Œ (^) git switch main returns you to the mainline.  ļ€Œ (^) git branch lists branches; the * marks where you are.  ļ€Œ Prefer switch/restore over the older, overloaded checkout. (^) 

 ON THE JOB

On the job  , teams keep main always-releasable and do every piece of work on a short-lived branch — one branch per feature or fix. This keeps changes small, reviewable, and easy to abandon if an idea doesn't pan out. (^) 

CHAPTER 06

Merging & Conflicts Integrate a branch back into main — and resolve the overlap when two edits collide.

When a branch is ready, you merge it back into main. Most merges are automatic.

Occasionally two branches change the same lines — that's a conflict , and Git pauses to let

you choose the right result. Conflicts feel scary at first, but they're routine and

completely recoverable.

 THE CORE IDEA

Merging combines the history of two branches into one.  Switch to the branch you want to merge into (usually main), then run git merge feature. If the changes don't overlap, Git finishes instantly. If they do, Git marks the clash and waits for you.

(^) git merge  (^) Merge into main  (^) Most are automatic  (^) Conflicts are normal

ļ€Œ (^) You choose the result

6.2 (^) Resolving a conflict, calmly ļ‚­

If both branches changed the same lines, Git writes both versions into the file between marker lines and stops. Your job: edit the file to the version you want, delete the markers, then stage and commit. ļ‚­

the conflict markers Git inserts

<<<<<<< HEAD price = 100 # the version on main ======= price = 120 # the version from feature >>>>>>> feature-login

1 Open edit the marked file 

2 Choose keep the correct lines, delete markers

3 Stage git add the resolved file 

4 Commit git commit finishes the merge

Four steps — the same every time.

6.3 (^) Chapter recap 

 KEY TAKEAWAYS

ļ€Œ (^) Merge into the branch you're on: switch to main, then git merge feature. ļ€Œ (^) Most merges complete automatically with no input from you.  ļ€Œ A conflict only happens when the same lines changed on both sides. (^)  ļ€Œ Resolve by editing the file , removing the <<<< ==== >>>> markers. (^) ļ‚­ ļ€Œ Then add and commit to finish — nothing is lost while you decide. (^) ļ€Œ

 ON THE JOB

On the job  , small, frequent merges beat giant ones — the less a branch drifts from main, the smaller and rarer conflicts become. When a conflict does appear, git status always tells you exactly which files still need attention. (^) 

ļ‡š

CHAPTER 07

Undoing Changes restore, reset, and revert: the precise tool for each kind of 'take it back'.

Everyone makes mistakes — Git's real gift is that almost all of them are reversible. ļ‡š

The trick is knowing which undo to reach for. Three commands cover nearly every case:

restore for uncommitted edits, reset for moving your branch pointer, and revert for

safely undoing a public commit.

 THE CORE IDEA

Match the tool to the situation.  git restore throws away uncommitted edits. git reset moves the branch to an earlier commit (local clean-up). git revert makes a new commit that cancels an old one — the safe choice once work is shared.

 (^) restore = file edits ļ‡š (^) reset = move pointer  (^) revert = safe undo ļ€Œ (^) Pick the right one

 (^) Almost all reversible