I will be creating git repository for a website that I am creating now. 

Prerequisites

  1. Download git in https://git-scm.com/downloads
  2. GitHub account created at github.com

Basic Git commands

  1. Initialize Git in a project: git init
  2. Check file status: git status
  3. Stage changes: git add .
  4. Commit changes: git commit -m "changes"
  5. Push code to GitHub: git push origin main
  6. View commit history: git log
  7. Create a new branch: git branch < branch name>
  8. Delete a branch: git branch -d <branch name>
  9. Switch to a branch: git checkout < branch

Steps

Initialize Git and configure username and email

  1. Go to the folder where you want to have version control.
    1. grace@Mac React Full Stack % pwd     
      /Users/grace/Documents/React Full Stack
      grace@Mac React Full Stack % 
  2. Execute git init. You will notice a .git file after
    1. grace@Mac React Full Stack % git init
      Initialized empty Git repository in /Users/grace/Documents/React Full Stack/.git/
      grace@Mac React Full Stack % ls -al
      total 0
      drwxr-xr-x@  4 grace  staff   128 Apr 13 18:50 .
      drwx------@ 69 grace  staff  2208 Apr 12 21:58 ..
      drwxr-xr-x@  9 grace  staff   288 Apr 13 18:50 .git
      drwxr-xr-x@ 15 grace  staff   480 Apr 12 22:02 front-end
      grace@Mac React Full Stack % 
  3. First, check the initial configuration. If you don't set user.name and user.email, Git will let you connect to GitHub, but when you try to commit it will give you a warning like:
    1. *** Please tell me who you are.
      
      Run
      
        git config --global user.email "you@example.com"
        git config --global user.name "Your Name"
      
      to set your account's default identity.

      ✅ This is needed for commit identity. No connection to GitHub yet. Git needs your name and email to save inside the commit metadata!

  4. You can add the username and email using:
    1. git config --global user.name johndoe
      git config --global user.email johndoe@youremail.com
    2. Then recheck again in git config --list    or git config --global --list
  5. Those configurations will be save to ~/.gitconfig. You can also use git config --list to check it.
    1. Checking config in ~/.gitconfig

      [filter "lfs"]
              process = git-lfs filter-process
              required = true
              clean = git-lfs clean -- %f
              smudge = git-lfs smudge -- %f
      [user]
              name = johndoe
              email = johndoe@youremail.com
                                                                                                                                                                
      "~/.gitconfig" 8L, 191B
    2. Checking config using git config --list    
      1. grace@Mac React Full Stack % git config --list            
        credential.helper=osxkeychain
        init.defaultbranch=main
        filter.lfs.process=git-lfs filter-process
        filter.lfs.required=true
        filter.lfs.clean=git-lfs clean -- %f
        filter.lfs.smudge=git-lfs smudge -- %f
        user.name=johndoe
        user.email=johndoe@youremail.com
        core.repositoryformatversion=0
        core.filemode=true
        core.bare=false
        core.logallrefupdates=true
        core.ignorecase=true
        core.precomposeunicode=true

Setup Github Token for Authentication

Since GitHub does not allow password login anymore for Git commands, you need a Personal Access Token.


Creating repository remotely

  1. Install GitHub CLI (gh)
    1. Mac (Homebrew):

      brew install gh
    2. Ubuntu Linux

      sudo apt install gh
    3. Windows: Download installer from https://cli.github.com/
    4. Check if installed. You should see gh version output:

      gh --version
      
    5.  
  2. Authenticate GitHub CLI using Personal Access Token

    gh auth login
    
    1. Select GitHub.com
    2. Select HTTPS
    3. It will ask:

      Authenticate Git with your GitHub credentials?

      ✅ Say Yes

    4. When asked about login:
      1. Choose Paste authentication token
      2. Paste your Personal Access Token you generated from GitHub.
    5. ✅ Now gh is connected securely to your GitHub account.

      grace@Mac React Full Stack % gh --version
      gh version 2.54.0 (2024-07-31)
      https://github.com/cli/cli/releases/tag/v2.54.0
      grace@Mac React Full Stack % gh auth login
      ? What account do you want to log into? GitHub.com
      ? What is your preferred protocol for Git operations on this host? HTTPS
      ? Authenticate Git with your GitHub credentials? Yes
      ? How would you like to authenticate GitHub CLI? Paste an authentication token
      Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
      The minimum required scopes are 'repo', 'read:org', 'workflow'.
      ? Paste your authentication token: ****************************************

     


Create Remote GitHub Repository from Local Machine

  1. Create a repo directly from your terminal

    gh repo create react-blog --private --source=. --remote=origin --push
  2. Internally, without using gh command, this is the code it run

    git remote add origin https://github.com/yourusername/my-blog.git
    git push -u origin main
    

    ✅ But you didn’t need to type those yourself because the GitHub CLI (gh) did it automatically.

    How to see it?

    - using git remote -v

    grace@Mac React Full Stack % git remote -v
    origin  https://github.com/grace188031/react-blog.git (fetch)
    origin  https://github.com/grace188031/react-blog.git (push)

    ✅ This confirms:

    • origin is set
    • It points to your GitHub repository
    • Git knows where to push and pull from

    Summary

    • ActionOld Manual WayNew Way with gh CLI
      Set remote manuallygit remote add origin <url>gh repo create auto does it
      Push manuallygit push -u origin maingh repo create can push first commit too
  3. Tried to execute the command but said no commit yet found. Hence, I will try to do my first commit first locally

    grace@Mac React Full Stack % gh repo create react-blog --private --source=. --remote=origin --push
    `--push` enabled but no commits found in /Users/marygracezabala/Documents/React Full Stack
    
    grace@Mac React Full Stack % git add .
    grace@Mac React Full Stack % git commit -m "End of Chapter 1: Creating a React-Front-End"
    [main (root-commit) 12d2b4b] End of Chapter 1: Creating a React-Front-End
     26 files changed, 3828 insertions(+)
     create mode 100644 front-end/.gitignore
     create mode 100644 front-end/README.md
     create mode 100644 front-end/eslint.config.js
     create mode 100644 front-end/index.html
     create mode 100644 front-end/package-lock.json
     create mode 100644 front-end/package.json
     create mode 100644 front-end/public/vite.svg
     create mode 100644 front-end/src/App.css
     create mode 100644 front-end/src/App.tsx
     create mode 100644 front-end/src/ArticlesList.tsx
     create mode 100644 front-end/src/Layout.tsx
     create mode 100644 front-end/src/NavBar.tsx
     create mode 100644 front-end/src/article-content.tsx
     create mode 100644 front-end/src/assets/react.svg
     create mode 100644 front-end/src/index.css
     create mode 100644 front-end/src/main.tsx
     create mode 100644 front-end/src/pages/AboutPage.tsx
     create mode 100644 front-end/src/pages/ArticlePage.tsx
     create mode 100644 front-end/src/pages/ArticlesListPage.tsx
     create mode 100644 front-end/src/pages/HomePage.tsx
     create mode 100644 front-end/src/pages/NotFoundPage.tsx
     create mode 100644 front-end/src/vite-env.d.ts
     create mode 100644 front-end/tsconfig.app.json
     create mode 100644 front-end/tsconfig.json
     create mode 100644 front-end/tsconfig.node.json
     create mode 100644 front-end/vite.config.ts
    grace@Mac React Full Stack % 
    grace@Mac React Full Stack % gh repo create react-blog --private --source=. --remote=origin --push
    ✓ Created repository grace188031/react-blog on GitHub
      https://github.com/grace188031/react-blog
    ✓ Added remote https://github.com/grace188031/react-blog.git
    Enumerating objects: 33, done.
    Counting objects: 100% (33/33), done.
    Delta compression using up to 12 threads
    Compressing objects: 100% (29/29), done.
    Writing objects: 100% (33/33), 35.61 KiB | 11.87 MiB/s, done.
    Total 33 (delta 1), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (1/1), done.
    To https://github.com/grace188031/react-blog.git
     * [new branch]      HEAD -> main
    branch 'main' set up to track 'origin/main'.
    ✓ Pushed commits to https://github.com/grace188031/react-blog.git
    marygracezabala@Mac React Full Stack % 
  4. I successfully created my remote repository. Initially, this is private while we I am developing sample react blog site

    Explanation:

    • my-blog → your GitHub repo name
    • --private → or use --public
    • --source=. → current folder
    • --remote=origin → set remote name origin
    • --push → push immediately after creating
  5. Summary of commands
# Configure Git identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Authenticate GitHub CLI
gh auth login

# Create project
mkdir my-blog && cd my-blog
git init

# Create remote repo and push
gh repo create my-blog --private --source=. --remote=origin --push

# Normal Git work
echo "# My Blog" > README.md
git add .
git commit -m "First commit"
git push

Create another branch locally

I want to separate my learningsper Chapter. Hence I will create another branch named Chapter 1

  1. Create and switch to a new branch locally

    grace@Mac React Full Stack % git checkout -b chapter-1
    Switched to a new branch 'chapter-1'
    

    ✅ This creates a branch called chapter-1 and moves you to it.

    (You can check you're on the right branch anytime with:)

    grace@Mac React Full Stack % git branch
    * chapter-1
      main

    (* means you're currently on chapter-1.)

  2. I will add Chapter1.md in my chapter-1 branch

    grace@Mac React Full Stack % git branch
    * chapter-1
      main
    grace@Mac React Full Stack % echo "Chapter 1: Creating a React-Front-End" > chapter1.md
    grace@Mac React Full Stack % git add chapter1.md
    grace@Mac React Full Stack % git commit -m "Add Chapter 1 read me content" 
    [chapter-1 aded723] Add Chapter 1 read me content
     1 file changed, 1 insertion(+)
     create mode 100644 chapter1.md
  3. Push the chapter-1 branch to GitHub

    grace@Mac React Full Stack % git push -u origin chapter-1
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 12 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 335 bytes | 335.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    remote: 
    remote: Create a pull request for 'chapter-1' on GitHub by visiting:
    remote:      https://github.com/grace188031/react-blog/pull/new/chapter-1
    remote: 
    To https://github.com/grace188031/react-blog.git
     * [new branch]      chapter-1 -> chapter-1
    branch 'chapter-1' set up to track 'origin/chapter-1'.

    Explanation:

    • push → uploads your branch
    • -u → sets upstream, so future git push or git pull will automatically know you mean origin/chapter-1
    • origin → your GitHub remote
    • chapter-1 → branch name

       

  4. Summary of commands
git checkout -b chapter-1
git add .
git commit -m "Work on Chapter 1"
git push -u origin chapter-1

Creating branch  chapter1-basic-css-design, Edit, Commit then Push

  1. Create and switch to new branch

    git checkout -b chapter1-basic-css-design
  2. Delete the chapter1.md and create a file chapter1-css.md

    grace@Mac React Full Stack % git branch
      chapter-1
    * chapter1-basic-css-design
      main
    grace@Mac React Full Stack % rm chapter1.md
    grace@Mac React Full Stack % echo "# Chapter 1: Basic CSS Design
    
    In this chapter, we introduce basic CSS concepts:
    - Styling text
    - Adding background colors
    - Adjusting margins and padding
    " > chapter1-css.md
    grace@Mac React Full Stack % 
  3. Now stage the chapter1-css for commit

    grace@Mac React Full Stack % git add chapter1-css.md chapter1.md
    grace@Mac React Full Stack % git status
    On branch chapter1-basic-css-design
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   chapter1-css.md
            deleted:    chapter1.md
    
    
    grace@Mac React Full Stack % git commit -m "Add Chapter 1 Basic CSS Design content"
    
    [chapter1-basic-css-design 2ff32ca] Add Chapter 1 Basic CSS Design content
     1 file changed, 7 insertions(+), 1 deletion(-)
     create mode 100644 chapter1-css.md
     delete mode 100644 chapter1.md
    marygracezabala@Mac React Full Stack % 
  4. Push the branch to Github

    marygracezabala@Mac React Full Stack % git push -u origin chapter1-basic-css-design
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 12 threads
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 459 bytes | 459.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    remote: 
    remote: Create a pull request for 'chapter1-basic-css-design' on GitHub by visiting:
    remote:      https://github.com/grace188031/react-blog/pull/new/chapter1-basic-css-design
    remote: 
    To https://github.com/grace188031/react-blog.git
     * [new branch]      chapter1-basic-css-design -> chapter1-basic-css-design
    branch 'chapter1-basic-css-design' set up to track 'origin/chapter1-basic-css-design
  5. Summary

    git checkout -b chapter1-basic-css-design 
    # (edit or create chapter1.md file)
    git add chapter1-css.md chapter1.md
    git commit -m "Add Chapter 1 Basic CSS Design content"
    git push -u origin chapter1-basic-css-design

Using Git Diff to differentiate two branches

  1. Here is an example

    grace@Mac React Full Stack % git diff chapter-1 chapter1-basic-css-design
    diff --git a/chapter1-css.md b/chapter1-css.md
    new file mode 100644
    index 0000000..032def8
    --- /dev/null
    +++ b/chapter1-css.md
    @@ -0,0 +1,7 @@
    +# Chapter 1: Basic CSS Design
    +
    +In this chapter, we introduce basic CSS concepts:
    +- Styling text
    +- Adding background colors
    +- Adjusting margins and padding
    +
    diff --git a/chapter1.md b/chapter1.md
    deleted file mode 100644
    index 8b53f46..0000000
    --- a/chapter1.md
    +++ /dev/null
    @@ -1 +0,0 @@
    -Chapter 1: Creating a React-Front-End
  2. Now for example, you already execute git add .(adds all files you added or modified) and you want to see the changes first before committing, you can execute git diff --staged. Please see example below

    grace@Mac React Full Stack % git diff --staged
    grace@Mac React Full Stack % git add .        
    grace@Mac React Full Stack % git diff --staged
    diff --git a/front-end/src/App.css b/front-end/src/App.css
    index b9d355d..6081cc6 100644
    --- a/front-end/src/App.css
    +++ b/front-end/src/App.css
    @@ -1,42 +1,5 @@
     #root {
    -  max-width: 1280px;
    +  max-width: 800px;
       margin: 0 auto;
       padding: 2rem;
    -  text-align: center;
    -}
    -
    -.logo {
    -  height: 6em;
    -  padding: 1.5em;
    -  will-change: filter;
  3. Here are useful commands you can use
Command	What it shows
git diff	Changes not yet staged (not added)
git diff --staged or git diff --cached	Changes that are staged (already added but not yet commited)
git diff main chapter1-basic-css-design	Differences between two branches
git diff HEAD	Difference between current files and latest commit