Version control

Every change is kept, with who, when and why

WITHOUT VERSION CONTROLreport.docxreport-final.docxreport-final-v2.docxreport-final-v2-REALLY.docxwhich is current? who changed what?WITH VERSION CONTROL: ONE FILE AND ITS HISTORYa1Ana: first draftb2Raj: add the costsc3Ana: fix the totalsevery change: who, when, what and whyVersion control keeps every change to a project, with who made it, when and why.You can see what changed, go back to any earlier state, and work at the same time as otherswithout overwriting them. Git is the version control system nearly every team uses.

Version control records each change, so you can see it, undo it and share it.

Version control · Going deeper

What version control gives you

What version control gives youHistoryEvery version of every file, and the reason for each changeUndoReturn one file, or the whole project, to any earlier pointCollaborationMany people change the same project at once and combine the workBranchingTry an idea on a separate line without disturbing the main oneTraceabilityEvery line can be traced to an author, a date and a messageBackupEvery copy of a Git repository holds the full historyTwo designsCentralised (Subversion)One server holds the history; you need it for almost everythingDistributed (Git)Every clone holds the whole history, so you can commit offlineGit is a distributed version control system: no single copy is special.

History, undo, collaboration and branching. Git is distributed.

Git and GitHub

Git is the tool. GitHub is where you share it

GIT: A TOOL ON YOUR MACHINEcommitsbranchesmergesthe full historyworks with no network at allGITHUB: A SERVICE THAT HOSTS GIT REPOSITORIESpull requestsissuesActionsaccess controladds collaboration around the codepushpullGit is the tool that records history. GitHub is a place to keep it and work on it together.Git is free, open source software that runs on your own machine. GitHub is a company's servicebuilt around Git: it stores a copy of your repository and adds pull requests, issues, automationand permissions. You can use Git without GitHub, but not GitHub without Git.

Git records history on your machine. GitHub hosts it and adds collaboration.

Git and GitHub · Going deeper

Git and GitHub side by side

GitGitHubWhat it isversion control softwarea hosting and collaboration platformWhere it runson your own machinein the cloud, in a browserMade byan open source project (2005)GitHub, owned by MicrosoftYou use it bythe git command, or a GUIthe website, GitHub Desktop, Mobile, the gh CLIIt addscommits, branches, merges, tagspull requests, issues, Actions, Pages, securityOthers like itMercurial, SubversionGitLab, BitbucketA pull request, an issue, a fork and a star are GitHub ideas. Git has none of them.A commit, a branch, a merge, a tag and a clone are Git ideas, and work the same on any host.

Pull requests, issues, forks and stars are GitHub ideas, not Git.

Check yourself

Your laptopa cloned repository,Git installed✗ no networkGitHubunreachable

With the connection down, which of these still works?

Repositories, commits, branches

A branch is a name that points at a commit

A REPOSITORY: THE PROJECT AND ITS WHOLE HISTORYa1b2c3commit c3author Anamessage fix the totalsparent b2a commit: one snapshot of every filemaind4e5featureA repository holds commits. A branch is a movable name for one of them.Each commit is a snapshot of the whole project and points back to its parent, which makes thehistory. A branch is only a label on a commit, so creating one is instant. New work on thefeature branch does not touch main until someone merges it.

A repository holds commits. A branch is a movable label on one of them.

Repositories, commits, branches · Going deeper

The Git words to know

TermMeaningRepository (repo)A project's files plus the full history of every changeCommitA saved snapshot with an id (a hash), an author, a date and a messageBranchA movable name pointing at a commit; the default branch is usually mainHEADThe commit and branch you currently have checked outCloneA full copy of a repository on your machine, linked to where it came fromRemoteAnother copy of the repository, usually on GitHub; the first is named originTagA fixed name for one commit, used to mark releases such as v1.2.0.gitignoreA file listing what Git should not track: build output, secrets, editor filesThe exam expects these eight words to be automatic.

HEAD, clone, remote, origin, tag and .gitignore.

The basic workflow

Add, commit, push: three moves to GitHub

Working directorythe filesyou editStaging areachosen for thenext commitgit addLocal repositorycommits onyour machinegit commitGitHubthe remoterepositorygit pushgit pull brings other people's commits back downA change travels in three moves: add, commit, push.You edit files in the working directory. git add chooses which changes go into the next commit.git commit records them in your local repository. git push sends your commits to GitHub, whereother people can see them. Nothing reaches GitHub until you push.

git add stages, git commit records, git push shares. git pull brings changes back.

The basic workflow · Going deeper

The basic Git commands

CommandWhat it doesgit initStart a new repository in the current foldergit clone <url>Copy a repository from GitHub to your machinegit statusShow what has changed and what is stagedgit add <file>Stage a change for the next commitgit commit -m "message"Record the staged changes as a commitgit pushSend your commits to the remotegit pullFetch the remote's commits and merge them into your branchgit switch -c <name>Create a branch and move to it (older: git checkout -b)git merge <branch>Bring another branch's commits into the current onegit logShow the history of commitsgit fetch downloads without merging. git pull is fetch followed by merge.

Ten commands. git pull is fetch followed by merge.

Check yourself

abcdemainfeature

How many commits does feature have that main does not?

Merging

A merge joins two lines of work into one

BEFORE: TWO LINES OF WORKb2c3d4e5mainfeatureAFTER THE MERGE: ONE HISTORYb2c3d4e5m6mainMerging joins a branch back into main, keeping the work of both.Git combines the two lines and records a merge commit with two parents. If both sides changed thesame lines, Git stops and reports a merge conflict, and a person decides which version to keep.On GitHub, a merge normally happens through a pull request.

A merge brings a branch into main. A merge conflict needs a person to decide.

Merging · Going deeper · 1 of 2

Merge methods and conflicts

Merging a pull request on GitHubCreate a merge commitKeeps every commit of the branch and adds a merge commitSquash and mergeCombines the branch's commits into one new commit on mainRebase and mergeReplays the branch's commits on top of main; no merge commitWhen things collideMerge conflictBoth branches changed the same lines; Git cannot chooseResolving itEdit the file to the version you want, then commit the resultWhereIn GitHub's web editor for simple cases, or on your machineFast-forwardmain had no new commits, so its label simply moves forwardSquash gives a tidy history of one commit per pull request.

Merge commit, squash or rebase: three buttons on a pull request.

Merging · Going deeper · 2 of 2

Clue words for Git and GitHub

When the question saysThe answer isRemember"track changes to files over time"Version controlhistory, undo, collaboration"distributed version control system"Gitevery clone has the history"cloud platform that hosts repositories"GitHubbuilt around Git"a snapshot of the project"A commitid, author, message, parent"an isolated line of development"A brancha movable pointer"copy a repository to your machine"git clonea full copy, with history"choose changes for the next commit"git addthe staging area"upload local commits"git pushto the remote"download and integrate changes"git pullfetch, then merge"combine two branches"Mergethrough a pull request"same lines changed on both sides"Merge conflicta person resolves it"files Git should not track".gitignorebuild output, secretsThis heading and the next are the largest domain: 25 to 30% of the exam.

Commit, branch, clone, push, pull and merge.

Check yourself

Working directoryStaging areanotes.mdLocal repository

notes.md is where the drawing shows. Which command records it in the history?

Lesson complete

You finished “Version control, Git and GitHub”

Answer the questions along the way to see your score here.

Open in a new tab