Nov 162012
 

If you are currently working on a project that you would like to check into github, it may not be very obvious at first. This step by step tutorial assumes that you already setup your github account.  If you have not, follow these instructions first: https://help.github.com/articles/set-up-git. Once your github is setup, create a new repo trough UI: https://github.com/new

In this example, I will use repo “testRepo”, just to keep it simple!

Now, on your command line, go to your existing project. For the sake of this tutorial, the names of existing project and git repo are the same, but they do not have to be.

My current project has 2 files:

egle@ubuntu:~/testRepo$ ls -la
total 16
drwxrwxr-x  2 egle egle 4096 Nov 16 19:41 .
drwxr-xr-x 25 egle egle 4096 Nov 16 19:40 ..
-rw-rw-r--  1 egle egle   14 Nov 16 19:41 file1.txt
-rw-rw-r--  1 egle egle   15 Nov 16 19:41 file2.txt

To turn this directory into a git repository, I need to initialize it:

egle@ubuntu:~/testRepo$ git init
Initialized empty Git repository in /home/egle/testRepo/.git/

This creates a local git repository on your machine. That’s right, you can version files locally without ever needing to connect to github or other git server! Note that currently your new git repo contains no files.

Add all the files to the repository:

egle@ubuntu:~/testRepo$ git add .

Once the files are added, they need to be committed:

egle@ubuntu:~/testRepo$ git commit -m "first commit"
[master (root-commit) b9c1002] first commit
Committer: Egle <egle@ubuntu.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 file1.txt
create mode 100644 file2.txt

In this case, I have not setup my git settings, so will need to fix that later. The important part here is that files got committed to the local repo.  However, local repositories are a little hard to share. Lets add a remote repository, named origin, and push files to it:

egle@ubuntu:~/testRepo$ git remote add origin https://github.com/eglute/testRepo.git
egle@ubuntu:~/testRepo$ git push -u origin master
Username for 'https://github.com': eglute
Password for 'https://eglute@github.com': 
To https://github.com/eglute/testRepo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Now your current project is in github, ready for pulls, pushes, merges, branches, and forks!

-eglute

 

 Posted by at 11:02 pm

  2 Responses to “Tutorial: Adding Existing Project to Github”

  1. I think you might want do check the hub project on github https://github.com/defunkt/hub which provides github extensions for the git command line .

  2. Mnay thanks, this tutorial is very useful.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>