Git and GitHub

Track changes, branch, merge and collaborate through pull requests: the workflow every team expects on day one.

Beginner · about 6 hours · difficulty 2/5

Git records snapshots of your project so you can experiment safely, undo mistakes and work with others. GitHub (or GitLab, Bitbucket) hosts repositories and adds collaboration features around them.

The daily loop

git status to see what changed, git add to stage, git commit -m "..." to snapshot, git push to share, git pull to receive. Learn what the working tree, staging area and repository are; most confusion disappears once you picture those three places.

Branching and merging

  • Create a branch per feature or fix (git switch -c feature/login).
  • Merge combines histories with a merge commit; rebase replays your commits on top of another branch for a linear history. Never rebase commits others have already pulled.
  • Conflicts happen when two branches change the same lines. Read the markers, choose the right result, test, then commit.

Collaboration on GitHub

  • Fork or clone, branch, push, open a pull request, get review, address comments, merge.
  • Write commit messages that explain why (many teams use Conventional Commits like feat: and fix:).
  • Use .gitignore for node_modules, build output and .env files. Secrets committed once live in history forever.
  • Issues, project boards and GitHub Actions round out the workflow.

Getting out of trouble

git log --oneline, git diff, git restore, git revert (safe undo on shared branches), git reset (rewrites local history), git stash and git reflog (finds "lost" commits).

Move on when you can open a PR from a feature branch, resolve a merge conflict and revert a bad commit without panicking.

Key concepts

  • Working tree, staging and commits
  • Branches and switching
  • Merge vs rebase
  • Resolving conflicts
  • Remotes, push and pull
  • Pull requests and code review
  • .gitignore and secrets
  • revert, reset, stash and reflog
  • Conventional commits

Mini project

Put all your projects so far on GitHub; for one of them, make a feature branch, open a pull request with a clear description, deliberately create and resolve a conflict, and merge it.

Check yourself

When is git revert the right way to undo a commit, and when is git reset acceptable?

Free resources