85

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1406716
  • 9,565
  • 22
  • 96
  • 151
  • 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 Jul 10 '13 at 13:11
  • I have already created a repo on bitbucket. – user1406716 Jul 11 '13 at 00:38
  • 3
    The exact thing u want is described here. http://samranga.blogspot.com/2015/07/create-git-bitbucket-repository-from.html?view=sidebar – Samitha Chathuranga Jul 02 '15 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 – Peter PitLock Sep 21 '16 at 13:16

5 Answers5

182

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
Choletski
  • 7,074
  • 6
  • 43
  • 64
  • 1
    @VibhavChaddha there is a git command: `git remote add origin YOUR_LINK_TO_REPO` – Choletski Feb 22 '17 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. – Vibhav Chaddha Feb 23 '17 at 05: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://@bitbucket.org//reponame.git` – M_K Jun 20 '17 at 08:34
  • @Choletski Hi, I just want to ask what this line do; `Type command git commit -m "my first commit"` – Blues Clues Jul 24 '17 at 03:30
  • @Jonjie that line will commit all your project files to your online repo, but don't forget to push it up - in this case on master branch – Choletski Jul 24 '17 at 09:12
  • @Choletski Oh, ok. is this line necessary to be `"my first commit"`? or I can put something in there? – Blues Clues Jul 25 '17 at 00:18
  • 1
    @Jonjie there can be any text you want – Choletski Jul 25 '17 at 09:31
  • Worked perfect. Not sure why Bitbucket doesn't explain this themselves. – Hillcow Aug 06 '17 at 14:57
  • how do you do that in the new UI? – vir us Dec 20 '21 at 11:53
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
johannes
  • 15,807
  • 3
  • 44
  • 57
  • 1
    Would you need to create the git repo in bitbucket first to be able to push to it? – Jeff B Jul 10 '13 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 Jul 10 '13 at 13:49
  • I get the error below. {master *} ~/websites/example7.dev$ git remote add some_name https://mavme@bitbucket.org/mavme/example-site-example7.dev.git 04:40:46 {master *} ~/websites/example7.dev$ git push some_name Password for 'https://mavme@bitbucket.org': – user1406716 Jul 11 '13 at 00:10
  • next part of the error message as it did not fit in one comment: To https://mavme@bitbucket.org/mavme/example-site-example7.dev.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://mavme@bitbucket.org/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 – user1406716 Jul 11 '13 at 00:11
  • It seems like bitbucket,for whatever reason, created a master branch. You could clone it (`git clone mavme@bitbucket.org/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 Jul 11 '13 at 10:14
  • This exact thing is well explained in detail here... http://samranga.blogspot.com/2015/07/create-git-bitbucket-repository-from.html?view=sidebar – Samitha Chathuranga Aug 26 '15 at 14:06
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arrigo
  • 411
  • 3
  • 5
  • 3
    The commands in 4th step will give errors. Have to follow following steps in this link. http://samranga.blogspot.com/2015/07/create-git-bitbucket-repository-from.html?view=sidebar – Samitha Chathuranga Jul 02 '15 at 14:10
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sver
  • 866
  • 1
  • 19
  • 44
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.