Home iOS & Swift Books Git Apprentice

Creating a Repository Written by Sam Davies & Chris Belanger

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

You’ve come a long way in your Git journey, all the way from your first commit, to learning about what Git does behind the scenes, to managing some rather complicated merge scenarios. But in all your work with repositories, you haven’t yet learned exactly where a repository comes from. Sure, you’ve cloned a repository, and you’ve forked repositories and worked with remotes, but how do you create a repository and a remote from scratch?

This chapter shows you how to create a brand-new repository on your local machine, and how to create a remote to host your brand-new repository for all to see.

Getting started

Many people will blindly tell you that the easiest way to create a repository is to “Go to GitHub, click ‘New Repository’, and then clone it locally.” But, in most cases, you’ll have a small project built up on disk before you ever think about turning it into a full-fledged repository. So this chapter will put you right into the middle of your project development and walk you through turning a simple project directory into a full-fledged repository.

But, first, you’ll need a project! Check the starter folder for this chapter; inside, you’ll find a small starter project that is the starting webpage for the sales page for this book.

Copy the entire mastering-git-web directory from the starter folder into your main MasteringGit folder.

Now, open up your terminal program and navigate into the mastering-git-web directory. If you’ve been following along with the book so far, you’re likely still in the MasteringGit/ideas folder, so execute the following command to get into the mastering-git-web subdirectory:

cd ../mastering-git-web/

Once there, execute the following command to tell Git to set this directory up as a new repository:

git init

Git tells you that it has set up an empty repository:

Initialized empty Git repository in /Users/chrisbelanger/MasteringGit/mastering-git-web/.git/

Why does Git tell you it’s an empty repository, when there are files in that directory? Think back to how you staged files to add to a repository: You have to use the git add command to tell Git what to include in the repository; Git wouldn’t just assume it should pick up any old file lying around. And the same is true, here; Git has created an empty repository, just waiting for you to add some files.

Now, before you add any files, you’ll want to get two things in your repository that are good hygiene for any repository that’s designed to be shared online: a LICENSE file, and a README file.

Creating a LICENSE file

It’s worth understanding why you need a license file, before you go and create one blindly.


Creating a README file

The README is much more straightforward than the license file. Inside the README, you can put whatever details you want people to know about you, your project, and anything that will help them get started using your project.

# mastering-git-web

This is the main website for the Mastering Git book, from raywenderlich.com.

contact: @crispytwit
~MasteringGit/mastering-git-web $ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)


nothing added to commit but untracked files present (use "git add" to track)
git add .
git commit -m "Initial commit of the web site, README and LICENSE"
[master (root-commit) 443f9b3] Initial commit of the web site, README and LICENSE
 5 files changed, 111 insertions(+)
 create mode 100644 LICENSE
 create mode 100644 README.md
 create mode 100644 css/style.css
 create mode 100644 images/SFR_b+w_-_penguin.jpg
 create mode 100644 index.html

Create mode

That create mode is something you’ve seen before in the output from git commit, and have probably wondered about. It’s of academic interest only at this point; it really doesn’t affect you much at this stage of your interaction with repositories.

Creating and syncing a remote

At the moment, you have your own repository on your local system. But that’s a bit like practicing your guitar in your room your whole life and never jamming out at a party so you can wow your guests with a performance of “Wonderwall.” You need to get this project out where others can see and potentially collaborate on it.

git remote add origin https://github.com/belangerc/mastering-git-web.git
git remote -v
origin	https://github.com/belangerc/mastering-git-web.git (fetch)
origin	https://github.com/belangerc/mastering-git-web.git (push)
git push --set-upstream origin master
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Key points

  • Use git init to set up a Git repository.
  • It’s accepted practice to have a LICENSE file and a README.md file in your repository.
  • Use git add followed by git commit to create the first commit on your new repository.
  • create mode is simply Git telling you what file permissions it’s setting on the files added to the repository.
  • You can create an empty remote on GitHub to host your repository, and you can choose to not have GitHub populate your remote with a LICENSE and README.md by default.
  • Use git remote add origin <remote-url> to add a remote to your local repository.
  • Use git remote -v to see the remotes associated with your local repository.
  • Use git push --set-upstream origin master or git push -u origin master to push the local commits in your repository to your remote, and to start tracking your local branch against the remote branch.

Where to go from here?

You’ve come full circle with your introduction to Git! You started out with cloning someone else’s repo, made a significant amount of changes to it, learned how to stage and commit your changes, how to view the log, how to branch, how to pull and push changes, and now you’re back where you started, except that you are the creator of your very own repository. That feels good, doesn’t it?

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.