Keeps history
Every change you save is stored. You can look back at old versions or undo mistakes any time.
Git is a tool that keeps track of every change you make to your files over time. It is called a version control system. Think of it like a save button that remembers every single version of your project, so you can always go back to an older version if something breaks.
Git runs on your own computer. It does not need the internet to work, because it saves everything in a hidden folder called .git inside your project.
Keeps history
Every change you save is stored. You can look back at old versions or undo mistakes any time.
Safe experiments
You can try new ideas in a separate “branch” without touching your main, working code.
Works with a team
Many people can work on the same project at the same time without overwriting each other’s work.
Backup and sharing
When you push your code to GitHub, it is safely stored online and easy to share with others.
GitHub is a website where you can store your Git projects (called repositories, or “repos” for short) online. It adds extra features on top of Git, like:
Git saves your project as a series of snapshots (called commits), not just a list of changes. Each commit points back to the commit before it, forming a chain of history.
.git folder on your computer.Install Git: Download it from the official Git website and follow the steps for your computer (Windows, macOS, or Linux).
Check that it installed correctly:
git --versionSet up your name and email: Git attaches your name and email to every commit you make, so people know who made each change.
git config --global user.name "Your Name"git config --global user.email "you@example.com"The --global flag means these settings apply to every project on your computer. If you want different settings for just one project, run the same commands without --global while inside that project’s folder.
Check your settings any time:
git config --listgit initThis turns the current folder into a Git repository by creating a hidden .git folder. From now on, Git will start tracking changes in this folder.
If a project already exists on GitHub, you can download a full copy, including all its history, using git clone.
git clone https://github.com/username/repository.gitThis creates a new folder on your computer with the same name as the repository, and downloads every file and every past commit.
This is the everyday flow you will use again and again: change a file, stage it, then commit it.
1. git status - Shows you which files have changed, which are staged, and which are not tracked yet.
git status2. git add <file> - Moves a file into the staging area, meaning “I want to save this change in my next commit.”
git add index.html# stage just one file
git add .# stage every changed file in the current folder3. git commit -m "message" - Saves a snapshot of everything in the staging area, along with a short message describing what changed.
git commit -m "Add homepage layout"4. git log - Shows the history of commits, from newest to oldest.
git log# shows full details for every commit
git log --oneline# shows a short, one-line summary for each commit5. git diff - Shows the exact lines that changed, before you stage them.
git diff# shows changes not yet staged
git diff --staged# shows changes that are staged, ready to commitMistakes happen. Git gives you several ways to undo them, depending on how far you’ve already gone.
git checkout -- <file># or, in newer Git versions:git restore <file>Throws away your unsaved changes in that file and brings back the last committed version.
git reset <file># or, in newer Git versions:git restore --staged <file>Removes the file from the staging area, but keeps your changes in the working directory.
git revert <commit id>Creates a new commit that undoes the changes from an older commit. This is the safest way to undo something that has already been pushed and shared with others.
git reset --hard <commit id>Moves your branch back to an older commit and throws away everything after it. Use this only on commits that you have not shared with anyone else yet.
A branch is like a separate copy of your project where you can try new things without touching your main, working code. The default branch is usually called main (older projects sometimes call it master).
1. git branch - Lists every branch in your project, and shows which one you’re currently on.
git branch2. git branch <name> - Creates a new branch, but does not switch to it yet.
git branch feature-login3. git checkout <name> or git switch <name> - Moves you onto a different branch.
git checkout feature-login# or, in newer Git versions:git switch feature-login4. Create and switch in one step:
git checkout -b feature-login# orgit switch -c feature-login5. git branch -d <name> - Deletes a branch that has already been merged.
git branch -d feature-loginOnce your work on a branch is ready, you need to bring it back into main. There are two common ways to do this.
git checkout maingit merge feature-loginCombines the two branches together and creates a new “merge commit” that joins their histories. This keeps the full history of both branches, exactly as it happened.
git checkout feature-logingit rebase mainTakes your branch’s commits and replays them on top of the latest main, making the history look like a single straight line, as if you had started your branch just now.
Sometimes Git cannot automatically combine two changes, because they both edited the same lines. This is called a merge conflict. Git will pause and mark the conflicting area in the file, like this:
<<<<<<< HEADThis is the version from your current branch.=======This is the version from the branch you're merging in.>>>>>>> feature-loginOpen the file and look for the <<<<<<<, =======, and >>>>>>> markers.
Edit the file so it has the final, correct content, and remove the marker lines.
Stage the fixed file:
git add <file>Finish the merge with a commit:
git commitA remote is simply a saved link to an online copy of your repository, like the one on GitHub.
1. git remote add <name> <url> - Connects your local project to an online repository.
git remote add origin https://github.com/username/repository.gitorigin is just a nickname - almost everyone uses origin as the standard name for the main remote.
2. git remote -v - Shows every remote connected to your project, and their URLs.
git remote -v3. git push - Uploads your commits to the remote repository.
git push origin main# uploads the "main" branch to the "origin" remote
git push -u origin main# same as above, but also remembers this pairing,# so next time you can just type "git push"4. git pull - Downloads new commits from the remote and combines them into your current branch.
git pull origin maingit pull is really just git fetch followed by git merge, done in one step.
5. git fetch - Downloads new commits from the remote, but does not merge them into your files yet.
git fetch originUsing SSH keys lets you connect to GitHub without typing your username and password every time.
Create an SSH key (skip this if you already have one):
ssh-keygen -t ed25519 -C "you@example.com"Copy your public key:
cat ~/.ssh/id_ed25519.pubAdd it to GitHub: Go to GitHub -> Settings -> SSH and GPG keys -> New SSH key, and paste the key you copied.
Test the connection:
ssh -T git@github.comClone repositories using the SSH link instead of the HTTPS one:
git clone git@github.com:username/repository.gitA .gitignore file tells Git which files and folders to never track or save. This is useful for things like passwords, build files, or huge folders like node_modules that don’t need to be saved in your project’s history.
# Ignore dependency foldersnode_modules/venv/
# Ignore environment and secret files.env.env.local
# Ignore build outputdist/build/
# Ignore log files*.log
# Ignore OS and editor files.DS_Store.vscode/Sometimes you need to quickly switch to a different branch, but you’re not ready to commit what you’re working on yet. git stash lets you save your unfinished changes for later, without committing them.
git stash# saves your uncommitted changes and cleans your working directory
git stash list# shows every stash you've saved
git stash pop# brings back the most recent stash, and removes it from the list
git stash apply# brings back the most recent stash, but keeps it in the list tooA tag marks a specific commit as important, usually to label a release version, like v1.0.0.
git tag v1.0.0# creates a tag on the current commit
git tag# lists every tag in the project
git push origin v1.0.0# uploads a single tag to GitHub
git push origin --tags# uploads every tag to GitHubA repository (or “repo”) is a project’s home on GitHub. It holds your code, its full history, and extra tools like issues and pull requests.
A fork is your own personal copy of someone else’s repository, made under your own GitHub account. You can freely make changes in your fork without affecting the original project.
A pull request is a request asking the project owner to pull your changes into their repository. It shows exactly what you changed, and lets other people leave comments, ask questions, or request changes before it gets merged.
Push your branch to GitHub:
git push origin feature-loginGo to the repository on GitHub. You’ll usually see a button to “Compare & pull request”.
Write a clear title and description explaining what you changed and why.
Submit the pull request, and wait for a review.
If changes are requested, make them, commit, and push again - the pull request updates automatically.
Once approved, the pull request gets merged into the main branch.
Issues are GitHub’s way of tracking bugs, feature requests, and to-do items for a project. Anyone with access can open an issue, discuss it in the comments, and link it to the pull request that fixes it.
Project owners can set up branch protection rules on important branches like main, so that:
GitHub Actions is GitHub’s built-in tool for automating tasks, like running tests, checking code style, or deploying your app, every time something happens in your repository (like a push or a pull request). This is often called CI/CD (Continuous Integration / Continuous Deployment).
Continuous Integration (CI)
Automatically test and check your code every time someone pushes changes, so bugs get caught early.
Continuous Deployment (CD)
Automatically deploy your app to a server or a website once your code passes all its checks.
Automation
Automate almost anything - labeling issues, sending notifications, running scheduled jobs, and more.
.github/workflows/ folder of your repository.Workflow files must be placed in this exact folder in your repository:
.github/workflows/Each .yml file inside this folder is its own separate workflow.
This example runs tests automatically every time someone pushes code, or opens a pull request, to the main branch.
# .github/workflows/ci.ymlname: Run Tests
on: push: branches: [main] pull_request: branches: [main]
jobs: test: runs-on: ubuntu-latest
steps: - name: Check out the code uses: actions/checkout@v4
- name: Set up Node.js uses: actions/setup-node@v4 with: node-version: "20"
- name: Install dependencies run: npm install
- name: Run tests run: npm testname: Run Tests - A friendly name for the workflow, shown on GitHub’s “Actions” tab.on: - Lists the events that trigger this workflow. Here, it runs on a push or a pull request to main.jobs: - Lists the jobs in this workflow. Here there is one job, named test.runs-on: ubuntu-latest - Says which type of virtual machine (runner) this job should use.steps: - The list of tasks this job runs, in order, from top to bottom.uses: - Runs a pre-built action made by GitHub or the community (here, actions/checkout downloads your repository’s code onto the runner, and actions/setup-node installs Node.js).run: - Runs a plain command line instruction, just like you would type in a terminal.on:)on: push: branches: [main]Runs the workflow every time someone pushes commits to the listed branch.
on: pull_request: branches: [main]Runs the workflow whenever a pull request is opened or updated, targeting the listed branch.
on: schedule: - cron: "0 0 * * *"Runs the workflow on a timer, using cron syntax. This example runs once a day, at midnight.
on: workflow_dispatch:Adds a manual “Run workflow” button on GitHub, so you can start the workflow yourself, whenever you want.
Never write passwords, API keys, or tokens directly inside a workflow file, since anyone who can see your repository could read them. Instead, store them as secrets.
Go to your repository on GitHub.
Open Settings -> Secrets and variables -> Actions.
Click New repository secret, give it a name (like API_KEY), and paste in the value.
Use it in your workflow like this:
steps: - name: Deploy app run: ./deploy.sh env: API_KEY: ${{ secrets.API_KEY }}A matrix lets you run the same job multiple times, automatically, with different settings each time - for example, testing your app on several versions of Node.js at once.
jobs: test: runs-on: ubuntu-latest strategy: matrix: node-version: [18, 20, 22]
steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm testThis example runs the whole job three separate times, once each for Node.js 18, 20, and 22, all at the same time.
By default, jobs run at the same time (in parallel). Use needs to make one job wait for another to finish first.
jobs: build: runs-on: ubuntu-latest steps: - run: echo "Building the app..."
deploy: needs: build runs-on: ubuntu-latest steps: - run: echo "Deploying the app..."Here, the deploy job will always wait for build to finish successfully before it starts.
Artifacts let you save files created during a job (like a build output) and share them with other jobs, or download them later from GitHub.
steps: - name: Build the app run: npm run build
- name: Upload build output uses: actions/upload-artifact@v4 with: name: build-output path: dist/name: Deploy
on: push: branches: [main]
jobs: deploy: runs-on: ubuntu-latest steps: - name: Check out code uses: actions/checkout@v4
- name: Set up Node.js uses: actions/setup-node@v4 with: node-version: "20"
- name: Install dependencies run: npm install
- name: Build the app run: npm run build
- name: Deploy run: ./deploy.sh env: DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}| Task | Command |
|---|---|
| Start tracking a new project | git init |
| Copy an existing project | git clone <url> |
| Check what changed | git status |
| Stage changes | git add <file> or git add . |
| Save a snapshot | git commit -m "message" |
| See history | git log --oneline |
| Create a branch | git branch <name> |
| Switch branches | git switch <name> |
| Create and switch at once | git switch -c <name> |
| Combine branches | git merge <branch> |
| Upload to GitHub | git push origin <branch> |
| Download from GitHub | git pull origin <branch> |
| Save unfinished work for later | git stash |
| Undo an unstaged change | git restore <file> |
| Undo a committed change (safely) | git revert <commit id> |