Git Basics in 15 Minutes
Git saves snapshots of your project so you can undo mistakes, try ideas safely and work with other people. Here's the core workflow, minus the jargon.
Snapshots of your project
Git is version control: it keeps a history of your project as a series of snapshots called commits. You can look back at any snapshot, compare them, or return to one.
Git runs on your computer. GitHub (and similar sites like GitLab) store a copy online so you can back it up and share it.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# start tracking a folder
cd my-project
git initThe everyday cycle: change, add, commit
Files move through three places:
| Place | What it means | Command to move on |
|---|---|---|
| Working folder | Files as you're editing them | git add |
| Staging area | Changes picked for the next snapshot | git commit |
| History | Saved snapshots | (done) |
git status # what has changed?
git add index.html # stage one file (or: git add . for everything)
git commit -m "Add contact page" # save the snapshot
git log --oneline # see the historyGood commit messages say what the change does, in a few words: “Add contact page”, not “stuff” or “update”.
Branches: try ideas safely
A branch is a separate line of work. Make one to try a new feature without touching the working version on main.
git switch -c new-header # create a branch and move to it
# ...edit, add, commit as usual...
git switch main # go back to main
git merge new-header # bring the changes acrossMerge conflicts happen when two branches changed the same lines. Git marks the clash in the file with <<<<<<< and >>>>>>>; keep the version you want, delete the markers, then add and commit.
Sharing on GitHub
git remote add origin https://github.com/you/my-project.git
git push -u origin main # first time
git push # after that
git pull # get other people's changes
git clone https://github.com/someone/project.git # copy a projectNever commit passwords or API keys. Once pushed, assume they're public. Add files like .env to a .gitignore file so Git ignores them.
Click files to edit them, then stage and commit.