Joshua L.
Joshua L.

Reputation: 35

Make git clone its own repository

This question may have already been asked, but after searching I cannot find what I am looking for.

I created a project and pushed it to github a while ago, and then decided to use that as a type of boiler plate for a new project. So I cloned it to my desktop and then completely re-designed it. After the project was finished I decided to create its own repo on github and try to push it up. This however ended up in my redesigned project reverting back to its original state. I was able to correct this by hitting CTRL Z on all my files (LOL). I need to find a way to completely separate the projects. When I try to push the original file to git hub, I get the following error:

"Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again."

To clarify, I do not want to merge or pull the two projects together, or end up deleting one of them. If it is possible I would like to find a way to make the cloned project its own, and then correct the error stated above for the original project.

Upvotes: 1

Views: 1499

Answers (1)

tomclegg
tomclegg

Reputation: 485

Say your boilerplate is at [email protected]:foo/boilerplate.git and you want that to be the basis of your new project [email protected]:foo/project.git.

On the github web site, create an empty repository called project.

Clone your boilerplate repo into a new directory called project.

git clone [email protected]:foo/boilerplate.git project

Git remembers that remote address so it can push/pull from there by default. But you don't want that: instead, from now on, you want git push to push to the project.git repo on github, not the boilerplate.git repo. git remote set-url is how you tell git that project.git is your new origin (i.e., the default remote for pushing and pulling).

cd project
git remote set-url origin [email protected]:foo/project.git

Now, git push will push to the project.git repo.

Optionally, add the boilerplate as a separate remote (but not origin, so you won't use it by accident):

git remote add boilerplate [email protected]:foo/boilerplate.git

Now, when you update your boilerplate, you can merge those changes into your project as well. (This can work well if the boilerplate files tend not to change very much in your project repo.)

git pull boilerplate master

To roll back the boilerplate.git repo to the point where you wanted it to diverge, you just need push -f. Two ways:

  1. If you still have a local copy of the boilerplate repo from before you accidentally pushed stuff to github, just go back there and run git push -f. This will remove the "work that you do not have locally" from the boilerplate repo on github -- but you've already got that stuff in a separate repo now, right? (You should think at least twice before doing git push -f. Git is trying to help you avoid deleting stuff by accident. Don't delete stuff by accident on purpose!)

  2. Clone boilerplate. Use git log to find the commit where you wish you had left it, say abc1234. Reset the github repo's "master" branch back to that commit by running git push -f origin abc1234:master.

To see where origin points to (and what other remotes you have), run git remote -v.

Upvotes: 7

Related Questions