git clone is a Git operation

  • copy a repository from a remote server to local machine

0. Go to the Folder Where You Want to Store the Project

  • Before cloning, first move to the directory where you want the repository to live.
cd ~/Desktop/projects
mkdir -p ~/Desktop/projects  
cd ~/Desktop/projects

1. Clone the repository

This copies the existing project to your local machine.

git clone <repository-url>

2. Create a new branch

Work on your own branch, not on main.

git status
git checkout -b your-branch-name
git branch
  • git checkout -b alan/explore-repo

Scenario: If original repo updated

If the original repo gets updated, you usually want to bring the latest changes from main into your branch.

Option 1: If you have not started much work yet

git checkout main  
git pull origin main  
git checkout -b <your-branch-name> # -b: create a new branch
# git checkout <your-branch-name>
git merge main

This means:

  1. go to main
  2. pull the newest code
  3. go back to your branch
  4. merge the updated main into your branch

Option 2: If you want a cleaner history

git checkout main  
git pull origin main  
git checkout alan/explore-repo  
git rebase main
A---B---C   main
     \
      D---E   your-branch

If you run: git rebase main

  • Git does this idea:
    1. find your commits (D, E)
    2. move your branch pointer to C
    3. replay D and E on top of C

So it becomes:

A---B---C   main
         \
          D'---E'   your-branch