Skip to main content

Git and GitHub Essentials

Git tracks changes to your code so you can collaborate, experiment and undo mistakes.

Core concepts​

TermMeaning
RepositoryA project folder with its history
CommitA snapshot of your changes with a message
BranchAn independent line of development
RemoteA hosted copy of the repository (for example on GitHub)
Pull requestA 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​

MergeRebase
HistoryKeeps branch history with a merge commitReplays commits for a linear history
Safe on shared branchesYesNo: never rebase commits others have pulled

Fixing mistakes​

SituationCommand
Discard changes in a filegit restore file.js
Unstage a filegit restore --staged file.js
Amend the last commit messagegit commit --amend
Undo a pushed commit safelygit revert <commit>
Temporarily set work asidegit 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 .gitignore for node_modules/, build output and local config.