Git Explained: Save Points for Your Work
Git remembers every version of your project so you can undo mistakes and work with others. Learn the four ideas and the six commands you will use every day.
Have you ever saved files as `report_final`, `report_final2`, and `report_final_REAL`? Git solves that problem for good. It is a tool, used by almost every software team, that records every change to your files so you can go back to any earlier version.
The idea in one sentence
Git is a time machine for your project. Each time you reach a good state, you take a snapshot, called a commit, with a short note about what changed.
Four words to know
- Repository (repo): the project folder that Git is tracking, with its full history.
- Commit: a saved snapshot of your files, with a message and the author's name.
- Branch: a separate line of work, so you can try something new without touching the main version.
- Remote: a copy of the repo stored online, such as on GitHub, used for backup and teamwork.
Your first project, step by step
Install Git
Download it from the official Git website and install it. Check it with `git --version`.
Set your name
Run `git config --global user.name "Your Name"` and `git config --global user.email "you@example.com"`.
Start tracking a folder
In the project folder, run `git init`. Git now watches it.
Choose what to save
Run `git add .` to stage all current changes, or `git add filename` for one file.
Take a snapshot
Run `git commit -m "Add home page"`. Write messages that say what and why.
Look at the history
Run `git log --oneline` to see your commits.
Working with branches
To try a new idea safely:
Create a branch
`git switch -c new-idea`
Work and commit as usual
Your main branch is untouched.
Go back when you want
`git switch main`
Merge the idea in when it works
`git merge new-idea`
Sharing with others
- `git clone URL` copies a project from GitHub to your computer.
- `git push` uploads your commits to the remote copy.
- `git pull` brings other people's new commits to you.
What most people miss
- Never commit secrets. Passwords, API keys, and private files should stay out of Git. Use a `.gitignore` file to exclude them. Once pushed publicly, a secret should be treated as stolen.
- Git is not backup by itself. It only protects what you have pushed to a remote.
- Small, frequent commits are easier to understand and undo than rare giant ones.