Blog

18
November 2013

Gavin Pickin

Git for Dummies - Converting a Project into a Git Repo

Source Control

Thank you for reading along with my Git for Dummies series. It can be a daunting task, and this series tries to show you step by step all the little processes you will come across as you start using Git. The best part about it, I'm not a Git expert, I'm fairly new to it, and as I help people I know get better, I'm sharing the knowledge with you. 

So far we have covered Why you should use Source Control, Not Using Source Control, amazingly you're not alone, Installing Git on Windows, Installing Git on Mac OSX, Installing Git on Linux, Your First RepoSetting up your first Remote Repo on BitbucketCreating your SSH Keys for Bitbucket, and today, Converting a Project into a Git Repo.

Being a developer, you have loads of projects sitting around, small ones, big ones, but we've only showed you how to start a repo from scratch, well, that doesn't help you with all the existing projects, so we'll walk through how to make an existing project into a repo, and how to store it on Bitbucket. If you have been following along, a lot of these steps will be the same as when we created a repo from scratch. Its actually very similar.

So first, lets go to BitBucket, and add a repo. Click on the Create button, and you'll see the form below.

 

Ok, so we fill in the form, adding a useful name for easy reference... note if its private or not (if its public, we use GitHub due to the open source social atmosphere, although Bitbucket can be used for Public repos to), select our options, language, and click Create Repository when we're all set.

 

Once we do that... we see one of Bitbucket's helper pages. Every time you create a repo, they have they little helper pages to help you through the initial startup, and they're a nice way to make sure you know what you're doing. So you can select I'm starting from scratch (like a previous entry), or I have an existing project to push up, thats what we're doing, so lets Virtually Click that.

 

So now we see this page. If you are a true beginner, you might not see something is missing here.
We said we had an existing Project, but not an existing repository, so the instructions below are correct, but if you jump straight into your terminal / bash, and do this, you'll get an interesting error, I think. Lets try it and see.

 

$ git remote add origin ssh://git@bitbucket.org/gpickin/www.trymynuts.com.git
fatal: Not a git repository (or any of the parent directories): .git

As I suspected... you cannot do a git remote add origin command, on a directory which is a Project yes, but an existing repo, no.
So, lets click back on the Start from Scratch slide, and see what we can do.

Ok, now, since we have an existing project, we'll skip the first line, making the directory... so lets just cd to the path, and then we'll do a git init.

 

Now below you can see the result or my bad first command, and then I do a git init, and now the repo is initialized.

 

So now we can go back to the previous slide, where we have a Project, and it is now a Repo, and now we can continue.

 

We have a repo, so we can add the git remote add origin, so lets do that.

$ git remote add origin ssh://git@bitbucket.org/gpickin/www.trymynuts.com.git

Ok, now we have done that, the next step says, git push -u origin --all and --tags. 
Thinking about this, we initialized a repo, but we haven't got anything to push yet... so what does this do.

 

Why did that happen? We initialized the Local Git Repo... but you didn't add anything to it. Git only pushes files you tell it to watch. We made a repo, now we have to add the files to the repo. If we were starting from scratch, or we just wanted to add a readme, we could follow the instructions below, but lets add all of the files from our project into the repo.

 

Now, this is the only real difference between a start from Scratch and Existing project... we need to do a simple command.

$ git add .
$ git commit -m "Added Existing Project Files"

What line 1 does, is recursively add all files to gits list of tracked files, . is a wildcard.
You can add them individually like the readme in the above slide instructions, but with an existing project, that is not realistic.

The second line, we commit the tracked files, into a commit point on the timeline. Now, we'll see some output like the following screenshots.

Lots and lots more files and then

 

Now, once the files are committed to the local repo, we can use git push to push them to the origin, and we want them in the master branch of the origin... so we use the following command.

$ git push -u origin master

 

The next step in the slide walkthrough is to invite your friends, and or colleagues.

 

And ta-da, you're all set. Now when you go to the Repo Page, instead of these helper slides, you'll see the actual commit history.

 

Now, everytime you go to the Repo Overview, you'll see this.

 

Now, that is a long detailed walkthrough, for those wanting to know what we actually did, he is the short version, for future use.

$ cd /path/to/your/project/
$ git init
$ git remote add origin ssh://git@bitbucket.org/gpickin/www.trymynuts.com.git
$ git add .
$ git commit -m "Added Existing Project Files"
$ git push -u origin master

If this is the first time you have committed to any project on this machine, you might have to set a couple of global variables if Git cannot determine them on its own... your name, and email address... you can follow the commands you see in git, or you can go here for a simple walkthrough to add Global or Local settings for you Name and Email Address in Git.

Text we'll learn about Pushing and Pulling from Local to Remote, and from Remote to Dev, and maybe even Remote to Production.

Blog Search