APEX Educational Institute

Git and GitHub for Beginners: Essential Commands with Examples

Learn Git from zero: repositories, commits, branches, merging, pull requests and fixing common mistakes, with the exact commands developers and testers use every day.

Beginner | 3 min read | Updated

Git is a version control system: it records every change to your code so you can go back, compare versions and work with a team without overwriting each other's work. GitHub (like GitLab or Bitbucket) hosts Git repositories online and adds pull requests, code review and CI/CD.

Every developer, tester and DevOps engineer uses Git daily.

One-time setup

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

The three areas

AreaMeaning
Working directoryFiles you are editing
Staging areaChanges selected for the next commit (git add)
RepositorySaved history of commits (git commit)

Your first repository

bash
mkdir my-project && cd my-project
git init
echo "# My Project" > README.md
git status
git add README.md
git commit -m "Add README"
git log --oneline

Write commit messages that say what changed and why, in the imperative: "Add login validation", not "changes".

Daily commands

bash
git status                 # what changed?
git diff                   # exact line changes not yet staged
git add .                  # stage everything in this folder
git commit -m "Fix fee calculation for discounts"
git pull                   # get the latest from the remote
git push                   # send your commits to the remote

Branches: work without breaking main

bash
git switch -c feature/login-page   # create and move to a new branch
# ...edit files, add, commit...
git push -u origin feature/login-page

Then open a Pull Request (PR) on GitHub. Teammates review the code, CI runs the tests, and when everything passes the branch is merged into main.

Keep branches small and short-lived. A PR with 200 changed lines gets a careful review; one with 3,000 lines gets a rubber stamp.

Merging and conflicts

bash
git switch main
git pull
git merge feature/login-page

A merge conflict happens when two branches changed the same lines. Git marks the file:

text
<<<<<<< HEAD
const fee = 40000;
=======
const fee = 42000;
>>>>>>> feature/fee-update

Edit the file to keep the correct version, remove the markers, then:

bash
git add pricing.js
git commit

Connect to GitHub

bash
git remote add origin https://github.com/your-name/my-project.git
git push -u origin main

Undoing mistakes safely

SituationCommand
Discard unstaged changes in a filegit restore file.txt
Unstage a filegit restore --staged file.txt
Fix the last commit message (not yet pushed)git commit --amend -m "Better message"
Undo a pushed commit safelygit revert <commit-id>
Save work temporarilygit stash, later git stash pop

Avoid git push --force on shared branches; it rewrites history for everyone. If you must, use --force-with-lease.

.gitignore: never commit secrets

Create a .gitignore file so passwords, keys and build output never enter the repository:

text
node_modules/
.env
.env.local
*.log
target/

If a secret is ever pushed, consider it leaked: rotate the key immediately, even after deleting the commit.

Interview questions

  • `git fetch` vs `git pull`? fetch downloads changes without merging; pull = fetch + merge (or rebase).
  • `merge` vs `rebase`? Merge keeps history as it happened with a merge commit; rebase replays your commits on top of the target for a linear history.
  • `revert` vs `reset`? revert adds a new commit that undoes a change (safe for shared branches); reset moves the branch pointer and can discard history.

Next steps

Create a GitHub repository for your projects and practise the branch and PR flow daily. Then automate builds with Docker and CI/CD in the DevOps + AI course.

Master it hands-on

Cloud + AI

DevOps + AI (AIOps)

Linux, Git, CI/CD, Docker, Kubernetes, Terraform, cloud and AIOps.

16 weeks Beginner to Advanced
Online LiveRecorded Course

More Cloud + AI tutorials