Moving Local git Repo to GitHub

Recently I was working on a learning type project that I considered important enough to create a local git repo for it, but not a remote one. However it got to the point that I wanted to preserve it on GitHub. I can add a repo to GitHub, link to it, then push new branches to it. The problem is that I cannot push the master and have that history. First of all there is nothing in my local repo to tell me that it is connected to any remote repo. This is the command used to determine the associated remote repo:

git remote –v

In this case it returned nothing. To get where I wanted to go I created a git repo named GitToAdd. Here is the command I used to associate this remote repo with my local repo:

git remote add origin https://github.com/SharePointGuy1/SampleGit.git

(I got the url from GitHub by copying it the same way as if I was cloning the repo.) After doing so I get the following result from the git remote –v command:

λ git remote -v
origin https://github.com/SharePointGuy1/AddToGit.git (fetch)
origin https://github.com/SharePointGuy1/AddToGit.git (push)

From here I can create a new branch and push that up to the remote, however, if I try to push the master to the remote I get the following result:

λ git push origin master
Username for 'https://github.com': SharePointGuy1
Password for 'https://SharePointGuy1@github.com':
To https://github.com/SharePointGuy1/AddToGit.git
  ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/SharePointGuy1/AddToGit.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.

The solution is to use the --force parameter. This forces the history from the local repo to overwrite that of the remote.

λ git push origin master --force
Username for 'https://github.com': SharePointGuy1
Password for 'https://SharePointGuy1@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/SharePointGuy1/AddToGit.git
  + 6121ab3...6312e4d master -> master (forced update)

Now everything is in synch, all history is retained and life is good. ;-)

1 Comment

Add a Comment