07 / 07
Version control with commits, branches and a remote.
Version control is a system that records changes to files over time. You can take a file, or a whole project, back to an earlier state, and see what changed when and by whom.1
Git is the most widely used version control system, and it is distributed: every developer has a full copy of the project and all of its history on their machine. That is why you do not need to be online to commit.21
GitHub is not Git. It is a service that hosts Git repositories (repos for short) and adds team tools such as pull requests and code review on top. GitLab and Bitbucket do the same job.2
Git is usually already installed on macOS and Linux; check with git --version in the terminal. Once it is installed, the first thing to do is set your name and e-mail address. Every commit carries them, and they cannot be changed afterwards:3
git config --global user.name "Ömer Gülçiçek"
git config --global user.email iletisim@omergulcicek.com
git config --global init.defaultBranch mainThat last line names the main branch main in the projects you start from now on. GitHub and the other services use main, and Git itself will move its default from master to main in version 3.0.34
A file in Git is in one of three states:5
| State | Meaning |
|---|---|
| Modified | You changed the file but have not recorded it yet |
| Staged | You marked the change to go into the next commit |
| Committed | The change is safely stored in Git's database |
So the flow is always the same: you change a file, you add the changes you want in the commit to the staging area with git add, and then you take a photograph of that moment with git commit.52
| Command | What it does |
|---|---|
git init | Starts a new repo in the folder you are in |
git clone <url> | Downloads a remote repo, with all of its history, to your machine |
git status | Shows which files changed and which are staged |
git diff | Shows the changes that are not staged yet, line by line |
git add index.html | Adds the file to the staging area (git add . adds everything) |
git commit -m "Add cart button" | Records the staged changes in the history |
git log --oneline | Lists the commit history, one line each |
Tip
When you are not sure what to do, run git status first. Its output often suggests the next command as well.
Files that should stay out of the repo go in a .gitignore at the root. In frontend projects those are usually node_modules, the build output (dist, .next) and files that carry secrets, such as .env.6
node_modules
dist
.envWarning
Adding an already committed file to .gitignore does not remove it from the repo. Untrack it first with git rm --cached .env. If a password or an API key has been pushed even once, change the key as well.6
A branch is an independent line of work where you split off from the main code and make your own changes. You can try a new feature without disturbing main.
git switch -c add-to-cart-button # create a branch and switch to it
git switch main # go back to main
git branch # list the local branchesgit switch -c creates the branch and switches to it.7 Older sources do the same job with git checkout -b; both work.8
git checkout used to be used both for switching branches and for undoing changes to files. Having one command do two different jobs was confusing, so Git 2.23 (2019) split them in two: git switch for moving between branches, git restore for undoing file changes.9 git checkout still works, but git switch is safer for switching branches: it only deals with branches, and it will not wipe your file changes by accident.7
| Task | New command | Old equivalent |
|---|---|---|
| Switch to a branch | git switch main | git checkout main |
| Create a branch and switch | git switch -c add-to-cart-button | git checkout -b add-to-cart-button |
| Undo a change in a file | git restore index.html | git checkout -- index.html |
When the work is done, you merge the branch into main:
git switch main
git merge add-to-cart-buttonIf main has not moved since you left it, Git just moves the pointer forward (a fast-forward). If both sides moved on, it creates a new merge commit joining the two ends and their common ancestor.8
The repo on your machine is the local one, the one on GitHub is the remote. The default remote is called origin.2
| Command | What it does |
|---|---|
git pull | Downloads new commits from the remote and adds them to the branch you are on |
git push | Sends your local commits to the remote |
git push -u origin add-to-cart-button | Sends a new branch for the first time and links it to its remote counterpart |
A commit is a local operation; your teammates do not see your change until you git push.2
Most teams work with something close to GitHub flow:1011
Bring main up to date: git switch main and git pull.
Open a new branch for the job.
Make small commits and push the branch.
Open a pull request; your teammates review the code (see Code and team).
Address the comments with new commits. Every push updates the PR on its own.
Once it is approved, merge the PR and delete the branch.
In this setup nothing is committed to main directly; everything arrives through a PR. That way main always carries working code.11
A good branch name lets the team look at the list and tell who is working on what: short, but descriptive.10 On teams that use Jira the name usually starts with the ticket number, because Jira picks that number out of the branch name and shows the branch, its commits and the PR under the ticket:12
git switch -c feature/PROJ-123-add-to-cart-button
git switch -c bugfix/PROJ-456-price-roundingThe common pattern is type/TICKET-NUMBER-short-description. The type prefixes are usually feature/, bugfix/, hotfix/ and release/.13 Write branch names in lowercase, separating words with hyphens; keep spaces and accented characters out of them. If your team has settled on a rule of its own, that is the one that counts.
Jobs that have nothing to do with each other should not share a branch. GitHub recommends the same: a separate branch for each unrelated group of changes.10
Review gets easier. A PR that tells one story is read faster and more carefully.
Neither job holds the other up. With two jobs on one branch, a problem in one keeps the other from shipping too.10
There are fewer conflicts. Small, short-lived branches carry fewer changes and find their way back to main easily.14
If you spot another bug while working, open a new ticket and a new branch rather than fixing it on the same one. The exception is a small fix that is blocking your own work; mention those in the PR description.
A commit message is written for the person who asks "why is this line like this?" a few months from now. Chris Beams's widely accepted rules, briefly:15
Keep the subject under 50 characters and do not end it with a full stop.
Write the subject in the imperative: "Fix add to cart button", not "Fixed add to cart button".
If more is needed, leave a blank line and explain what you changed and why.
Teams using Jira put the ticket number in the commit message too: PROJ-123 Fix add to cart button.12
A merge conflict happens when two branches change the same lines of the same file differently, or when one deletes a file the other changed. Git cannot decide which is right, so it stops the merge.16 On a team you usually meet it like this: you are working on your branch, a teammate's PR lands in main, and your PR starts warning about a "conflict".
To fix it, bring the current main into your own branch:
git switch main
git pull
git switch feature/PROJ-123-add-to-cart-button
git merge mainIf there is a conflict, Git tells you which files it is in, and git status lists them under Unmerged paths.8 Open the file and you see the two versions side by side between markers:816
<<<<<<< HEAD
<button class="btn-primary">Add to cart</button>
=======
<button class="btn btn-lg">Add to Cart</button>
>>>>>>> mainBetween HEAD and ======= is the version on your branch, and between ======= and >>>>>>> the one coming in. Then:17
Decide which one stays. The right answer is often a combination of the two.
Delete the three marker lines.
Run the project and test it.
Mark it resolved with git add <file>.
Finish the merge with git commit, and git push.
Editors such as VS Code show "Accept Current", "Accept Incoming" and "Accept Both" buttons on a conflicted file; that is the same job, done by clicking.
Don't
Do not resolve a conflict by saying "keep mine" and deleting the other side without reading it.
Do not leave the marker lines (<<<<<<<, =======, >>>>>>>) in the file and commit them.
Some teams use git rebase main instead of git merge main. The result is similar, but it rewrites history; ask which one your team prefers.
Practice: Learn Git Branching, which teaches branching, merging and rebasing visually in the browser.18
Book: Pro Git, Git's official and free book. The first three chapters are enough for daily use.1
Inside Git: Mary Rose Cook's article and talk on how Git stores commits and branches as a graph.19
Talk: Pro Git's author Scott Chacon on little-known Git features, at FOSDEM 2024 (50 min). Watch it once the basics have settled.20
Sources
What version control is, local, centralised and distributed systems, and every copy being a full backup.
The difference between Git and GitHub, distributed version control, and the `clone`, `pull`, `add`, `commit`, `push` flow.
The `user.name`, `user.email` and `init.defaultBranch` settings.
The default branch name in new repos becoming `main` in Git 3.0; the release has no date yet.
The modified, staged and committed states; the working tree, the staging area and the Git directory.
`.gitignore`, removing a tracked file with `git rm --cached`, and the ready-made templates.
`git switch`, and creating a branch and switching with `-c`.
Opening a branch, fast-forward and merge commits, the conflict markers, `Unmerged paths` and resolving with `git add`.
`git checkout`'s jobs being split into the experimental `git switch` and `git restore` (August 2019).
The branch, commit, PR, review, merge and delete steps; short descriptive branch names and separate branches for unrelated work.
Developing every feature on its own branch, and keeping broken code out of `main`.
Linking to Jira by putting the Jira key in the branch name, the commit message and the PR title.
The `feature/`, `bugfix/`, `hotfix/` and `release/` prefixes.
: — Feature branches, integration frequency, and small frequent integrations reducing conflicts.
: — The seven rules of a good commit message and the imperative-mood test.
When a conflict appears, the markers, `git status` and `git merge --abort`.
Resolving a conflict in the terminal step by step: edit, `git add`, `git commit`.
Visual, interactive Git exercises that run in the browser.
: — How Git holds commits, branches and HEAD as a graph; there is a too.
: — 50 minutes. Little-known Git settings and commands; for experienced users.
Check the lesson's main ideas with three questions.
Your progress is stored only in this browser.
Last updated: November 3, 2026