Git stores the project history in the hidden `.git` folder. `git init` creates it and turns an ordinary folder into a repository. The modern way to set the default main branch `main`: the `-b main` flag at initialization or the `init.defaultBranch` setting for all future repositories.
- Turn the project folder into a repository and start keeping a history of changes.
- Set the default branch `main` (the modern standard instead of `master`).
git init -b mainOutput:
Initialized empty Git repository in /Users/you/project/.git/The `-b main` flag immediately creates the main branch main — master is no longer the default.
git config --global init.defaultBranch mainThe setting applies to all future `git init` without the -b flag — now everywhere it will be main.
Key rules:
- ✓A repository is a folder with history in the hidden .git directory.
- ✓The default main branch is main (set with the -b flag or init.defaultBranch).
Common mistakes:
- !Do not initialize Git inside a subfolder of another repository — you get a nested repository.
The working tree — files on disk. `git status` shows what changed. Changes first go to the index (staging area) via `git add`, and then are recorded in history via `git commit`. Untracked new files also first go into the index via `git add`.
- See uncommitted changes and pending files (git status).
- Commit a meaningful set of changes: git add → git commit -m.
git add index.html
git commit -m "Add home page"Output:
[main 3f2a1b0] Add home page
1 file changed, 12 insertions(+)git add moves the file into the index, git commit records it in history with a short message.
git statusOutput:
On branch main
Changes not staged for commit:
modified: index.html
Untracked files:
styles.cssmodified — a changed but uncommitted file; Untracked — new files not yet added to Git.
Key rules:
- ✓Working tree → git add (index) → git commit (history).
- ✓The -m commit message describes what was done.
Common mistakes:
- !A forgotten `git add` — the changes stay uncommitted, the commit will not record them.
`git log` shows the commit history, and `--oneline` condenses the output to one commit per line. Modern undo commands: `git restore` returns the file to the last commit, and `git revert` creates a new commit that undoes the changes of an old one without touching history.
- View the change history briefly (git log --oneline).
- Undo uncommitted edits (git restore) or committed ones (git revert).
git log --oneline --graphOutput:
* a1b2c3d Home page edits
* 3f2a1b0 Add home page
* 7e9f10a Initial project filesEach line is a commit: short hash and the first line of the message. --graph draws the links.
git restore index.htmlgit restore returns the file to the last committed state, discarding uncommitted edits.
Key rules:
- ✓git restore — undo uncommitted changes (file or only the index via --staged).
- ✓git revert — safe undo of a commit via a new commit, without rewriting history.
- ✓git reset --hard — dangerous: permanently erases uncommitted changes.
Common mistakes:
- !git revert does not change history but adds a new commit — that is what makes it safe.
A branch is an independent line of development. The modern command for switching and creating is `git switch`: `git switch <branch>` switches to an existing branch, `git switch -c <name>` creates a new one. Merging a branch into the current one is done by `git merge`.
- Work on a feature in isolation, without breaking main.
- Merge the finished branch back via git merge.
git switch -c feature-loginOutput:
Switched to a new branch 'feature-login'The command creates the feature-login branch and immediately switches to it. This is the modern analog of git checkout -b.
git switch main
git merge feature-loginOutput:
Updating 3f2a1b0..7e9f10a
Fast-forward
index.html | 5 +++-
1 file changed, 5 insertions(+)First we switch to main, then git merge pulls in the feature-login changes. After merging the branch can be deleted with git branch -d.
Key rules:
- ✓git switch — switch branch; -c creates a new one and switches to it at once.
- ✓git merge merges the given branch into the current one.
- ✓git branch -d deletes a branch after merging (safely).
Common mistakes:
- !Merging can produce a conflict — Git marks the files, you need to resolve them manually and commit.
A remote repository is a copy of the project on a server (GitHub, GitLab, etc.). `git clone` copies it locally and immediately sets up the `origin` remote. `git push` sends local commits, and `git pull --rebase` fetches changes from the server and replays your local commits on top — giving a clean history.
- Clone a remote project for work (git clone).
- Send your commits (push) and fetch theirs (pull --rebase).
git clone https://github.com/user/project.gitOutput:
Cloning into 'project'...
remote: Enumerating objects: 10, done.
Receiving objects: 100% (10/10), 1.50 MiB.git clone downloads the project and sets up the origin remote source.
git push origin mainOutput:
To https://github.com/user/project.git
* [new branch] main -> maingit push sends the local main branch to the origin remote under the same name.
git pull --rebase origin mainOutput:
Successfully rebased and updated refs/heads/main.git pull --rebase fetches new commits from the server and replays your not-yet-pushed commits on top.
Key rules:
- ✓origin — the default remote name after git clone.
- ✓git push — send commits; git pull --rebase — fetch theirs and replay yours on top.
- ✓For extra files use .gitignore so you do not commit them accidentally.
Common mistakes:
- !git pull without --rebase may create extra merge commits — prefer pull --rebase.