36

I have a repo called react. I cloned it into a different repo locally called different-repo.

How can I then get different-repo to push remotely to different-repo because currently it is pushing to react.

Effectively I want to clone many times from react into different named repos but then when i push from those repos they push to their own repo.

The worm
  • 5,580
  • 14
  • 36
  • 49
  • `git remote add react ` – mic4ael Feb 07 '17 at 07:32
  • "...they push to their own repo"? I don't understand that. – Christoph Feb 07 '17 at 07:38
  • Can you please edit your question properly. – Shivkumar kondi Feb 07 '17 at 07:40
  • @Christoph it makes sense because I clone from repoA into repoB but when I push a commit from repoB it pushes to repoA because repoB is basically repoA but with a different name. but I want to clone off repoA many times as it is boilerplate code and then I want to push to each individual repo remotely...make sense? – The worm Feb 07 '17 at 08:03

4 Answers4

74

You have to add an other remote. Usually, you have an origin remotes, which points to the github (maybe bitbucket) repository you cloned it from. Here's a few example of what it is:

  • https://github.com/some-user/some-repo (the .git is optional)
  • git@github.com:some-user/some-repo (this is ssh, it allows you to push/pull without having to type your ids every single time)
  • C:/some/folder/on/your/computer Yes! You can push to an other directory on your own computer.

So, when you

$ git push origin master

origin is replaced with it's value: the url

So, it's basically just a shortcut. You could type the url yourself each time, it'd do the same!

Note: you can list all your remotes by doing git remote -v.

For your problem

How can I then get different-repo to push remotely to different-repo because currently it is pushing to react.

I'm guessing you want to create a second repository, right? Well, you can create an other remote (or replace the current origin) with the url to this repo!

Add an other remote — recommended

git remote add <remote-name> <url>

So, for example:

$ git remote add different-repo https://github.com/your-username/your-repo

And then, just

$ git push different-repo master

Change the origin remote

git remote set-url <remote-name> <url>

So

git remote set-url origin https://github.com/your-username/your-repo
math2001
  • 4,167
  • 24
  • 35
  • Do you know if there is a way to have different exclusions for different remotes? For example, pushing identical code to two separate repos each with a different README.md. I want each readme in my local repo for editing but need each remote to be unique. – jacob Dec 02 '20 at 21:17
4

Here different-repo is the first repo from which you created/cloned the child repo react

So by default child repo react will have its default remote as different-repo where you can push/pull changes.

Here child repo will maintain all the commit history of parent repo within its .git folder

If you want to push the changes to different repo from this react repo then add another remote(you can add as many as remotes here and also can delete the old remotes)

Add new Remote to react

git remote add <remote-name> <url>

If you want to remove the old remote

git remote remove <remote_name>
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
4

Git push to new repo from existing repo’s branch

This blog is to the point and explains it very well. Here is the snippet from the blog.

Go to current project: 
$ cd my-project

Add new origin (origin2): git remote add origin2 <git_url>
$ git remote add origin2 https://github.com/my-org/new-project

The following command pushes master branch of current repo to master branch of new repo with remote configured as origin2.
$ git push <remote_name> <remote_repo_branch>
$ git push origin2 master

The following command pushes specific branch (say dev) of current repo to master branch of new repo with remote configured as origin2.
$ git push origin2 <source_branch>:<destination_branch>
$ git push origin2 dev:master
Use --force (to forcefully push into that new branch if required)
$ git push origin2 <source_branch>:<destination_branch> --force
Mahima
  • 132
  • 9
0

Note that if your current repo's branch that you want to push is called master and the remote repo branch where you push is called main, you should do git push different-repo master:main

Ilir
  • 488
  • 5
  • 20