git

🏠 Homepage 🏠 Syllabus of Day 2 ⬅️ Previous:Pushing Changes

4. Cloning a Repository

What is Cloning?

Cloning is the process of creating a local copy of a remote repository on your computer. This allows you to work on the project files, make changes, and use Git commands locally.

When to Use Cloning

Cloning Your Own Repo vs. a Forked Repo

How to Clone a Repository

Example: Cloning Your Fork

Suppose you have forked https://github.com/samyak-shrestha/git-repo-example to your own account.

  1. Copy the SSH or HTTPS URL from your fork:
    • SSH: git@github.com:<your-username>/git-repo-example.git
    • HTTPS: https://github.com/<your-username>/git-repo-example.git
  2. Run the clone command in your terminal:
    git clone git@github.com:<your-username>/git-repo-example.git
    
  3. Navigate into the cloned directory:
    cd git-repo-example
    
  4. (Optional) Add the Original Repo as Upstream for Syncing: This step helps you keep your fork up to date with the original repository.
    git remote add upstream git@github.com:samyak-shrestha/git-repo-example.git
    

Example: Cloning the Original Repo

If you have permission to contribute directly, you can clone the original repository:

git clone git@github.com:samyak-shrestha/git-repo-example.git
cd git-repo-example

Next Steps After Cloning

After cloning, you typically:

  1. Create and switch to a new branch:
    git checkout -b my-feature-branch
    
  2. Make changes to files in your editor.

  3. Stage your changes:
    git add .
    
  4. Commit your changes:
    git commit -m "Describe your changes"
    
  5. Push your branch to GitHub:
    git push origin my-feature-branch
    

Now you’re ready to open a pull request or continue working!


Summary Table

Scenario Command Example
Clone your fork (SSH) git clone git@github.com:<your-username>/git-repo-example.git
Clone your fork (HTTPS) git clone https://github.com/<your-username>/git-repo-example.git
Clone original repo git clone git@github.com:samyak-shrestha/git-repo-example.git

Tip:

➡️Up Next: 🍴 Forking a Repository