Sub topic 1
Starting point: a local repository with commits
This chapter sets up the local repository that the rest of the tutorial connects to GitHub. You will create a repository with one commit and check that it is in a state that can be pushed.
Prerequisites
This chapter assumes you have:
- Git installed and configured with
user.nameanduser.email. - Basic comfort with a terminal: changing directories, creating files, and listing them.
- Some experience making commits with
git addandgit commit. - A GitHub account. You will not use it until the next chapters.
You can check your Git setup with these commands:
git --version
git config user.name
git config user.emailIf the last two print nothing, set them with git config --global user.name "Your Name" and git config --global user.email "[email protected]".
Why start locally
A Git repository lives in a hidden .git directory inside your project folder. It holds the full history, so you can commit, branch, and inspect changes with no network and no GitHub account.
GitHub enters later as a second copy of that history. To send history to it, you need history to send. A push of a repository with no commits has nothing to transfer, so this chapter makes sure a commit exists first.
Create the repository and the first commit
Make a project folder and put one file in it:
mkdir my-project
cd my-project
echo "# my-project" > README.mdNow run the sequence from the plan:
git init
git add .
git commit -m "Initial commit"
git statusEach command does one job:
git initcreates the.gitdirectory and an empty repository.git add .stages every file in the current directory for the next commit.git commit -m "Initial commit"records the staged files as a commit with that message.git statusreports the current state.
If you already have a project, skip the folder creation and run the four commands inside it.
Check the result
After the commit, git status should print something like this:
On branch main
nothing to commit, working tree cleanTwo details matter for later chapters.
- Working tree clean means everything is committed. Uncommitted changes are not sent by a push, so a clean tree tells you that what you see is what will be pushed.
- The branch name may be
mainormaster, depending on your Git version and configuration. Newer Git setups often default tomain, while older ones usemaster. You will need to match this name against GitHub's default branch in a later chapter, so note what you see.
You can also confirm the commit with the history log:
git log --onelineThis prints one line per commit. A new repository shows a single line with a short hash and your message.
What this repository does not have yet
The repository has no remote. You can confirm this:
git remote -vThe command prints nothing, because no remote is configured. A remote is a named pointer to another copy of the repository, usually on a server. The next chapter explains what a remote is and why the common name origin is only a convention.
The diagram shows where you are now and what the tutorial adds.
Common problems at this stage
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: not a git repository` | You are outside the project folder | Run `cd` into the project, then retry |
| `nothing to commit` after `git add .` | The folder is empty, or everything is already committed | Add a file, or check `git log --oneline` |
| `Author identity unknown` | `user.name` or `user.email` is not set | Set both with `git config` |
| An unwanted file appears in the commit | `git add .` stages everything | Add a `.gitignore` before staging, or run `git rm --cached FILE` |
Check for secrets before you move on. Once a commit reaches a public GitHub repository, anyone can read it. Files such as .env or private keys should be ignored now, not removed later.
Exercise
After running git init, git add ., and git commit -m "Initial commit", what do git status and git remote -v show, and why does each result matter for connecting to GitHub?
Show answer
git status shows the branch name and "nothing to commit, working tree clean". This confirms that all work is committed and tells you the branch name (main or master) that you must match with GitHub. git remote -v prints nothing because no remote is configured yet, which is the state in which you can add one.