1

I have worked with git here and there but unfortunately it has never fully clicked with me. I want to use it now so that I can share my code with others later.

I have managed to create a new (remote?) repository on git. I then created a folder on my computer (with my code inside) and linked it to my repository and staged and pushed my files and everything worked correctly...

To clarify, I used this tutorial: http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners

Then I closed my Terminal window and my system still "remembers" my remote repository git remote add --track master origin [url-to-git-repo] but the local folder where my code is stored as been forgotten. Instead git sees my entire user folder - instead of the designated folder within.

So my question is, for a really basic user (who doesn't even necessarily want to build side branches and merge them or do any team editing),

How do I reconnect my local folder to my repository to pick up where I left off (with each new terminal use)?

Afterwards I can run a status check to see which files were updated, but how do I then make sure that they are pushing correctly? I am not sure of how to correctly set up the "ignore" files, so up until now I have been using the git push -a origin master command.

(Whatever I have been doing seems not to be very consistent although I think that I am using the same commands each time...) I know this question seems simple, but it seems like every "connection" instruction involves creating a new repository $ git clone [url] and new files $ git init [project-name], and that is not something that I want to do.

Thank you in advance for your help.

April
  • 87
  • 1
  • 10

2 Answers2

0

Git works by having repositories. You can't just link files that are not inside a repository. Local and Remote repositories have nothing to do with each other. Remote is just there for you to push your local repository and share it with others. You must first create a local repository and start from there. Git doesn't even have to have a remote repository. It can work entirely with just having a local repository and all features are still there.

Edit: You can't have added a remote without having a local so you must have created it in the wrong folder. Probably the user folder

itsundefined
  • 1,409
  • 2
  • 12
  • 32
  • 1
    I was under the impression that the local files were called a "local repository" and that the "remote repository" is accessed by the "clone" URL provided in GitHub... As I mentioned, I did -as far as I know- create a local repository with the commands above, otherwise I could have never pushed my files to my "remote" (URL) repository. (?) My question is not how to create the initial connection, rather on how to reinstate the said connection. – April May 20 '17 at 12:11
  • Update: after following the tutorial, a folder was added here: MyComputer/Users/MyNewProjectFolder - I have only pushed files located within that project folder. – April May 20 '17 at 12:20
  • you already did the connection. If you have both a remote and a local then you don't have to do anything else. Just don't forget to use the git commands from the folder that has the local repository. – itsundefined May 20 '17 at 12:31
  • For new projects you should follow this simple instructions. Create a folder for the project. Git init while you have the terminal inside that folder. Then go to github or any other git website and create a new repository there. Then add the remote as you did with this one and you are done. After that you can do git push and git pull normally. Don't forget that if you want to do what I described you should create an empty remote repository without a readme. Otherwise you will first have to pull this files before pushing. – itsundefined May 20 '17 at 12:36
  • It isn't a new project anymore... hence the question of how to "reconnect" my local files to my repository. It can't be that I have to recreate a file every time after I shut down my terminal application!? Can you explain what you mean by pulling (I know the command but maybe better how it works) because I thought pulling was to update the local files, but my local files are newer than the ones in my repository, so I cannot see why I would be wanting to do that.... then they would just be replaced with their original state? What I want: 1.) reconnect, 2.) get status, 3.) push – April May 22 '17 at 05:59
  • I am having the same problem as here: http://stackoverflow.com/questions/9813353/git-status-picking-up-parent-folders-files But I have no idea why, because I thought I followed the tutorial exactly and I didn't move any files... but good, I guess this means I need to start over... ? – April May 22 '17 at 06:34
  • http://stackoverflow.com/questions/7705499/git-status-shows-untracked-file-on-a-higher-level - This also seems to be the same issue. But I get the following: fatal destination path "." already exists and is not an empty directory... therefore I do not know how to proceed. – April May 22 '17 at 06:48
  • i do not understand your problem. You do not reconnect. You just use the files that are inside the folder that has the git repository and then you can git commit and git push to update the remote. – itsundefined May 22 '17 at 10:14
  • Hi slowdeath007, Please see my comment above... maybe it will explain what I was trying to do a little better. For now, I have resolved my issue. Thanks for trying to help me out. – April May 22 '17 at 10:19
0

Okay after spending quite a while to find the answer to my question:

I used the following commands:

mv - I moved my git folder to the desired directory

cd

git add

git commit -a 

via: My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)

Then I had to reset my directory:

$ git remote set-url origin 

via: Github "fatal: remote origin already exists"

Then I still couldn't connect properly so I had to do this:

git fetch

git reset --hard origin/master

via: GIT Fatal : refusing to merge unrelated histories

Then finally:

cd /path/to/my/repo

git init

git add --all

git commit -m "Initial commit"

git remote add origin <remote repo URL>

git push -u origin master

via: How to fix "refusing to merge unrelated histories" when uploading project to github?

Note: I still have issues with "fatal: refusing to merge unrelated histories"

But when I used add "/my-changed-file" - I was still able to connect to my repository and make an update, so I am hoping that I will be able to continue using my repository.

If anyone knows how to properly deal with the "merge unrealated histories" issue without creating more havoc, please let me know. I read a little about it here: Git refusing to merge unrelated histories - but can't claim to know what all of the commands actually do.

Thank you for your help, and I hope this comment might help someone else.

Community
  • 1
  • 1
April
  • 87
  • 1
  • 10