Installing Git
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
|
|
Creating a repository
To create a git repository in the current directory, use the init
subcommand:
|
|
This command will create the .git
directory with the following structure (or similar):
|
|
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 .git/config
.
This is where your repository’s configuration is stored.
There’s also a global git configuration file, usually ~/.config/git/config
.
It applies to all repositories.
Status
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:
|
|
Now, let’s check the status of the repository:
|
|
You’ll get an output that looks like this:
|
|
file1.txt
is current “untracked.” This means it isn’t being followed by git.
Add it to the index using the add
subcommand:
|
|
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.
Commits
You can use the commit
subcommand to create commits.
Try it out:
|
|
If you haven’t used git before (as this user), you’ll get an error like the following:
|
|
This happens because Git needs a name and email address for your commit. Run the commands it provides to set these accordingly:
|
|
The
global
flag 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 -m
flag:
|
|
If you did everything, you’ll have one commit in your tree.
To view the tree, use the log
subcommand.
There are a few extra options you can add which will change the look of the log, which I prefer.
|
|
--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:
|
|
The standard name for the remote is origin
, so your command will look like this:
|
|
3. Push!
The last step is actually two steps combined into one.
You’ll “push” your local changes to the master
branch on the origin
remote.
The command looks like this:
|
|
The --set-upstream
flag is used to make git remember these choices.
Next time you need to push the master
branch, you can just use
|
|