Cloning branch to new repo with command line (cli)

Certainly! If you want to clone a specific branch from another user's repository and push it to a new repository under your own GitHub account using the command-line interface (CLI), follow these general steps:


Prerequisites

  • Git Installed: Ensure that Git is installed on your system. You can download it from Git's official website.
  • GitHub Account: You should have a GitHub account and be logged in.
  • Permissions: Verify that the repository's license permits you to clone and use the code. Check for a LICENSE file or licensing information in the repository's documentation.

Step 1: Identify the Repository and Branch

  • Repository URL: Get the HTTPS or SSH URL of the repository you want to clone. For example:
    arduino
    Copy code
    https://github.com/username/repository.git
  • Branch Name: Note the name of the branch you want to clone (e.g., develop, feature-xyz).

Step 2: Clone the Specific Branch

Open your terminal or command prompt and execute the following commands:

bash
Copy code
# Clone the specific branch from the repository git clone -b branch-name --single-branch repository-url # Example: git clone -b develop --single-branch https://github.com/Fab-AF/elearn-ai.git
  • The -b flag specifies the branch name.
  • The --single-branch flag ensures only the specified branch is cloned.

Step 3: Navigate into the Cloned Repository

Change your directory to the cloned repository:

bash
Copy code
cd repository-name # Example: cd elearn-ai

Step 4: Remove the Original Remote

Since the cloned repository points to the original remote (origin), remove it to avoid pushing changes to the original repository:

bash
Copy code
# Remove the original 'origin' remote git remote remove origin

Step 5: Create a New Repository on GitHub

  1. Navigate to GitHub: Go to https://github.com/new to create a new repository.
  2. Repository Details:
    • Owner: Ensure your account (Sinvidia) is selected.
    • Repository Name: Enter a name (e.g., elearn-ai).
    • Description: Optionally, add a description.
    • Public/Private: Choose the visibility.
    • Initialize: Do not initialize with a README, .gitignore, or license.
  3. Create Repository: Click on "Create repository".

Step 6: Add Your New Repository as the Remote

Add your new GitHub repository as the origin remote:

bash
Copy code
# Add your GitHub repository as 'origin' git remote add origin https://github.com/YourUsername/YourRepository.git # Example: git remote add origin https://github.com/Sinvidia/elearn-ai.git

Step 7: Push the Branch to Your Repository

Push the cloned branch to your GitHub repository:

bash
Copy code
# Push the branch to your repository git push -u origin branch-name # Example: git push -u origin develop
  • The -u flag sets the upstream tracking, so future git push commands will know where to push.

Step 8: Verify the Code on GitHub

  1. Navigate to Your Repository: Go to https://github.com/YourUsername/YourRepository.
    • Example: https://github.com/Sinvidia/elearn-ai.
  2. Check the Branch: Make sure the branch you pushed is present and the files are as expected.

Optional: Push Other Branches or Tags

If you have other branches or tags you want to push, you can do so with:

bash
Copy code
# Push all branches git push -u origin --all # Push all tags git push origin --tags

Additional Considerations

  • Updating Documentation: Update any documentation to reflect changes in repository ownership or URLs.
  • Dependencies and Submodules: If the project uses submodules, ensure they are properly configured and pushed.
  • Licensing and Attribution: Include the original license and give appropriate credit if required.

Summary

You've successfully cloned a specific branch from another user's repository and pushed it to a new repository under your GitHub account using the CLI.


Example Commands Recap

Here's a summary of the commands with placeholders replaced:

bash
Copy code
# Clone the specific branch from the original repository git clone -b develop --single-branch https://github.com/Fab-AF/elearn-ai.git # Navigate into the repository cd elearn-ai # Remove the original remote git remote remove origin # Add your new repository as the remote git remote add origin https://github.com/Sinvidia/elearn-ai.git # Push the branch to your repository git push -u origin develop

Need Help?

If you encounter any issues or have questions during this process, feel free to ask for assistance!

Did you find this article useful?