Git Basics for Web Projects
Learn how Git tracks your website files, the commands you use every day, and how to undo mistakes without losing work.
Key takeaways
- A commit is a saved snapshot of your project with an author, a time and a message explaining the change.
- The everyday loop is edit, check
git status, stage withgit add, then record withgit commit. - Branches let you work on a change in isolation and merge it only when it works.
- A remote such as a hosted Git service gives you an off-machine copy and a place to collaborate.
- Keep passwords and API keys out of the repository, because Git remembers them even after you delete the file.
Git is a version control system: a program that records snapshots of your project so you can see what changed, when, and why, and return to an earlier state whenever you need to. For a website, that means you can try a new layout, break it badly, and be back to a working copy in seconds.
You only need a handful of commands to get most of the value. This guide covers setting up a repository, saving your work as commits, using branches for experiments, sharing the project with a remote server, and recovering from the usual mistakes.
What Git actually stores
A repository is your project folder plus a hidden .git directory that holds the history. Your visible files are the working tree. When you commit, Git saves the state of every tracked file, reusing the stored copy of anything that has not changed, so history stays compact.
Each commit points to the one before it, which forms a chain you can walk backward. A commit is identified by a long hash derived from its contents, and you usually refer to it by the first several characters.
Between your files and the history sits the staging area, also called the index. It is the list of changes you have selected for the next commit. This extra step lets you fix a typo and rewrite a stylesheet in the same sitting, then record them as two separate, understandable commits.
Git works entirely on your machine. You don't need an account or a network connection to commit, branch or look through history. Hosting services build on top of Git, but they are optional.
Set up your first repository
Install Git from your operating system's package manager or from the official Git website. Then tell it who you are, since every commit carries a name and email address:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main The email appears in your history, and anyone who can read the repository can see it. Many hosting services offer a private forwarding address you can use instead.
Now turn a project folder into a repository:
cd my-website
git init
git status git status is the command to run whenever you're unsure. It lists untracked files (ones Git has never recorded), modified files and staged changes, and it often suggests the next command.
If you're starting from a project that already exists on a server, use git clone followed by its address. Cloning copies the files and the full history, and remembers where they came from.
Ignore the files that don't belong
Before your first commit, create a file named .gitignore in the project root. Each line is a pattern for files Git should leave alone:
node_modules/
dist/
.env
*.log
.DS_Store Good candidates are downloaded dependencies, build output, log files, editor clutter and anything containing secrets. Dependencies can be reinstalled from a lock file, and build output can be regenerated, so storing them only bloats the history.
The ignore file works on untracked files. If you already committed something, adding it to .gitignore changes nothing until you run git rm --cached filename and commit that removal. The older commits still contain the file. For a password or key, the right fix is to revoke it and issue a new one, as the guide to environment variables and secrets explains.
The daily loop: status, add, commit
Most of your time with Git is a short cycle. Edit some files, then look at what changed:
git status
git diff
git add index.html css/site.css
git diff --staged
git commit -m "Add contact section to home page" git diff shows unstaged changes line by line. git diff --staged shows what is about to be committed, which is a good last check for stray debugging code.
You can stage everything with git add . but read the status output first. That habit catches the screenshot, database dump or credentials file you didn't mean to include.
Write messages for the person reading the history later, who is usually you. A short first line that says what the change does ("Fix menu overlap on narrow screens") beats "updates". If the reason isn't obvious, leave a blank line and add a few sentences explaining why.
Commit in small, complete steps. A commit that does one thing is easy to review, easy to undo, and easy to find with git log --oneline.
Use branches for anything risky
A branch is a movable label pointing at a commit. Creating one is nearly instant and costs almost nothing, so use them freely.
git switch -c new-header
# edit and commit as usual
git switch main
git merge new-header
git branch -d new-header git switch -c creates the branch and moves you to it. Your main branch stays as it was, so you can jump back to fix an urgent problem on the live site and return to the experiment afterward. Older tutorials use git checkout -b, which does the same job.
When you merge, Git combines both lines of work. If main hasn't moved, it simply slides the label forward. If both branches changed, it creates a merge commit.
A conflict happens when both sides edited the same lines. Git marks the spot in the file with rows of <<<<<<<, ======= and >>>>>>>. Open the file, decide what the final text should be, delete the markers, then run git add on the file and git commit. If the situation looks confusing, git merge --abort returns you to where you were before the merge.
Work with a remote
A remote is another copy of the repository, usually on a hosting service or your own server. After creating an empty repository there, connect it and upload:
git remote add origin [email protected]:you/my-website.git
git push -u origin main The -u flag links your local branch to the remote one, so later you can type just git push and git pull. Pull downloads new commits and merges them into your branch. git fetch downloads them without touching your files, which lets you look first.
Addresses starting with git@ use SSH keys, covered in SSH for beginners. Addresses starting with https:// typically use an access token or a credential helper in place of your account password.
If a push is rejected because the remote has commits you lack, pull first, resolve any conflicts, then push again. Avoid git push --force on a shared branch: it can erase other people's commits.
Teams usually push a branch and open a pull request (some services call it a merge request) so someone can review the change before it reaches main. That same moment is a natural place to run automated tests, as described in continuous integration and delivery.
Undo mistakes safely
Git offers several kinds of undo, and choosing the right one matters.
| Situation | Command | What it does |
|---|---|---|
| Discard unsaved edits to a file | git restore index.html | Replaces the file with the staged or last committed version |
| Unstage a file | git restore --staged index.html | Keeps your edits, removes them from the next commit |
| Fix the last commit message | git commit --amend | Replaces the last commit with a new one |
| Reverse a published commit | git revert <hash> | Adds a new commit that undoes the old one |
| Set work aside briefly | git stash, later git stash pop | Shelves uncommitted changes |
The first row is the one to be careful with. Edits that were never committed or staged cannot be recovered from Git once discarded.
For history you have already pushed, prefer git revert. It leaves the record intact, so nobody else's copy gets out of step. Commands that rewrite history, such as amend, rebase and git reset --hard, are best kept to commits only you have.
If you seem to have lost a commit, run git reflog. It lists where your branch pointers have been recently, and you can create a new branch at any of those hashes.
Connect Git to your live site
Git makes a clean base for publishing. A common setup is that pushing to main triggers a build and deploy on your host. Another is that you log into the server and pull. Either way, you know exactly which version is live and can roll back to the previous one. The walk-through on deploying a website compares the options.
Two cautions apply. First, never let your web server serve the .git directory to the public, since it can expose your whole history; check by requesting /.git/config on your own site and confirming you get an error. Second, Git is not a full backup. It doesn't include your database, user uploads or server settings, so plan for those separately, using a tested website backup.
Large binary files such as video also fit poorly, because each new version is stored again. Keep them in file storage, or look at Git's large-file extensions if you must version them.
Common questions
Do I need Git for a one-person website?
It still helps. The history answers "what did I change before this broke?" and the undo options remove the habit of making copies like index-old-final.html. If you only edit through a hosted site builder, you don't need it.
Is Git the same as GitHub?
No. Git is the tool on your computer. GitHub, GitLab, Bitbucket and others host Git repositories and add reviews, issue tracking and automation.
How often should I commit?
Whenever you reach a state you might want to return to. Several small commits in an afternoon is normal.