Writing and Committing Code in GitHub: Creating a Repository

A commit is the smallest unit of work with Git. It represents a small logical group of related changes to the repository. A commit additionally represents a snapshot in time — the state of the entire repository can be represented by referencing a single commit.

Before writing code, you need to create a local repository to store the code. In the following examples, we create a repository in a directory named best-example. Feel free to change best-example to a directory of your choice. Fortunately, this process is quick and painless:

  1. Open the terminal on your computer.

If you don’t know how to do so, see Chapter 1 for guidance.

  1. Go to the directory where you want your project folder to be stored and type the following commands:

$ git init best-example 

$ cd best-example

The first command creates an empty Git repository in the specified directory, best-example. Because the best-example directory doesn’t already exist, Git creates it. The second command changes the current directory to this new directory.

Nearly every Git tutorial I’ve seen that covers initializing a Git repository does it in the current directory by calling git init with no parameters or git init . where the . represents the current directory. People can be forgiven for not real­izing you can both create the repository directory and initialize it in one step by passing in the path to the new repository like we do here. In fact, you can combine both of these commands into a single command: git init best-example && cd best-example. This tip can help you gain the admiration and adulation of your less efficient peers!

Source: Guthals Sarah, Haack Phil (2019), GitHub for Dummies, Wiley.

Leave a Reply

Your email address will not be published. Required fields are marked *