Wednesday, July 1, 2015

Create a git BitBucket/ Github repository from already locally existing project



  • This post is originally targeted at BitBucket repositories, but the basic steps are common to Github too. 
  • This is a simple issue but would get hours if followed the official Bitbucket.com instructions.  :P .So I am posting this.
  • The case is that we have a project in our pc and it is almost completed (or partially done) and now we want to include it in a git repository and push it to a bitbucket repository. So note that we do not have still a BitBucket / Github repository for our project or a local git repo in our pc too. (but of course we have a bitbucket/github account :) )
  • So, believe me.. follow these simple steps.
Pre-requisite: You should have installed git into your pc(PC=Personal Computer>>simply your computer).

1) Create a repository in Bitbucket.org / Github.com to contain our project.

  • For this just click Repositories tab> Create New Repository (or simply click this link- https://bitbucket.org/repo/create)
  • Simply fill the details you want and guess we filled the name as "TestingGitRepo"
  • Click "Create Repository"

Now u have done with creating bitbucket repo.
(If u are dealing with Github, instead of Bitbucket, create the repository as the default way and remember not to tick "
Initialize this repository with a README" ,because it will cause bit hard when doing the initial commit later.)

Just after creating the repo you will be redirected to a page as below.

Click "I have an existing project" and copy the command displayed below.
(It will be easier if you copy it here now)

The command we copied is,

git remote add origin https://Samitha@bitbucket.org/Samitha/testinggitrepo.git

(If u are using Github, a similar command with the starting part "git remote add origin"
will be displayed in the following page after the creation of repo, and just copy it)
2) Open command prompt in your PC and go inside to the directory where u want to be as a repository.
   For example if you go into the directory "G:\AndroidStudioWorkspace", the contents in that directory will be sent to the bitbucket/github repository you created.

3) Enter,
       git init

    This will initialize this directory as a git repo.
4) Now paste the command we copied and press enter.
  git remote add origin https://Samitha@bitbucket.org/Samitha/testinggitrepo.git

5) Enter,  
git add --all

This will add all the files and folders in this directory into the git repository.

6)Now you have to make the initial commit. So enter,  
git commit -m "Initial Commit"

At here sometimes there will give an error message as follows if you are using git in your PC fresh and so have not configured your Bit Bucket account with the git.



If you get this error just do what has been asked to do.
Enter,
git config --global user.email "rmschathuranga@gmail.com"
git config --global user.name "Samitha"

Note that you have to use your email address and bit bucket user name instead of 
rmschathuranga@gmail.com and Samitha (which are MINE)..!!!
7) Now enter,  
git push -u origin master
Enter the password of your bitbucket/github account when prompted.So then all the files and folders in your local repo will be pushed (uploaded) to the bitbucket repo, creating a new branch with the name "master". You will see messages as below,



     And that's all. You have done it.
Go check in the bitbucket/github repository you created. Your project has been successfully uploaded into the bitbucket repo. And the repository is successfully created. 

Important Notes:


  • Whenever you make changes in your local project files, and want to push the changes into the remote bitbucket/github repository just follow above 5,6,7 steps.
  • Note that if a directory is empty, that will not be added to git (to the remote repo too).
  • If you want deeper clarification, anyway Git doesn't ignore empty directories. It typically ignores all directories. In Git, directories exist only implicitly, through their contents. Empty directories have no contents, therefore they don't exist in git repositories.


--------------------------------------------------------------------------------------------------------------

For extra knowledge
---------------------------------------------------------------------------------------------------------------
git add command

For extra knowledge I would like to go deep on git add command.

git add has number of options for various requirements. Following tables (extracted from http://certificationquestions.com/version-control-system/git/difference-git-add-git-add-git-add-u/ ) clearly shows the difference between them. Note that,
git add -A  = git add --all 

You can find your git version by git version command

For git versions 1.x

For git version 2.x

So my recommendation is to use git add --all which is similar to git add -A as it is the most common and general requirement. 

And here I am highlighting the difference between,
           git add . and git add --all 
in git version 1.x which most of us use now. It is that git add --all stages all the changes to the repository, while git add . do not stage deleted files. It means that if u had deleted a file in your local repository and u want it to be deleted from the remote repository too, u should do git add --all But git add . would not remove that file from the remote repository.

Comments a and suggestions are highly appreciated if u found this post useful..!!! :-) 

22 comments:

  1. Thanks for the tutorial! Only problem is, when I follow these exact instructions, only one file is added to my repo :((
    My project folder has many thousands of files. The command "git add ." is not adding anything from subfolders. Does one need a different command for a recursive add of folder structure? Thanks again! :)

    ReplyDelete
    Replies
    1. There can be few reasons for your problem.
      1. Directories which are empty are not added to git. (so are your those missed directories are empty?)
      -If you want deeper clarification, anyway Git doesn't ignore empty directories. It ignores all directories. In Git, directories exist only implicitly, through their contents. Empty directories have no contents, therefore they don't exist in git repositories.

      2. You might have defined the .gitignore file to ignore all the type of files which are existing in your repository.
      -use this command to check it.
      git status --ignored
      -What is the output of that command?

      Delete
  2. A very good explanation. Thanks

    ReplyDelete
  3. Hi Samitha,
    I am trying to add multiple porjects to single repository, I followed all the steps you have mentioned above, but it is not working. It is just adding empty folders in to the repo, no subfolders and files have been added. Could you please help me here?

    ReplyDelete
    Replies
    1. Did u started right from the beginning of my instructions? Anyway check the .gitignore file. U might have ignored specific file types and sub directories.
      -use this command to check it.
      git status --ignored
      -What is the output of that command?
      (Sorry for the late reply. If u have already fixed the issue, please mention the mistake u had done. It will be useful for others too)

      Delete
  4. Much thanks for the tutorial. I cannot believe that it was so easy, but nobody can explain it simply.

    ReplyDelete
  5. If i want to push on the branch other than master ?

    ReplyDelete
    Replies
    1. This is not very different. Just change the step 7 command. i.e:
      git push -u origin other-branch-name
      replace "master" with the required other branch to push.

      Delete
  6. I am getting this error on git push command Please make sure you have the correct access rights and the repository exists.

    ReplyDelete
  7. It was very helpful. Nicely explained. Couple of years I used git, but I didn't knew the difference between git add . and git add --all.

    ReplyDelete
  8. git push orgin master
    after enter i got this
    error: src refspec master does not match any.
    error: failed to push some refs to 'orgin'


    please help me out

    ReplyDelete
    Replies
    1. I am have the same issue after git push -u origin master. If anyone can help with this, would be greatly appreciated. Thanks :)

      Antonio

      Delete
    2. I got this error when I forgot to commit before pushing.

      Delete


  9. the future of the industries these days, this article helps me to figure out which language I need to learn to pursue the future in this field. DevOps Training in Chennai | DevOps Training in anna nagar | DevOps Training in omr | DevOps Training in porur | DevOps Training in tambaram | DevOps Training in velachery

    ReplyDelete
  10. Thanks a lot, very clear and useful tutorial.
    Best wishes!!!!

    ReplyDelete
  11. Thanks for sharing this post. Your blog has been a source of great tips and knowledge..

    ReplyDelete

Comments are highly appreciated... :-)