Git and GitHub Essentials
Git tracks changes to your code so you can collaborate, experiment and undo mistakes.
Core concepts
| Term | Meaning |
|---|---|
| Repository | A project folder with its history |
| Commit | A snapshot of your changes with a message |
| Branch | An independent line of development |
| Remote | A hosted copy of the repository (for example on GitHub) |
| Pull request | A proposal to merge a branch, with review |
Everyday commands
git init # start a repository
git status # what changed?
git add src/app.js # stage a file (or `git add -p` to pick hunks)
git commit -m "Add login form validation"
git log --oneline --graph # history
git diff # unstaged changes
Branching workflow
git switch -c feature/search # create and switch
# ... edit, add, commit ...
git push -u origin feature/search
# open a pull request on GitHub, get review, then merge
git switch main
git pull
git branch -d feature/search
Merge vs rebase
| Merge | Rebase | |
|---|---|---|
| History | Keeps branch history with a merge commit | Replays commits for a linear history |
| Safe on shared branches | Yes | No: never rebase commits others have pulled |
Fixing mistakes
| Situation | Command |
|---|---|
| Discard changes in a file | git restore file.js |
| Unstage a file | git restore --staged file.js |
| Amend the last commit message | git commit --amend |
| Undo a pushed commit safely | git revert <commit> |
| Temporarily set work aside | git stash and git stash pop |
CAUTION
Never commit secrets (API keys, passwords, .env files). Add them to .gitignore first. If a secret is pushed, revoke it immediately: deleting the commit is not enough.
Good commit habits
- One logical change per commit; write the message in the imperative ("Fix", "Add").
- Pull before you push; open small pull requests.
- Use
.gitignorefornode_modules/, build output and local config.
Loading Comments...