Chitranjan
Chitranjan

Reputation: 67

How to transfer folder(s) from one repository to another repository?

I mistakenly created the second repo(Repo-2). I can easily delete this and again add the files in the existing repo(Repo-1). But I want to know if I can transfer the folder(folder-xyz) from Repo-2 to Repo-1

Also, if it matters, Default branch for Repo-1 is 'master' and for Repo-2 is 'main'.

Upvotes: 1

Views: 1222

Answers (1)

Marcin Kłopotek
Marcin Kłopotek

Reputation: 5981

  1. Open CLI in the directory with your local copy of the repository you want to transfer the folder-xyz directory to (Repo-1 in our case).

  2. Add additional remote pointing to the repository you want to transfer the folder-xyz directory from (Repo-2 in our case):

    git remote add Repo-2 <Repo-2-remote-reposiotry-url>
    
  3. Fetch the Repo-2 remote changes

    git fetch Repo-2
    
  4. Checkout only the changes related to the folder-xyz directory we want to transfer from Repo-2 main to the local Repo-1 master branch:

    git checkout Repo-2/main -- folder-xyz
    
  5. Examine the status to see if you did what was intended:

    git status --branch --short
    

    Now you should see all the files from folder-xyz added to git index (staging area).

  6. If all the changes look good just commit them:

    git commit --message "Add folder-xyz from Repo-2/main"
    

Upvotes: 2

Related Questions