118

I cloned the repo at https://github.com/railstutorial/sample_app_rails_4 and made a lot of changes to it (I used it as a starting point for my own app), and now I would like to push the changed app to a repo on my own github account.

How can I change what github repo it is linked to?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
jackerman09
  • 2,492
  • 5
  • 29
  • 46
  • 1
    possible duplicate of [How to change a remote repository URI using Git?](http://stackoverflow.com/questions/2432764/how-to-change-a-remote-repository-uri-using-git) – deefour Aug 13 '13 at 03:44

13 Answers13

175

As Deefour says, your situation isn't much unlike the one in Change the URI (URL) for a remote Git repository. When you clone a repository, it is added as a remote of yours, under the name origin. What you need to do now (as you're not using the old source anymore) is change origin's URL:

$ git remote set-url origin http://github.com/YOU/YOUR_REPO

If the original repository would update often and you want to get those updates from time to time, then instead of editing origin it would be best to add a new remote:

$ git remote add personal http://github.com/YOU/YOUR_REPO

Or maybe even call the old one upstream:

$ git remote rename origin upstream
$ git remote add origin http://github.com/YOU/YOUR_REPO

Then, whenever you want to get changes from upstream, you can do:

$ git fetch upstream

As this the source is a sample repository (seems to be kind of a template to start off), I don't think there's a need to keep it nor fork it at all - I'll go with the first alternative here.

Community
  • 1
  • 1
mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
  • 1
    I did this originally (I tried both options at different times), but I kept getting an error saying I did not have access (I don't know the exact message b/c I'm not at my computer right now, but I can add it later). Thanks – jackerman09 Aug 13 '13 at 14:53
  • @jackerman09: whenever you can, please provide the error message so we can work it out. – mgarciaisaia Aug 13 '13 at 15:05
  • here's the error message I'm getting when I run git push to an existing repo: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. – jackerman09 Aug 13 '13 at 22:48
  • 1
    @jackerman09: [Verify the public key is attached to your GitHub account](https://help.github.com/articles/error-permission-denied-publickey#verify-the-public-key-is-attached-to-your-github-account) (or read the hole page, if that doesn't help). Or you can try [using HTTPS authentication](https://help.github.com/articles/changing-a-remote-s-url) instead. – mgarciaisaia Aug 14 '13 at 03:25
  • thanks for the help! The answer was that I had no ssh key on the computer I was using. Here are the sites I used to figure it all out: 1) https://help.github.com/articles/generating-ssh-keys AND 2) https://help.github.com/articles/error-permission-denied-publickey Thanks again! – jackerman09 Aug 14 '13 at 17:27
  • I'm late to the party. I've done all the steps above. The original repo is indeed updated very often, and I'm out of sync because I don't want to do a PULL. The original repo is named "upstream" now. I've added my own repo that I want to PUSH to as origin. The original branch is, say "version-13". My own branch is "main". I'm using "main". My GitHub repo is blank; no .gitignore, no README.md. Nothing. When I PUSH, I always get "error: src ref spec origin does not match any\n failed to push some refs to 'main'". Aiya... no idea how to get this PUSHed. – charleslcso Jun 18 '23 at 10:00
116

GitHub: git clone someone else's repository & git push to your own repository

I'm going to refer to someone else's repository as the other repository.


  1. Create a new repository at github.com. (this is your repository)
  • Give it the same name as the other repository.
  • Don't initialize it with a README, .gitignore, or license.
  1. Clone the other repository to your local machine. (if you haven't done so already)
  • git clone https://github.com/other-account/other-repository.git
  1. Rename the local repository's current 'origin' to 'upstream'.
  • git remote rename origin upstream
  1. Give the local repository an 'origin' that points to your repository.
  • git remote add origin https://github.com/your-account/your-repository.git
  1. Push the local repository to your repository on github.
  • git push origin main

Now 'origin' points to your repository & 'upstream' points to the other repository.

  • Create a new branch for your changes with git checkout -b my-feature-branch.
  • You can git commit as usual to your repository.
  • Use git pull upstream main to pull changes from the other repository to your main branch.
Derek Soike
  • 11,238
  • 3
  • 79
  • 74
  • 2
    Well explained +1 – Drenai Oct 10 '17 at 16:28
  • Can you include how (in addition to the above) you can make a pull request to "the other repository" ? This would make it a very helpful mini tutorial – Tanya Gupta Feb 07 '18 at 14:19
  • 1
    @TanyaGupta GitHub has a great tutorial page on [Creating a pull request from a fork](https://help.github.com/articles/creating-a-pull-request-from-a-fork/). – Derek Soike Feb 13 '18 at 19:46
  • Excellent explonation, brief, and simple – Stryker Jun 21 '19 at 21:38
  • 2
    Nice explanation but when trying to `git push origin master` I get: `... Compressing objects: 100% (1093/1093), done. Writing objects: 100% (4185/4185), 504.89 KiB | 126.22 MiB/s, done. Total 4185 (delta 3049), reused 4185 (delta 3049) remote: Resolving deltas: 100% (3049/3049), done. remote: pre-receive.sh: execution exceeded 5s timeout To https://github.com/your-account/your-repository.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://github.com/your-account/your-repository.git'` – user9074332 Jul 09 '19 at 17:08
  • ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/your-account/your-repo.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. – add787 Jan 21 '20 at 22:49
  • To avoid the remote-rejected silliness, you may need to change step 5 to use `git@github` instead of https. – Jonathan Mugan Apr 20 '22 at 21:01
19

Delete git and re-init.

Your purpose is probably to put this repo on yours and make it yours.

The idea is to delete the .git/ and re-initialize.

  1. go to your cloned repo folder rm -rf .git
  2. re-initialize it and then add your remote and do your first push.
    git init
    git add .
    git commit -m "your commit message"
    git remote add origin 
    git push origin master
    
MAOXU
  • 329
  • 2
  • 3
  • Do you even have to remove the `.git` directory first? Won't `git init` wipe it out anyway? – Mike Wise Jul 10 '19 at 17:27
  • 1
    @MikeWise: No, git init will never destroy any data if a repo already exists there, but can alter a few settings via the command-line args you pass, I think. – Agent Friday Oct 26 '19 at 03:24
  • By removing git, and reinit we will lost the commit history and multiple branches of the repository. I will not recommend it to anyone. – Muhammad Shifa Apr 03 '23 at 09:06
9

I think that the "most polite way" to do so would be:

  1. Fork the original repo on your GitHub account
  2. Checkout a new branch for your changes git checkout -b <your_branch_name> (in case you didn't do that before)
  3. Add a new remote for your local repository: git remote add github <your_repository_ssh_url>
  4. Push your beautiful new branch to your github repository: git push github <your_branch_name>

In this way you will have a repo forked to the original one, with your changes commited in a separate branch. This way will be easier in case you want to submit a pull request to the original repo.

llekn
  • 3,271
  • 2
  • 18
  • 23
  • 1
    Took me a minute to note that `github` in these instructions is the bit usually referred to as `origin`. Otherwise, straightforward and simple. – leanne Jan 05 '17 at 17:39
8

You can do this by creating a new remote from your local repository (via commandline).

git remote add <name> <url>

then you can call:

git push <name> <repo_name>

To replace the default "origin" remote that is set up you can run the following:

git remote rm origin
git remote add origin <url>
bmorgan21
  • 119
  • 3
  • I did this originally, but I kept getting an error saying I did not have access (I don't know the exact message b/c I'm not at my computer right now, but I can add it later). Thanks – jackerman09 Aug 13 '13 at 14:43
  • You may have to create the repository on your remote account first, then take that url to configure the new remote. – bmorgan21 Aug 13 '13 at 15:02
  • This is what I had done (creating the repo directly at github.com first). I reinitialized the local repo (git init), removed the remote's (rm origin) and re-added origin. That all worked, but when I tried to push to the repo I got the 'Fatal Error' saying 'Access Denied' – jackerman09 Aug 13 '13 at 15:41
  • try running "git config --list" and make sure you are using the right user and remotes. – bmorgan21 Aug 13 '13 at 17:36
  • I ran git config --list and the username, email and remote are correct. How can I confirm that my password is stored correctly? Can I re-enter the password? – jackerman09 Aug 14 '13 at 01:04
  • are you sure you don't have an ssh key set up? On invalid password (if it is using a password), it should prompt for the password. – bmorgan21 Aug 15 '13 at 18:35
  • thanks for following up. I got this working, it turns out I did not have an SSH key setup. Thanks again for the help – jackerman09 Aug 15 '13 at 23:51
1

Taken from Git push everything to new origin

basically you have to associate a new repo to your folder

git remote add origin <address>
git push origin <branchname>
Community
  • 1
  • 1
Eric C
  • 2,195
  • 1
  • 17
  • 23
1

I had a similar situation, but in my case what I just needed to do was, as suggested, but with https, like this:

$ git remote set-url origin https://github.com/YOU/YOUR_REPO

T J
  • 42,762
  • 13
  • 83
  • 138
1

After cloning, copy the files from their folder into a new one and start afresh with git init,

I had a similar problem like that I had to change the folder directory before I could stage the changes to my repo.

or you can remove current repository origin by the command git remote remove origin.

0

To save messing, I usually do this

  1. Clone it to a temp directory
  2. Create a new git repo with a README
  3. Clone that repo to my machine
  4. Delete all the git files from the original
  5. Copy all files to my repo
  6. Push it

Carry on as normal there and you can't be tripped up by issues with git. I do this fairly often. Certainly not as 'pure' as the other answers, but trauma free and removes any potential problems you may have.

0

Using vscode, these other solutions did not quite work for me,

Instead, I ran

git init

This will make it so that the 'new branch' button makes a completely new repository when you go to commit. I could not figure out how to commit into an already uninitialize repository.

edit : Since i was following a tutorial, i'm not very experienced with git, The flags were the culprit Remote rejected (shallow update not allowed) after changing Git remote URL

0

This worked for me as of December 2022. Straight forward

  1. I cloned the repo and then create a new repo on GitHub with a new name and no file

  2. I duplicated the folder and renamed it

  3. In the terminal (within the folder), rm -rf .git to del all existing git files

  4. I git init to initialize

  5. I git add -- . ': path' where the path was a directory with large weights I did not want to commit. It causes an error if you add all

  6. git commit -m "message"

  7. git remote add origin https://github.com/xxx/xxxx.git to newly created repo

  8. git branch -M main

  9. git push -u origin main

0

Simply:

git remote add origin https://github.com/your-username/name-of-repo.git
git branch -M main
git push -u origin main

UPD: Or master instead of main depends on the name of your master brunch.

dragomirik
  • 612
  • 2
  • 6
  • 13
-1

I have the same problem. If you just want the files in the repo and push them just as if they were normal files, you can..

  1. Download zip
  2. Open terminal (or cmd on windows)
  3. Delete the .github and .gitigore files (rm -r .github)(rm .gitigore)
  4. Copy and paste the directory into your project
    Github won't know that these are from another repo since the config files are gone.

It's doesn't have the hassle of messing too much with the command line and running into problems.

user18004704
  • 259
  • 2
  • 6