Git is a tool that records snapshots of your project over time, so you can see what changed, undo mistakes, and work with other people without overwriting each other. To start using it you install Git, tell it who you are with git config, run git init in a folder, then stage and commit your files. That is the whole loop, and the rest of this guide walks through it slowly.
Along the way I cover the two errors almost every beginner hits in their first ten minutes ("Please tell me who you are" and "fatal: not a git repository"), what the staging area really does, Git aliases that shorten the everyday commands, and how to put a WordPress project under Git.
If you have never used version control before, the hardest part of Git is not the commands. It is the mental model. Once the model clicks, the commands stop feeling arbitrary. So I am going to start there.
The mental model: four places your code lives
Most Git confusion comes from not knowing where your files are at any moment. Git moves your work through four distinct places:
- Working directory: the actual files on disk that you edit in your editor. This is the only place that looks normal in your file browser.
- Staging area (also called the index): a holding area where you gather the exact changes you want in your next snapshot.
- Repository (the local
.gitfolder): the permanent history of committed snapshots, stored on your own machine. - Remote: a copy of the repository hosted somewhere else (GitHub, GitLab, your own server) that you and your teammates push to and pull from.
Work flows in one direction most of the time:
working directory → staging area → local repository → remote
(edit) (git add) (git commit) (git push)You edit files in the working directory. You pick which changes to include with git add, which moves them to the staging area. You take a snapshot of the staging area with git commit, which writes it into your local repository. Finally you git push to send those commits to the remote.
The staging area is the part beginners skip past, and it is the part that gives Git its precision. It lets you commit some of your changes and leave the rest for the next commit. I break it down properly in the Git staging area explained below, because it is worth understanding rather than treating git add . as a magic word.
Installing Git
You probably already have Git. Check first:
git --versionIf you see a version number (anything 2.x is fine), skip ahead. If the command is not found, install it.
macOS: the easiest path is Homebrew:
brew install gitIf you have Xcode Command Line Tools installed, you already have a Git, but the Homebrew one stays more current.
Windows: download the installer from git-scm.com/download/win. It ships Git Bash, a terminal that behaves like Linux, which is what every tutorial (including this one) assumes. One thing to know up front: in Windows PowerShell, curl is an alias for Invoke-WebRequest, not the real curl, so when a guide tells you to run curl, use Git Bash or call curl.exe.
Linux (Debian/Ubuntu):
sudo apt update && sudo apt install gitOn Fedora it is sudo dnf install git; on Arch, sudo pacman -S git. Re-run git --version afterward to confirm.
First-time setup: tell Git who you are
Every commit records an author name and email. If you skip this, your very first commit fails with Please tell me who you are. Set it once, globally, and you never deal with it again:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"Use the same email here as on your GitHub account so your commits link to your profile. While you are at it, set a couple of sensible defaults:
git config --global init.defaultBranch main
git config --global pull.rebase falseThe first makes new repositories start on a branch called main instead of the older master. The second picks a clear default for what git pull does when your branch and the remote have both moved, which heads off the divergent-branches reconcile message. Confirm what Git recorded:
git config --listFix "Please tell me who you are"
If you skipped the setup above, git commit stops with this and creates nothing:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'you@laptop.(none)')The error already contains the fix: run those two git config lines with your own name and email, then commit again. Nothing to install or restart. The last line is Git explaining that it tried to guess an email from your system username and hostname, decided you@laptop.(none) was not trustworthy, and refused rather than stamp a placeholder author into your history. Older Git used to let such commits through with a warning; modern Git refuses, which is the better behavior. Every commit stores an author and a committer, each with a name and email, inside the commit object itself, so this check happens locally before any push.
Check what you set with git config --global user.name and git config --global user.email.
--global versus per-repo identity
| Scope | Command | Stored in | Use when |
|---|---|---|---|
| Global | git config --global user.email "..." | ~/.gitconfig | Your normal everyday identity, set once per machine |
| Per-repo | git config user.email "..." (no --global, run inside the repo) | .git/config | A specific repo needs a different identity (work vs personal email) |
| System | git config --system user.email "..." | /etc/gitconfig | Rare: a machine-wide default for all users (needs admin rights) |
A per-repo identity always wins over the global one for that repository, which is how I keep work and personal commits on different addresses:
# inside the work repository, no --global:
git config user.email "ada@company.com"
git config user.name "Ada Lovelace"When the error keeps coming back
- You set it per-repo in a different folder. Running the config without
--globalinside another project only fixed that one repo. Re-run with--global. - A typo in the key. The keys are
user.nameanduser.email, lowercase, with a dot. See what Git actually reads, and from which file, withgit config --list --show-origin | grep user. - A commit was already authored badly. Setting the identity only affects future commits. To restamp the most recent one with your corrected identity:
git commit --amend --reset-author --no-editRewriting older commits changes their hashes and forces everyone else to re-sync, so only do it on history you have not shared.
Your first repository
Make a folder, go into it, and turn it into a Git repository:
mkdir hello-git
cd hello-git
git initgit init creates the hidden .git folder. That folder is the repository: it holds your entire history. Delete it and the folder goes back to being an ordinary directory with no version control. The same git init works for adding Git to a folder that already has code, not just a fresh empty one. For the full start-to-first-push checklist on a brand-new project (init, the default branch, a starter .gitignore, and wiring the remote), see how to set up Git for a new project.
Now create a file and check the status:
echo "# Hello Git" > README.md
git statusYou will see something like:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)git status is the command you will run more than any other. It tells you which place each change is sitting in: untracked (Git does not know about it yet), staged (ready for the next commit), or modified-but-not-staged.
Fix "fatal: not a git repository"
Run a Git command outside a repository and you get:
fatal: not a git repository (or any of the parent directories): .gitGit looks for a .git folder by walking up from your current directory. If it reaches the top of the filesystem without finding one, it prints this. The repo is not broken; you are almost always in the wrong place. Check where you are, including hidden files:
pwd
ls -laIn a repository, the listing includes a .git entry:
drwxr-xr-x 12 techearl staff 384 May 31 10:02 .git
-rw-r--r-- 1 techearl staff 166 May 31 10:01 README.mdNo .git line means this folder is not the top of a repository, but you could still be inside one further down the tree (and a linked worktree has a .git file rather than a folder). Git can answer directly:
git rev-parse --is-inside-work-tree # prints true inside a repo
git rev-parse --show-toplevel # prints the repo root, or errors outside oneIf both fail with the same "not a git repository" error, you are outside any repository. Pick the fix that matches what you were trying to do:
| Your situation | What to run | What it does |
|---|---|---|
| The project is elsewhere on disk | cd /path/to/project | Moves your terminal into the existing repo |
| Brand-new project, no repo yet | git init | Creates a fresh .git folder here |
| You want a copy of a remote repo | git clone <url> | Downloads the repo and its history |
.git was deleted by accident | restore from backup or re-clone | Recovers the repository |
The classic clone mistake: git clone https://github.com/your-name/my-app.git drops the repo into a new my-app subfolder, so you have to cd my-app before Git commands work. Forgetting that cd lands you right back on this error.
Run git init in the project root, not a parent folder, or you will track far more than you meant to. If you did init one level too high, rm -rf .git removes that stray repository; it permanently deletes the history in the current directory, so confirm with pwd first.
Less obvious causes: you copied a project's files without the hidden .git folder (the copy is not a repository); you are in a folder that is not inside the repo at all (Git searches upward only, never into sibling folders); or the GIT_DIR environment variable points somewhere that does not exist (echo $GIT_DIR, and unset it if it prints anything).
What is inside .git
.git/
├── HEAD # points at your current branch
├── config # this repo's settings and remotes
├── objects/ # every commit, tree, and file blob
├── refs/ # branch and tag pointers
└── index # the staging areaYour project files live alongside .git in the working tree; .git is the database. Delete it and the current files survive, but the entire history is gone unless you have a backup or a pushed copy to re-clone. Because it carries your whole history, never let a web server serve .git publicly: an exposed .git directory lets anyone rebuild your source and any secrets committed along the way.
Your first commit
Move the file into the staging area, then snapshot it:
git add README.md
git commit -m "Add README"git add stages the file. git commit writes the staged snapshot into your repository with the message you passed. Run git status again and it reports a clean working tree, then look at the history:
git log --onelinea1b2c3d (HEAD -> main) Add READMEThat short string is the commit hash, a unique fingerprint for this snapshot. HEAD is just Git's word for "the commit you are currently sitting on."
A few habits worth forming on day one:
- Commit small, related changes together, not a giant dump at the end of the day.
- Write messages that describe why, in the present tense ("Fix login redirect", not "fixed stuff"). I keep a longer list of commit message conventions.
- Run
git statusbefore you commit so you know exactly what you are about to capture.
If your first commit captures the wrong thing or a bad message, you are not stuck. Undoing the last commit is a one-liner, and you almost never lose work doing it.
The Git staging area explained
The staging area, also called the index, is the snapshot of what will go into your next commit. git add <path> copies the current content of that file into the index; git commit records the index as a new commit, and nothing else. Most tools have two states, saved and unsaved. Git has three:
| Area | What it is | How a file gets there |
|---|---|---|
| Working directory | The actual files on disk that you edit | You edit them in your editor |
| Staging area (index) | The snapshot queued for the next commit | git add <file> |
| Repository (commit history) | The permanent, recorded snapshots | git commit |
Edit a file and git status reports it as modified but not staged; git add moves it to "Changes to be committed":

On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: app.jsThe key detail: git add snapshots the file at the moment you run it. Edit app.js again after staging it and the newer edit is not staged; the file now shows in both groups at once until you git add it again. git add . stages everything in the current directory and below, including new files.
Why staging exists
A commit and a set of edits are not the same thing. In one session you might fix a bug, correct a typo, start a feature, and leave two debug console.log lines. Staging lets you commit the bug fix on its own with a clear message, then the feature separately, so history is a series of focused commits instead of one "fixed stuff" blob. It is also a review checkpoint:
git diff # working-directory changes not yet staged
git diff --staged # what the next commit will contain (also spelled --cached)I run git diff --staged right before every commit. A pre-commit hook can automate the same check.
Stage part of a file with git add -p
Fixed a bug and added a debug line in the same file? git add app.js would stage both. Stage interactively instead:
git add -p app.jsStage this hunk [y,n,q,a,d,s,e,?]?| Key | Action |
|---|---|
y | Stage this hunk |
n | Do not stage this hunk |
s | Split the hunk into smaller pieces (when two changes are close together) |
q | Quit; stop reviewing hunks |
? | Show help for all the options |
Press y on the fix and n on the debug line; only the fix is committed, and the debug line stays in your working directory.
Unstage a file
Staged something by mistake? Nothing is committed yet, so this is safe:
git restore --staged app.js # Git 2.23 and newer
git reset HEAD app.js # older GitBoth remove the file from the index and leave your edits untouched; git status prints the right one for your version. Unstaging is not discarding: to throw edits away, see discarding local changes in Git.
Connecting to a remote and pushing
So far everything lives only on your laptop. To back it up and share it, push to a remote. Create an empty repository on GitHub (no README, so it does not conflict with yours).
The remote URL below is the SSH form (git@github.com:...). The other option is HTTPS (https://github.com/...). They behave the same once set up; they differ in how you authenticate. The short version: SSH uses a key you generate once, HTTPS uses a token. GitHub stopped accepting account passwords for Git over HTTPS in 2021, so if you try to push with your password you hit Support for password authentication was removed, which is why GitHub password pushes now fail. To use the SSH URL as written, generate the SSH key first, then follow adding an SSH key to GitHub, or read the full comparison in SSH vs HTTPS remotes. If you have not set up a key yet, use the HTTPS URL instead and authenticate with a token. Then connect the remote and push:
git remote add origin git@github.com:your-username/hello-git.git
git branch -M main
git push -u origin mainorigin is the conventional name for your main remote. The -u flag links your local main to the remote main so that future pushes are just git push with no arguments. Skip the -u and your next push complains that the current branch has no upstream branch, which I cover in the no-upstream-branch fix. If the push fails with src refspec main does not match any, you have not committed yet; that fix is here. This flow is for a repo you created locally; if you are instead joining a project that lives on GitHub already, the path is the inverse, cloning a project that already exists.
What not to commit
Some files should never go into Git: secrets, dependency folders, build output, editor junk. You tell Git to ignore them with a .gitignore file. A minimal one for a Node project:
node_modules/
.env
.DS_Store
dist/Add that file, commit it, and Git stops nagging about everything it lists. The two rules I care about most: never commit node_modules (it is huge and reproducible from your lockfile), and never commit a .env or any other file with credentials. A leaked secret in your history is genuinely dangerous, and getting one back out is a chore covered in removing a secret from Git history. Set up the ignore file early; the full reference is how to use .gitignore.
The everyday loop
Once the repo is connected, your normal day looks like this:
git pull # get the latest from the remote
# ... edit files ...
git add . # stage your changes
git commit -m "..." # snapshot them
git push # send them upThat is genuinely most of Git. Everything else is for when something goes sideways or you want more control.
How pull, fetch, add, and commit relate
A quick reference for the commands you will reach for constantly:
| Command | Which place it touches | What it does |
|---|---|---|
git add | working directory → staging | Selects changes for the next commit |
git commit | staging → local repository | Records a permanent snapshot locally |
git push | local repository → remote | Uploads your commits to the remote |
git fetch | remote → local repository | Downloads remote commits without changing your files |
git pull | remote → working directory | fetch plus merging the changes into your branch |
The fetch vs pull distinction trips up everyone at first: fetch is the safe "just show me what's new" and pull is "show me and apply it." I unpack it in git pull vs git fetch.
Set up Git aliases
Once the everyday loop is muscle memory, aliases shave the keystrokes off it. The whole mechanism is one command: git config --global alias.<name> <command>. So git config --global alias.co checkout makes git co mean git checkout in every repo on your machine.
Aliases set with --global are written to ~/.gitconfig under an [alias] section. You can edit that file directly, which is faster than running ten git config commands and easier to keep in your dotfiles:
[alias]
co = checkout
st = status -sb
br = branchDrop --global and the alias goes into the current repo's .git/config instead, which is rarely what you want for a shortcut. Keep aliases global and let every repo inherit them.
A short set worth keeping
This is the set I actually use every day, after years of trimming the list down. They are short on purpose: an alias you have to think about is an alias you will not reach for.
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.last "show HEAD"
git config --global alias.unstage "restore --staged"
git config --global alias.amend "commit --amend --no-edit"
git config --global alias.undo "reset --soft HEAD~1"git strunsstatus -sb: the short status format plus the branch line, so the whole picture fits on one screen.git lgis a compact, colored commit graph across all branches. The mnemonic for the four switches is "a dog": all, decorate, oneline, graph.git lastshows the most recent commit, message and diff.git unstage <file>wrapsgit restore --staged, the same move as unstaging a file above.git amendfolds your staged changes into the previous commit and reuses its message, no editor pop-up, because of--no-edit. Do not amend a commit you have already pushed to a shared branch.git undomovesHEADback one commit but leaves the changes staged, so a wrong commit becomes uncommitted-but-not-lost work. For the full set of undo moves see how to undo things in Git.
You can alias an alias since Git 2.20, so git config --global alias.unst unstage is legal if you want a shorter handle.
Shell aliases and the ! prefix
A plain alias can only expand to a git subcommand. The moment you need a pipe, a shell built-in, more than one command, or an argument anywhere other than the end, prefix the expansion with ! and Git hands the whole thing to your shell:
git config --global alias.cleanup "!git branch --merged | grep -v '\*' | xargs -r git branch -d"git cleanup now deletes every local branch already merged into the current one. Arguments are the catch: Git appends any extra arguments to the end of the expansion. When one has to land in the middle, wrap the body in a throwaway shell function (named f by convention) and call it, which is the pattern the git-config docs recommend:
git config --global alias.diffmain '!f() { git diff "$1" main; }; f'
git config --global alias.search '!f() { git log --all --oneline --grep "$1"; }; f'git diffmain feature/login diffs the branch you name against main, which a plain alias cannot do. git search "connection reset" greps every branch's commit messages, and quoting "$1" keeps a multi-word search as one pattern. Two more things to know: a shell alias runs from the top level of the repository, not your current directory (Git sets GIT_PREFIX to where you actually were), and the arguments are plain shell positional parameters, so quote them.
Git aliases still need the word git. A shell alias in your ~/.zshrc or ~/.bashrc removes that too:
alias g=gitNow g st runs git status -sb. I keep g in my shell and let the git aliases carry the real shortcuts, so a teammate copying my ~/.gitconfig gets the useful part without my shell config.
How to use Git with WordPress
Do not put your whole WordPress install under Git. Track only the code you actually write, your theme and custom plugins, and ignore core, uploads, third-party plugins, and anything with secrets in it like wp-config.php. WordPress mixes code you author, code you downloaded, and content users uploaded in one folder; Git is for the first kind.
| Path | Track in Git? | Why |
|---|---|---|
wp-content/themes/your-theme/ | Yes | Code you write. The whole point. |
wp-content/plugins/your-plugin/ | Yes | Custom plugins you author. |
wp-content/mu-plugins/ | Usually | Small must-use snippets you maintain. |
WordPress core (wp-admin/, wp-includes/, wp-*.php) | No | Downloaded code. Reinstallable. Huge churn on every update. |
| Third-party plugins and themes | No | Not yours. Manage via the dashboard or Composer. |
wp-content/uploads/ | No | User content, often gigabytes. Belongs in a backup, not a repo. |
wp-config.php | No | Contains DB credentials and salts. A secret. |
wp-content/cache/, transients, logs | No | Generated. Different on every machine. |
vendor/, node_modules/ | No | Installed dependencies. Rebuilt from a lockfile. |
Two rules cover almost every decision: if a tool can regenerate it, do not commit it; if it contains a credential, never commit it.
A WordPress .gitignore
For tracking a full project minus core, put this at the WordPress root (rename my-plugin and my-theme to your real folders):
# WordPress core (reinstallable, never edited)
/wp-admin/
/wp-includes/
/wp-*.php
/index.php
/license.txt
/readme.html
/xmlrpc.php
wp-content/index.php
!/wp-content/
# Secrets and config
wp-config.php
.env
.env.*
*.pem
*.key
# User uploads and generated content
wp-content/uploads/
wp-content/cache/
wp-content/upgrade/
wp-content/backup*/
wp-content/blogs.dir/
# Third-party plugins and themes (manage outside Git)
wp-content/plugins/*
wp-content/themes/*
# ...but DO track the ones I actually wrote
!wp-content/plugins/my-plugin/
!wp-content/themes/my-theme/
# Dependencies (rebuilt from a lockfile)
/vendor/
node_modules/
# Build output
wp-content/themes/my-theme/dist/
# OS and editor cruft
.DS_Store
Thumbs.db
*.log
*.sql
*.tmpwp-content/plugins/* ignores everything in the plugins folder, then !wp-content/plugins/my-plugin/ un-ignores the one you wrote. The un-ignore line has to come after the broad ignore. If you committed wp-config.php before ignoring it, it stays tracked until you untrack it with git rm --cached.
Track just your theme, or the whole site
| Approach | Repo root | Best when |
|---|---|---|
| Whole project | WordPress install root | You own the server and deploy the full site, including which plugins are active. |
| Theme/plugin only | Your theme or plugin folder | You ship a product to clients, or core and plugins are managed separately (managed host, Composer). |
Tracking just the theme is the cleaner default for most people; the .gitignore there only needs build output, node_modules, and editor files:
cd wp-content/themes/my-theme
git init
git add .
git commit -m "Initial theme commit"For the whole site, git init at the WordPress root (for example /var/www/my-site) with the .gitignore above, and read git status before the first commit: if you see wp-config.php or a thousand files under wp-admin/, your ignore rules are not matching yet.
A few WordPress-specific notes. The auth salts live in wp-config.php, so they never reach the repo; on a new server regenerate them from the official salt generator. The database (posts, pages, options, users) is not in Git; back it up separately (wp db export, a cron mysqldump, or a plugin) rather than committing .sql dumps. On Windows you will see the LF will be replaced by CRLF warning often; it is harmless.
To deploy, the simplest route is SSH to the server and git pull in the theme folder. For push-to-deploy without CI, use a bare repo with a post-receive deploy hook; if the theme has a build step (Sass, JS bundling), GitHub Actions is cleaner. Whichever you pick, make sure your web server blocks /.git/.
Where to go next: the rest of the series
This page is the trailhead. Once the basic loop feels natural, branch out into the specific topics below as you hit the need for each one.
Core concepts
- Git branching explained: work on features in isolation without breaking
main, plus tags and team workflows. - How to use .gitignore: keep junk and secrets out of your history.
Undoing things (you will need these)
- How to discard local changes, undo the last commit, and git reset vs git revert.
- How to use git stash: shelve unfinished work to switch tasks.
- How to recover lost commits with git reflog: the safety net almost no beginner knows about.
Working with others
- Start working on an existing project, including fork vs clone, pull requests, and syncing a fork.
- How to resolve merge conflicts and git merge vs git rebase.
- Git branching strategies for teams: GitHub Flow, Git Flow, and trunk-based, compared.
Common errors, fixed
- failed to push some refs
- Permission denied (publickey)
- src refspec main does not match any
- refusing to merge unrelated histories
Pick the topic that matches whatever Git just did to you, and come back here when you need the next piece.
Sources
Authoritative references this article was fact-checked against.
- Pro Git: Getting Started, About Version Controlgit-scm.com
- git-config, official Git documentationgit-scm.com
- Set up Git, GitHub Docsdocs.github.com
- Pro Git: Recording Changes to the Repositorygit-scm.com
- git-add: official Git documentationgit-scm.com
- git-reset: official Git documentationgit-scm.com
- Pro Git - First-Time Git Setupgit-scm.com
- git commit - Git documentationgit-scm.com
- git-init (official Git documentation)git-scm.com
- Pro Git: Getting a Git Repositorygit-scm.com
- git-clone (official Git documentation)git-scm.com
- gitignore: official Git documentationgit-scm.com
- Editing wp-config.php: WordPress Developer documentationdeveloper.wordpress.org
- git-rev-parse: official Git documentationgit-scm.com
- WordPress core source mirror on GitHubgithub.com
- git-config: alias.* (official documentation)git-scm.com
- Pro Git: Git Aliases (official book)git-scm.com





