Chapters

Hide chapters

Git Apprentice

Second Edition · Git 2.32 · Console

Section I: Beginning Git

Section 1: 11 chapters
Show chapters Hide chapters

10. Creating a Repository
Written by Chris Belanger & Bhagat Singh

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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 git-apprentice-web directory from the starter folder into your main GitApprentice folder.

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

cd ../git-apprentice-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/GitApprentice/git-apprentice-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.

As of late 2020, GitHub now uses main as the default branch name for all new repositories. But if you have a plain vanilla install of Git on your local workstation, you’re likely configured with master as your default branch name.

To check this, simply execute the following to see what git init set as your first branch name:

git branch

In my case, Git responds with the following:

* master

To fix this, execute the following command:

git branch -M main

Although Git gives you no output, this command changes the local name of your branch from master to main. Again, it pays to be paranoid with Git, so execute git branch again to confirm that your branch has been renamed to main.

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.

# git-apprentice-web

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

contact: @crispytwit
~GitApprentice/git-apprentice-web $ git status
On branch main

No commits yet

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

	LICENSE
	README.md
	css/
	images/
	index.html

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"
[main (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/<your-repo-name>/git-apprentice-web.git
git remote -v
origin	https://github.com/<your-username>/git-apprentice-web.git (fetch)
origin	https://github.com/<your-username>/git-apprentice-web.git (push)
git push -u origin main
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' 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.
  • If your Git installation uses master as the default branch in new repositories and you want to push to a newly created GitHub repository with main as the default branch, you’ll need to execute git branch -M main to rename the local master branch to main to match your remote.
  • Use git push --set-upstream origin main or git push -u origin main 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.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now