86

How can I add the contents of an existing folder to Git version control?

The tutorial here covers the case of making a directory and then adding source contents to it. I have some source code in a folder that is path dependent and don't want to move it.

So, how can I just go into my folder and make it a repository?

4
  • Does the git repo already exist on bitbucket? Or are you just starting from scratch and want to create a git repo that will be on bitbucket?
    – Jeff B
    Commented Jul 10, 2013 at 13:11
  • I have already created a repo on bitbucket. Commented Jul 11, 2013 at 0:38
  • 3
    The exact thing u want is described here. samranga.blogspot.com/2015/07/… Commented Jul 2, 2015 at 14:11
  • If you have a tool such as Visual Studio for Git - would you ever need to go command line? It has a publish facility where you can publish a whole new branch Commented Sep 21, 2016 at 13:16

5 Answers 5

183

Final working solution using @Arrigo response and @Samitha Chathuranga comment, I'll put all together to build a full response for this question:

  1. Suppose you have your project folder on PC;
  2. Create a new repository on bitbucket: enter image description here

  3. Press on I have an existing project: enter image description here

  4. Open Git CMD console and type command 1 from second picture(go to your project folder on your PC)

  5. Type command git init

  6. Type command git add --all

  7. Type command 2 from second picture (git remote add origin YOUR_LINK_TO_REPO)

  8. Type command git commit -m "my first commit"

  9. Type command git push -u origin master

Note: if you get error unable to detect email or name, just type following commands after 5th step:

 git config --global user.email "yourEmail"  #your email at Bitbucket
 git config --global user.name "yourName"  #your name at Bitbucket
9
  • 1
    @VibhavChaddha there is a git command: git remote add origin YOUR_LINK_TO_REPO
    – Choletski
    Commented Feb 22, 2017 at 11:56
  • No no that I know. I meant do we have to use it somewhere in between the steps that you have mentioned because you have specially specified it in the picture. Thanks. Commented Feb 23, 2017 at 5:09
  • 1
    For step 7 if you don't have a public key set up for ssh (i.e get Permission denied (publickey).fatal: Could not read from remote repository. when pushing) then use the https URL, git remote set-url origin https://<username>@bitbucket.org/<username>/reponame.git
    – M_K
    Commented Jun 20, 2017 at 8:34
  • @Choletski Hi, I just want to ask what this line do; Type command git commit -m "my first commit" Commented Jul 24, 2017 at 3:30
  • 1
    @Jonjie there can be any text you want
    – Choletski
    Commented Jul 25, 2017 at 9:31
27

You can init a Git directory in an directory containing other files. After that you can add files to the repository and commit there.

Create a project with some code:

$ mkdir my_project
$ cd my_project
$ echo "foobar" > some_file

Then, while inside the project's folder, do an initial commit:

$ git init
$ git add some_file
$ git commit -m "Initial commit"

Then for using Bitbucket or such you add a remote and push up:

$ git remote add some_name user@host:repo
$ git push some_name

You also might then want to configure tracking branches, etc. See git remote set-branches and related commands for that.

6
  • 1
    Would you need to create the git repo in bitbucket first to be able to push to it?
    – Jeff B
    Commented Jul 10, 2013 at 13:07
  • 4
    Yes. You have to create a place at bitbucket so they know where to expect by whom, the repo they create will be empty so you can push anything there :)
    – johannes
    Commented Jul 10, 2013 at 13:49
  • I get the error below. {master *} ~/websites/example7.dev$ git remote add some_name [email protected]/mavme/example-site-example7.dev.git 04:40:46 {master *} ~/websites/example7.dev$ git push some_name Password for '[email protected]': Commented Jul 11, 2013 at 0:10
  • next part of the error message as it did not fit in one comment: To [email protected]/mavme/example-site-example7.dev.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to '[email protected]/mavme/example-site-example7.dev.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details. 04:40:54 Commented Jul 11, 2013 at 0:11
  • It seems like bitbucket,for whatever reason, created a master branch. You could clone it (git clone [email protected]/mavme/example-site-example7.dev.git') and check it out if nothing of worth is in there you do the push from your project with force (git push -f some_name)
    – johannes
    Commented Jul 11, 2013 at 10:14
24

User johannes told you how to do add existing files to a Git repository in a general situation. Because you talk about Bitbucket, I suggest you do the following:

  1. Create a new repository on Bitbucket (you can see a Create button on the top of your profile page) and you will go to this page:

    Create repository on Bitbucket

  2. Fill in the form, click next and then you automatically go to this page:

    Create repository from scratch or add existing files

  3. Choose to add existing files and you go to this page:

    Enter image description here

  4. You use those commands and you upload the existing files to Bitbucket. After that, the files are online.

1
2

The commands are given in your Bitbucket account. When you open the repository in Bitbucket, it gives you the entire list of commands you need to execute in the order. What is missing is where exactly you need to execute those commands (Git CLI, SourceTree terminal).

I struggled with these commands as I was writing these in Git CLI, but we need to execute the commands in the SourceTree terminal window and the repository will be added to Bitbucket.

1

I have a very simple solution for this problem. You don't need to use the console.

TLDR: Create repo, move files to existing projects folder, SourceTree will ask you where his files are, locate the files. Done, your repo is in another folder.

Long answer:

  1. Create your new repository on Bitbucket
  2. Click "Clone in SourceTree"
  3. Let the program put your new repo where it wants, in my case SourceTree created a new folder in My Documents.
  4. Locate in windows explorer your new repository folder.
  5. Cut the .hg and README (or anything else you find in that folder)
  6. Paste it in the location where is your existing project
  7. Return to SourceTree and it will say "Error encountered...", just click OK
  8. On the left side you will have your repository but with red message: Repository Moved or Deleted. Click on that.
  9. Now you will see Repository Missing popup. Click on Change Folder and locate your existing project folder where you have moved the files mentoned earlier.
  10. Thats it!

Tips: Clone in SourceTree option is not available right after you create new repository so you first have to click on Create Readme File for that option to become available.

1
  • step 6.5 delete new repository folder.
    – AMAN77
    Commented Feb 28, 2019 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.