Git is a version control system. It helps you track file changes, save checkpoints, go back to older versions, and collaborate more safely.
A simple way to think about Git:
- Working directory = your current files
- Staging area = changes you want included in the next save
- Commit = a saved snapshot of staged changes
- Remote repository = an online copy, such as GitHub
Before making commits, set your identity. This name and email are written into your commits.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"To start Git in a project folder:
git initThis creates a hidden .git folder, which stores the repository history and settings.
--globalmeans the setting applies to all repositories on your computer.git initdoes not upload anything to GitHub.- After
git init, the folder becomes a Git repository.
Use git status often. It shows what is changed, what is staged, and what is still untracked.
git statusTo stage changes:
git add .Then save them with a commit:
git commit -m "Describe what you changed"git statuschecks the current repository state.git add .stages changes from the current folder downward.git commit -m "..."creates a snapshot of the staged changes.
git add .= prepare changesgit commit= save locallygit push= upload to GitHub later
These are similar, but not exactly the same in intent.
git add .
git add -A
git add --allgit add .stages changes from the current directory downward.git add -Astages all changes in the whole repository.git add --allis effectively the same asgit add -A.
.means from here-Ameans everything in the repo
For beginners, git add . and git add -A are the most useful to remember.
A branch is another line of development. It lets you work on something without affecting the main branch right away.
Create a new branch:
git branch feature-xSwitch to that branch:
git checkout feature-xMerge the branch into your current branch:
git merge feature-xToday, many people use git switch instead of git checkout for changing branches:
git switch feature-xCreate and switch in one step:
git switch -c feature-xgit branch feature-xonly creates the branch.git checkout feature-xorgit switch feature-xmoves you onto it.git merge feature-xbrings that branch’s changes into the current branch.
git switch -c feature-x
# make changes
git add .
git commit -m "work on feature x"
git switch main
git merge feature-xThese commands are for undoing different kinds of changes.
git restore <file>This discards local unstaged changes in that file.
Older form:
git reset HEAD <file>Modern form:
git restore --staged <file>This removes the file from the staging area but keeps your work in the folder.
git revert <commit-id>This does not erase history. Instead, it creates a new commit that reverses an earlier one.
restore= undo local file changesrestore --staged= unstage changesrevert= undo a past commit safely with a new commitresetcan rewrite history, so use it carefully
A remote repository is an online copy of your repo, usually on GitHub.
Link your local repo to GitHub:
git remote add origin <repo-url>Push your branch to GitHub:
git push -u origin mainGet the latest changes from GitHub:
git pull origin mainoriginis the usual default name for the remote.mainis the usual default branch name now.git pushuploads local commits.git pulldownloads and merges remote changes.
git commitdoes not upload to GitHub.
You must still run:
git pushThis is one of the most important beginner ideas.
git init
git add .
git commit -m "message"These only affect your computer.
git push
git pullThese interact with GitHub or another remote server.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git initgit status
git add .
git commit -m "message"git branch feature-x
git checkout feature-x
git switch -c feature-x
git merge feature-xgit restore <file>
git restore --staged <file>
git reset HEAD <file>
git revert <commit-id>git remote add origin <repo-url>
git push -u origin main
git pull origin main- Run
git statusoften. - Write clear commit messages.
- Use branches for new features or experiments.
- Be careful with undo commands.
- Remember that
commitis local andpushis remote. - Prefer
git restore --staged <file>over the oldergit reset HEAD <file>when learning.
# first time setup
git init
git status
# after creating or editing files
git add .
git commit -m "first commit"
# connect to GitHub
git remote add origin <repo-url>
git push -u origin mainThink of Git in this order:
change files -> check status -> add -> commit -> push
or even shorter:
edit -> stage -> save -> upload