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.
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:
Once there, execute the following command to tell Git to set this directory up as a new repository:
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.
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 master 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"
[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 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 branch -M main
git push -u origin main
* [new branch] main -> main Branch 'main' set up to track remote branch 'main' from 'origin'.
git initto set up a Git repository.
- It’s accepted practice to have a LICENSE file and a README.md file in your repository.
git addfollowed by
git committo create the first commit on your new repository.
create modeis 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.
git remote add origin <remote-url>to add a remote to your local repository.
git remote -vto see the remotes associated with your local repository.
- If your Git installation uses
masteras the default branch in new repositories and you want to push to a newly created GitHub repository with
mainas the default branch, you’ll need to execute
git branch -M mainto rename the local
mainto match your remote.
git push --set-upstream origin mainor
git push -u origin mainto 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?