Git
Git records the history of your project so you can experiment freely, undo mistakes, and work with others.
Git is a version control system. It snapshots your project over time, lets you branch off to try things, and merges work back together. Almost every software project uses it.
The three areas
A change moves through three places before it is shared:
- Your working directory — the files as they are right now.
- The staging area — changes you have marked for the next commit (
git add). - The repository — committed history (
git commit).
Everyday commands
git status # what has changed
git add file.txt # stage a change
git add . # stage everything
git commit -m "message" # record a snapshot
git log --oneline # see history
Branches
A branch is a movable pointer to a commit. Work on a feature without disturbing the main line:
git switch -c new-feature # create and switch
# ...make commits...
git switch main
git merge new-feature # bring it back in
Working with a remote
git clone https://... repo # copy a repo
git pull # fetch + merge others' work
git push # send your commits up
Undoing things
| You want to | Command |
|---|---|
| Unstage a file | git restore --staged file |
| Discard a change | git restore file |
| Fix the last commit message | git commit --amend |
| Undo a commit, keep changes | git reset HEAD~1 |
Commit often, in small pieces. A tidy history of focused commits is far easier to read, review, and bisect than one giant commit at the end of the day.