Git From Zero: Your First Repository to Your First Pull Request
Set up Git, make your first commit, push it to GitHub, work on a branch, and open a pull request. Includes the two things beginners hit first: a leaked .env and a merge conflict.
Prerequisites
- Git installed (git --version should answer)
- A GitHub account
- An SSH key added to GitHub, from the SSH Keys From Scratch guide
Set Your Git Name and Email
Use the email your GitHub account knows, otherwise your commits will not be linked to your profile.
Initialize a New Repository
Run this inside your project folder. It creates a hidden .git directory and changes nothing else.
Do this before the first commit. A secret committed once stays in history even after you delete the file.
Create a .gitignore Before the First Commit
Check Status and Recent Log
Get into the habit of running this before and after every command here. It is how you learn what Git just did.
Stage and Commit Changes
A commit is a save point with a message. Write the message for the person who reads it in six months, which is usually you.
Add a Remote Repository
Create the empty repository on GitHub first, then point your local one at it. Use the SSH address so your key does the authenticating.
Push Branch to Remote
The -u flag links local to remote once, so later you can just type git push.
Create and switch to new branch
Work on a branch, never directly on main. It keeps main deployable while your change is unfinished.
Open the pull request
# ارفع الفرع، ثم افتح المستودع على GitHub: # سيظهر شريط "Compare & pull request" # # أو من الطرفية إن كان GitHub CLI مثبتاً: gh pr create --fill
A pull request is a request to merge your branch, plus a place to discuss it. Even working alone it gives you one last read of your own diff.
Pull with Rebase
Before merging, bring in what others pushed. Rebase replays your commits on top so the history stays a straight line.
Resolve a Merge Conflict
It will happen. Two people changed the same lines and Git will not guess which wins.
Undo Last Git Commit (Keep Changes)
The everyday undo: keeps your changes, drops the commit, lets you redo the message.
Recover a Lost Commit with reflog
The safety net worth knowing exists: Git remembers where every branch pointed, so almost nothing is truly lost.