0

Let me start by saying I have reviewed similar questions to this, but I think the facts here are slightly different than in those questions.

I am doing courses on Angular University, starting with the Angular Core Deep Dive. My local folder structure is Documents/Angular-University/Angular Core Deep Dive/angular-course. I have a few different branches created in angular-course. I've pushed a few changes, and I think I had trouble pushing one time, but I didn't give it much thought.

Today I discovered that my last push ended up as a Pull Request on Angular University's official Github repo, angular-university/angular-course. I thought I had forked their repo at one point to use their starter code, but I also thought I had created my own repo (I have emailed them to please undo the Pull Request since I can't seem to cancel it myself).

What I want to do now is create my own repo and push my code with all my changes on my different branches to it.

I read on one question something about creating a new folder and moving all the files from the current repo's folder to the new folder, but how does that work when you have multiple branches locally? I guess I don't understand in the first place how different code in different branches is stored locally on your machine. I only see one set of files in the folder.

I don't want the new repo to be a fork of the Angular University repo any more, so I don't know if that complicates things.

Any help is appreciated.

apex2022
  • 777
  • 2
  • 8
  • 28
  • 1
    Create new empty repo of your own on github. In your **local** repo, add the URL to the new github repo as a new `remote` (check `git help remote`), then you can push whatever branch you deem necessary into that empty repo. `git remote add new-repo url-to-new-remote; git push new-repo some-branch-I-have-in-my-local` – eftshift0 Jan 13 '23 at 16:53

1 Answers1

0

Avoiding the problem

The best way to avoid pushing either the wrong branch or to the wrong destination is to establish the habit of ALWAYS using the two arguments form of push:

git push <repository> <refspec>

(where refspec 99% of the time is a branch name). This way you always will specify exactly what you are pushing, and exactly where you are pushing it. There is no room for any surprises1.

Number of remotes

Now with regards to disassociate your code from the upstream source where it originated from, there is no need to do that and normally it is beneficial to keep traces of the lineage.

Git supports having as many remotes associated with a repository as you like, and when you always push by explicitly specifying the remote destination, then it does not matter if you have dozens of other potential remote destinations.

From git's perspective the names of the remotes are without any specific meaning, but from a mental perspective it is best to be consistent and use origin as the remote you want to push thing to instead of using different names in different repositories. I use the remote name upstream for the repository that is the source for a fork. And if add additional forks I normally use the userhandle (e.g. from https://github.com/userhandle/...) as remote name.

Example

I have a fork of Michael's git-test repository and when I run git remote -v inside my local clone I get

origin  git@github.com:hlovdal/git-test.git (fetch)
origin  git@github.com:hlovdal/git-test.git (push)
upstream        https://github.com/mhagger/git-test (fetch)
upstream        https://github.com/mhagger/git-test (push)

So whenever I have some branch I have been working on and want to push to my fork I run git push origin documentation_update or similar (if I ever wanted to push something directly to his repository the command would be git push upstream documentation_update, although I cannot see any use case for this).

What having the upstream remote available allows me to do is to get updates from the upstream project and incorporate into my fork, e.g.

git fetch upstream
git checkout master
git merge --ff upstream/master
git push origin master
git rebase master documentation_update

So having the fork source as a remote is normally very beneficial, I would not recommend skipping this.

Remedy

Now back to your situation with your angular-university repo. Assuming the following:

Given that, then there is no need to do anything as drastic as cloning your fork and start all over from a new clone.

Since you have cloned angular-university's repo it will by default end up as the origin remote, e.g. when you run git remote -v it will show

origin  https://github.com/angular-university (fetch)
origin  https://github.com/angular-university (push)

No problem, just rename it:

git remote rename origin upstream

and then add your fork:

git remote add origin git@github.com:apex2022/angular-course.git

Now your repository's origin remote points to your for like you want to, and you have access to the upstream repo for updates.

If you find having the upstream remote makes the view in gitk --all too noisy, you can always just delete it with git remote remove upstream, and when you want it back just run git remote add upstream https://github.com/angular-university.

One last thing to check is if any of your branches is set to track upstream instead of origin. Review them with

git config -l | grep '^branch.*remote='

and then if you want to change then run

git branch --set-upstrem-to=origin/my_branch_name my_branch_name

1 For the same reason I NEVER run rebase with anything less that two arguments (e.g. git rebase onto_ref branch_to_be_rebased, sometimes three: git rebase --onto onto_ref starting_ref branch_to_be_rebased).

hlovdal
  • 26,565
  • 10
  • 94
  • 165