Developer Tools
Git & GitHub
Version control for every project: repositories, the staging area, commits, history, branches and merging, and sharing work on GitHub with push, pull, clones and pull requests.
Git is a version control system: it records snapshots of your project over time so you can see what changed, go back to any earlier version, and work with others without overwriting each other's work. GitHub is a website that hosts Git repositories online — and doubles as your public developer portfolio.
Why version control?
- Never lose work — every commit is a restore point.
- Understand history — who changed what, when, and why.
- Experiment safely — try ideas on a branch; throw them away if they fail.
- Collaborate — many people work on one codebase in parallel.
The three areas
| Area | What lives there |
|---|---|
| Working directory | the files you are editing right now |
| Staging area (index) | changes you have marked for the next commit |
Repository (.git folder) |
the permanent history of commits |
First-time setup
git config --global user.name "Asha Rao"
git config --global user.email "asha@example.com"
git config --global init.defaultBranch mainStarting and saving work
git init # turn the current folder into a repository
git status # what changed? what is staged?
git add main.c # stage one file
git add . # stage everything in this folder
git commit -m "Add linked list insert and delete"
git log --oneline # history, one line per commit
git diff # unstaged changes, line by line.gitignore
List files Git should never track — compiled programs, secrets, editor clutter:
# .gitignore
a.out
*.o
*.exe
__pycache__/
.env
node_modules/
.vscode/Undoing things
git restore main.c # discard unstaged edits to a file
git restore --staged main.c # unstage, keeping the edits
git commit --amend # fix the last commit's message or contents
git revert a1b2c3d # make a new commit that undoes an old one
git checkout a1b2c3d -- notes.txt # bring back an old version of one fileBranches
A branch is an independent line of development. main holds working code; you create a branch for each feature or fix, then merge it back.
git branch # list branches
git switch -c feature/stack # create and switch to a new branch
# ... edit and commit on the branch ...
git switch main # go back to main
git merge feature/stack # bring the branch's commits into main
git branch -d feature/stack # delete the merged branchIf both branches changed the same lines, Git reports a merge conflict and marks the file:
<<<<<<< HEAD
printf("Hello\n");
=======
printf("Hello, world!\n");
>>>>>>> feature/greetingEdit the file to keep the right version, delete the markers, then git add and git commit.
Working with GitHub
git clone https://github.com/user/repo.git # download a repository
git remote add origin https://github.com/you/project.git
git push -u origin main # upload your commits (first time)
git push # later pushes
git pull # fetch and merge others' changesPull requests (PRs): on GitHub, push your branch, then open a pull request asking to merge it into main. Teammates review the changes and comment before merging — the standard workflow in every software company.
Forks: copy someone else's repository to your account, change it, then open a PR back to the original — how open-source contributions work.
| Command | Purpose |
|---|---|
git init / git clone |
start a repository / copy one |
git status, git diff, git log |
inspect |
git add, git commit |
save a snapshot |
git switch -c, git merge |
branch and merge |
git push, git pull |
sync with GitHub |
git restore, git revert |
undo |
Test yourself
4 questions · pick an answer to check it instantly
- 1
Which command moves changes into the staging area?
addstages;commitrecords the staged snapshot. - 2
What does
git pushdo?pulldownloads;pushuploads. - 3
Which file tells Git which files to ignore?
Patterns listed in.gitignoreare never tracked. - 4
What is a pull request?
PRs are how teams review code before merging.