Command Library
0%
Beginner ~30 min

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.

0 / 13 done

Prerequisites

  • Git installed (git --version should answer)
  • A GitHub account
  • An SSH key added to GitHub, from the SSH Keys From Scratch guide
1
Step 1 Snippet

Set Your Git Name and Email


                            

Use the email your GitHub account knows, otherwise your commits will not be linked to your profile.

2
Step 2 Snippet

Initialize a New Repository


                            

Run this inside your project folder. It creates a hidden .git directory and changes nothing else.

3
Step 3 Snippet

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


                            
Run git rm --cached .env and commit that, which stops future tracking. Then rotate every credential inside it: the old values are still readable in the history, and on a public repository assume they were harvested within minutes.
4
Step 4 Snippet

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.

5
Step 5 Snippet

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.

6
Step 6 Snippet

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.

7
Step 7 Snippet

Push Branch to Remote


                            

The -u flag links local to remote once, so later you can just type git push.

8
Step 8 Snippet

Create and switch to new branch


                            

Work on a branch, never directly on main. It keeps main deployable while your change is unfinished.

9
Step 9 Command

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.

10
Step 10 Snippet

Pull with Rebase


                            

Before merging, bring in what others pushed. Rebase replays your commits on top so the history stays a straight line.

11
Step 11 Snippet

Resolve a Merge Conflict


                            

It will happen. Two people changed the same lines and Git will not guess which wins.

Yes. git merge --abort or git rebase --abort puts the branch back exactly as it was. Nothing is lost, so experiment without fear.
12
Step 12 Snippet

Undo Last Git Commit (Keep Changes)


                            

The everyday undo: keeps your changes, drops the commit, lets you redo the message.

13
Step 13 Snippet

Recover a Lost Commit with reflog


                            

The safety net worth knowing exists: Git remembers where every branch pointed, so almost nothing is truly lost.