Monday, September 5, 2016

GIT: Creating a new repository from a branch in an existing repository [Command Line]

This guide is for those users who have an existing repository(both local and remote) with one or many branches. They now want to create a new repository using one of these branches in their pre-existing repos. Lets for the sake of discussion call it old_repo

Steps to follow:

  1. Create a remote repository using existing clients that you may have (GitHub, GitLab)
  2. Assuming the remote that the remote repo now exists successfully, lets call it new_repo
  3. Go to a location file system in your file system where you want this new_repo to exist locally
  4. Follow these instructions:
  5. mkdir /path/to/new_repo (eg. C/Users/Omar/git/new_repo)
  6. cd /path/to/new_repo
  7. git --bare init (This will initialize your repository)
  8. Now go to the place of the existing repo
  9. cd /path/to/old_repo (eg. C/Users/Omar/git/old_repo)
  10. Assume we want to intialize the new remote repo with a branch called feature1. Also assume we would name this as master in the new repo
  11. Also ensure that the remote repo you just created is accessible to you via either HTTP or SSH (you can check with your project settings in GitLab or GITHub).For simplicity's sake, I am calling this http://mygit/new_repo.git
  12. Follow the command below:
  13. git push http://mygit/new_repo.git +feature:master
  14. Viola! The git would now take care of the proceedings for you now with something like this:
Counting objects: 2820, done.
Delta compression using up to 4 threads.
Writing objects: 100% (2820/2820), 3.35 MiB | 259.00 KiB/s, done
Total 2820 (delta 1667), reused 2112 (delta 1167)

Note: Please note that the branch names should exist as well as the remote path should be correct and accessible. Otherwise you may encounter an error such as this one:

"error: src refspec old_branhc does not match any."

If all goes well, you should see a message of this sort:

[new branch]      feature1 -> master

If you found this helpful, your comments would be highly appreciated.

Reference:
With thanks to the answer posted on this link:

No comments:

Post a Comment