0

I was working on 2 computers, alternating which one I use, and every time moved the whole thing between them. Then I setup git on computer A and synced it with github repository. So far so good.

Now I want to clone this to computer B, but git clone doesn't work because the folder and files already exist.

So this is what I've done on computer B:

git init
git remote add origin <my-github-rep.git>
git fetch

Now I need to bring the latest files from github here.

git reset origin/master

I think this is what resets current HEAD to point to master branch on origin.

Okay, now here is the problem:

I would assume that at this point I need the following:

git checkout -t origin/master

Because this would bring all the latest files from origin and would have the whole thing checked out on computer B. This is what I want, but it doesn't work. Here is the error msg: "fatal: A branch named 'master' already exists."

How do I overcome this and just bring the files? What have I done wrong.

(to clarify: I don't want just to delete the folder and the files and simply clone the repository because there are many more files in there which are not supposed to be on git, so it's hard to do pick and choose; at this point what I want is to overwrite all the relevant files in my local folders from the latest files in the remote repository - this way I'll have the latest code, and the files that are not tracked by git won't be affected)

Vlad
  • 1
  • 1
  • 4

2 Answers2

1

Your git reset command created the branch named master. It already points to the correct commit. The problem is that you now have a populated index, but not a correctly-populated work-tree.

The simplest solution at this point is to move all the files you want to keep out of this directory entirely, e.g., mkdir ../save and then mv * ../save to get them out of the way. Then you have an empty current working tree, and git status will show you that all files are deleted but not staged for commit. You can then do either git reset --hard or git checkout -- . to bring all files back, then mv ../save/* . to put your saved files back in place.

At that point, the one remaining issue will be that your local master has no upstream. To fix that, just run:

git branch --set-upstream-to=origin/master

and you will be all set.

torek
  • 448,244
  • 59
  • 642
  • 775
-2
git branch                          <-- view list of branches>
git checkout master                 <-- switch branch to master>

If this dosent work

git branch -d branch_name           <-- delete the specified branch locally>

To have a fresh local branch code cloned

git config --global                 <-- followed by the user id or name>
git clone repo_address.git          <-- repo_address.git is the repository url>
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92