How to set up Git at a directory with an empty repository

Assuming you are already at the corresponding directory:

Step 1: git init

This command, quote from git documentation,

creates an empty Git repository – basically a .git directory with subdirectories for objectsrefs/headsrefs/tags, and template files.


Step 2: git add -A

The git add command adds changes in the working directory to the staging area, or more specifically, the index. The -A flag means all files in the entire working tree are updated. Quote about the index:

The “index” holds a snapshot of the content of the working tree, and it is this snapshot that is taken as the contents of the next commit. Thus after making any changes to the working tree, and before running the commit command, you must use the add command to add any new or modified files to the index.


Step 3: git commit -m 'message'

This will create a new commit from the contents in the index, together with the log message after the -m flag. The default master branch is also created at this point.

The new commit is a direct child of HEAD, usually the tip of the current branch, and the branch is updated to point to it.


Step 4: git remote add origin <url>

This command adds a remote named origin for the repository at <url>.


Step 5: git push -u origin master

Firstly, the simplest format of this command is: git push <remote> <branch>, where you push the specified <branch> to the <remote> repository. Since we are using the default “master” branch and the “origin” reference to our remote repository we have just added, thus “origin master” in our command.

For the -u flag, it’s a shorthand for –set-upstream. That is to tell git to make your local branch to track the remote-tracking branch; in this case, the local master branch will track the origin/master branch. More explanation below.

Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository.

Checking out a local branch from a remote-tracking branch automatically creates what is called a “tracking branch” (and the branch it tracks is called an “upstream branch”). Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and which branch to merge in.

Setting an upstream branch has the advantage of telling Git to get/use the correct remote-tracking branch whenever you do a git pull, git push or git rebase, etc.

Published by Mageek

Just another geek in the world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: