Part of the Intro to Git series.
You can find Git in basically every package manager in existence. If you don’t use a package manager, you can get git from the official downloads page.
This tutorial uses git version
2.x.x. Check which version you have by running
$ git --version
Creating a repository
To create a git repository in the current directory, use the
$ git init
This command will create the
.git directory with the following structure (or similar):
.git ├── branches/ ├── config ├── description ├── HEAD ├── hooks/ │ ├── applypatch-msg.sample* │ ├── commit-msg.sample* │ ├── fsmonitor-watchman.sample* │ ├── post-update.sample* │ ├── pre-applypatch.sample* │ ├── pre-commit.sample* │ ├── pre-merge-commit.sample* │ ├── prepare-commit-msg.sample* │ ├── pre-push.sample* │ ├── pre-rebase.sample* │ ├── pre-receive.sample* │ └── update.sample* ├── info/ │ └── exclude ├── objects/ │ ├── info/ │ └── pack/ └── refs/ ├── heads/ └── tags/ 9 directories, 16 files
Most of these files you’ll never touch, and the tree will grow as your git tree does.
One of the very useful file is
This is where your repository’s configuration is stored.
There’s also a global git configuration file, usually
It applies to all repositories.
Go ahead and make a file with some content. The steps to do this will depend on your operating system and preferred applications, but for UNIX-like systems, this will do the trick:
$ echo "Some very important information" > file1.txt
Now, let’s check the status of the repository:
$ git status
You’ll get an output that looks like this:
On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) file1.txt nothing added to commit but untracked files present (use "git add" to track)
file1.txt is current “untracked.” This means it isn’t being followed by git.
Add it to the index using the
$ git add file1.txt
If you check the status again, you’ll see that
file1.txt is listed as “to be committed.”
Now let’s create our first commit.
You can use the
commit subcommand to create commits.
Try it out:
$ git commit
If haven’t used git before (as this user), you’ll get an error like the following:
*** Please tell me who you are. Run git config --global user.email "email@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'user@hostname.(none)')
This happens because Git needs a name and email address for your commit. Run the commands it provides to set these accordingly:
$ git config --global user.email "firstname.lastname@example.org" $ git config --global user.name "Your Name"
globalflag will set this in your global git configuration, so these settings will apply to all repositories. Omit that flag to set your identity in only the current repo.
Now let’s create the commit. Use the same command from earlier.
You’ll notice that git will either open a text editor or give you an error message.
This is because it needs a commit message.
If you enter one in the text editor, then save and close the file, it’ll use that.
Otherwise, specify one on the command line using the
$ git commit -m "create our first file"
If you did everything, you’ll have one commit in your tree.
To view the tree, use the
There are a few extra options you can add which will change the look of the log, which I prefer.
$ git log --graph --all
--graph: show a text-based tree representation next to the commits.
--all: show commits from all branches.
Uploading to a server
If you want to share your repository, chances are you’ll upload it to a git server like GitLab or GitHub. There are a few steps to this.
1. Create a repo on the server
First, you’ll have to create a repository on whichever server you choose to use. I won’t go over how to do that as it depends on the server.
2. Set the remote URL
The next step is to tell git where it needs to upload to. When you create your repository, it’ll give you a URL to push to. This is also sometimes called the clone URL.
To tell git about this, you’ll add a “remote” repository:
$ git remote add <remote name> <url>
The standard name for the remote is
origin, so your command will look like this:
$ git remote add origin <url>
The last step is actually two steps combined into one.
You’ll “push” your local changes to the
master branch on the
The command looks like this:
$ git push --set-upstream origin master
--set-upstream flag is used to make git remember these choices.
Next time you need to push the
master branch, you can just use
$ git push