Home › Programming › Git

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:

  1. Your working directory — the files as they are right now.
  2. The staging area — changes you have marked for the next commit (git add).
  3. 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 toCommand
Unstage a filegit restore --staged file
Discard a changegit restore file
Fix the last commit messagegit commit --amend
Undo a commit, keep changesgit 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.

Keep reading