Search code examples
gitrepositorybitbucketatlassian-sourcetree

How to map local folder with existing git repository


I want to map my working folder with existing bitbucket repository

What happened

I had a working folder connected with Bitbucket Repository. And I was working on a branch. The branch was already pushed and exist on Bitcucket remote. I have committed some changes locally, and then while committing one more changes SourceTree hanged and did not respond for half an hour. I will not blame source tree, it was my machine. So had to restart my machine. after that i cannot push any changes and was getting file corrupted error. Then, I deleted the .git folder :(

Problem

I was searching for any way to map the local folder with existing git repo, could not find anything. Is it possible or not?

Plan B

I can map my existing repository to a new folder and then copy the changed folders to the mapped folder. Not sure, that will work or not? It may work, but i am looking for any other solution better than this or a proper way to do that


Solution

  • First I just want to point out that by deleting the .git you did loose all your work on any other branches you had locally. That being said, as long as your current folder structure is the one that you want, then it should be okay.

    Here is what you want to do:

    1) Initialize a new .git repository using the command git init. it should tell you that you're on the master branch.

    2) Go ahead and run git checkout -b [name] where [name] is the name of the branch you were previously on. Once you have your branch name correct you can run git branch -D master

    3) Attach your git repository to your remote using git remote add origin [url] where [url] is the URL of your remote git repository.

    4) run git fetch to get the remote history

    5) track your current branch upstream using git push --set-upstream origin [branch] where [branch] is the same as [name] from step 2

    After following these steps, if you get errors it may be due to divergent paths. If that is the case just let us know because I'm afraid that is an entirely separate problem.