Create a local git repository and link to Github

I found two articles here and here which give an overview of common git commands. At the time I was trying to find a way to copy files up to github.com to a newly created repository. It took a bit of trial and error and additional searching but I eventually managed to work out how to create a repository and upload the files. Below is a script of the command used, this assumes the git install folder is referenced in the machine’s Environment Variables to recognise the git command.

If you already have a folder containing files skip the first two points

1) Open Windows Explorer and create a new folder at the desired location.
2) Copy your source files to the newly created folder.
3) Open a command prompt.
4) Change to the new folder, e.g if I created a folder called MyWebsite I would type the following before pressing Enter.

cd my_project

5) To initialise the repository for git versioning type this command.

git init

6) Tell git to check and reference all new files using the * wildcard.

git add *

7) At this point you can check the status of the files in the folder.

git status

A message is output to the command window similar to that below.  As all files are new they will all be listed prepended by new file:. Any modified or deleted files would have the relevant status.

On branch master

Initial commit

Changes to be committed:
(use "git rm --cached ..." to unstage)

new file: project.sln
new file: css/site.css

8) All new/changed files can be committed to the local repository.

git commit -m "Initial Project Commit"

Following this command a message similar to below will be output to the command window.

[master (root-commit) 864b930] Initial Project Commit
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 project.sln
create mode 100644 css/site.css

9) Add a remote origin (location).   Substitute **YourGithubUserName** with your github user name.  Substitute **GithubRepository** with the name of your github repository.

git remote add origin https://github.com:**YourGthubUserName**/**GithubRepository**.git

10) At this point you can now list all remote origins for local repository.

git remote -v

A message similar to below will be displayed in the command window.

origin https://github.com/**YourGthubUserName**/**GithubRepository**.git (fetch)
origin https://github.com/**YourGthubUserName**/**GithubRepository**.git (push)

11)  Before you can push files to the github repository you have to first pull down the current version.

git pull origin master

The command window will display a file similar to this:

From https://github.com/**YourGthubUserName**/**GithubRepository**
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
LICENSE | 25 +++++++++++++++++++++++++
README.md | 4 ++++
2 files changed, 29 insertions (+)
create mode 100644 LICENSE
create mode 100644 README.md

12) The local repository can now be pushed to github

git push

at which point you will be prompted for username and password.

Username for 'https://github.com':
Password for 'https://**YourGthubUserName**@github.com':

If successfully authenticated a summary of pushed files will be displayed.

Counting objects: 61, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (56/56), done;
Writing objects: 100% (60/60), 458.30 KiB | 0 bytes/s, done.
Total 60 (delta 9), reused 0 (delta 0)
To https://github.com/**YourGthubUserName**/**GithubRepository**.git
4bd0db0..b33bfd8 master -> master

And that is more or less be it. If you log in to your Github account and navigate to the repository folder you should now see all the files.

Create a GitHub repository

On the downloads page are links to some example applications relating to a number of posts on this blog. In this, the first of 2 posts on git I will explain how to create a repository on the Github site.

1. If you haven’t registered with the site go to the Github home page and do that now.

2. Once you have registered make sure you are logged in.

3. Click on the + in the top right of the page and select New Repository from the popup
github-new-repostory

4. On the new repository page enter a name for the repository and if you desire a description. Do not use spaces in the name, use hyphens ‘-‘ instead.
github-new-repository-name

5. Select either a public or a private repository.  At this time a free account is limited to public repositories which are visible to everyone.  A paid account allows you to create private repositories.  In either case the repository owner and contributors are the only people able to update or modify the repository.
github-new-repository-availability
6. It is recommended you add a README file, tick the Initialze this repository with a README box. We will edit the contents of this file later.
github-new-repository-readme

7. There are a number of license options. Click on the information icon (i) for details, different licenses define what you and those downloading the repository contents are responsible for.   I generally go for The Unlicense license as it states the information is offered under public domain and that I cannot be held liable and the recipients are not responsible for any type of recognition or copyright restrictions.
github-new-repository-license

8. Once all the options have been completed above click the Create repository button
github-new-repository-create-button

All done!  a repository will now be created,  you will be presented with a page like this.  Depending on the options selected previously you may have fewer, even no files listed.

github-new-repository-complete

Just to complete this example lets modify a file and commit the change back to the repository.

1. Click README.md link in the list of files.
2. When the file is displayed click the pencil icon in the header.

github-new-repository-file-edit

3. In the file editor modify the file.

github-new-repository-file-editing

4. When you have finished editing click the Preview changes tab.  Green bars highlight where the changes were made.

github-new-repository-file-comitting

 

5. At the bottom of the page in the Commit changes section it is good practice to enter an brief explanation of what changes are being committed.  A longer extended description is optional.

6. Click the Commit changes button.  The repository is now updated with the changes made to the readme.md file.

 

In the second part we will be looking at using the command line to create a local repository, link this to the Github repository and and commit files up to Github.