GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||
GeezerVison is showing www.kernel.org — gittutorial(7) gittutorial(7) Manual PageNAMEgittutorial - Introduction to Git for beginners DESCRIPTIONGit is a program that lets you collaborate with others on code, back up your code, roll back to an earlier version of your project, track how your project has changed over time, and much more. This tutorial explains how to create a new Git project, make changes to it, and upload the project to the Internet. It usually takes less than an hour if you have a little previous experience with Git, and 1-2 hours if not. We’ll show you how to use the commands What we’ll be doingGit lets you take snapshots of your code, like a checkpoint in a video game. These snapshots are called "commits". Which files to save in a commit is up to you. When Git stores a commit, it stores a copy of every file in the commit
in a database in a folder called . In this tutorial we’re going to show you how to:
We’ll also explain how to use Git to put your code on the Internet. Command line vs graphical interfacesGit is a command line program, but there are many graphical interfaces for Git built by the community. Your text editor might even have a built-in Git integration. In this tutorial, we’ll give instructions for using Git from the command line. We can’t give you instructions for how to do it in your graphical interface because there are so many of them. You can always switch back and forth between using the command line and a graphical interface at any time. Step 1: Make sure Git is installedTo do this tutorial, you’ll need:
You can check if Git is installed by running this in your terminal: $ git --version It should output something like this: git version 2.54.0 The exact version isn’t too important: this tutorial is designed to work with any version of Git after 2.23.0. Step 2: Introduce yourself to GitThe first time you use Git on a computer, it’s a good idea to tell Git your name and public email address. If you don’t, you’ll get a lot of warnings from Git. To do this, run: $ git config --global user.name "Namey McName" $ git config --global user.email [email protected] The name and email is so that other people you’re collaborating with can know who made the changes. You can set them to anything you want. To check if the name and email have been set, you can run these commands. They should display your name and email: $ git config user.name $ git config user.email Next, configure Git to set the default branch name to $ git config --global init.defaultBranch main If there’s no output, then it succeeded.
Step 3: Create an empty folder and 1 fileIn your terminal, create an empty folder and move into it. Later on you could follow the same steps in this tutorial to take an existing project and start managing it with Git, but we’re going to use an empty project so that everyone following this tutorial has the exact same files. For example, run these commands to make a folder called $ mkdir myproject $ cd myproject Next, using your text editor, create a file in this folder, so we have something to work with. Make a file called README.md, with these contents: We're doing a Git tutorial! We need 2 lines in this file so here's a second one Save You can check that the file is in the right place by running $ ls The output should look like this: README.md Step 4: Create a Git repositoryA Git repository lets you save snapshots of your code. Every time you make a snapshot, Git stores a copy of the contents of every file in the snapshot. This might feel unbelievable, but Git uses compression so you can easily store tens of thousands of snapshots without using too much disk space. You can create a Git repository for any folder in your computer by
running the command Run $ git init Git will reply: Initialized empty Git repository in .git/ You might notice a new hidden folder was created, named . Step 5: Make your first commitNext, we’re going to take a snapshot of all the files in the directory
and tell Git to store them. Git calls these snapshots "commits".
Making a commit in Git is a two step process: first you need to run The reason it’s a two step process is that sometimes you might not
actually want Git to store a commit with all the files in a folder.
For example, if you’re compiling your program to a binary, you might
want Git to put the program’s source code in your commit but not
compiled binary. You can also use a . For now, we’re going to store a commit with every file in your folder, as well as in every subfolder. The first step of making a commit is to tell Git which files you
want to include in the commit using $ git add . To check if it succeeded, you can run $ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
Next you can create the commit using the $ git commit -m "Initial commit" The The output should look like this: [main (root-commit) 4a7e367] Initial commit 1 file changed, 2 insertions(+) create mode 100644 README.md You’ve now stored the first version of your project in Git!
|
|
Note
|
All of the advice about how to use SSH in this tutorial is aimed at getting it to work as quickly as possible. Setting up SSH in a secure way is very far outside the scope of this tutorial. If you have a security team in your organization, they might have very different opinions about the appropriate way to configure authentication with Git. |
First, find your SSH key like this:
$ ls ~/.ssh/*.pub
That will output something like this. The exact filename depends on the type
of key. The important thing is that you’re looking for a filename that
ends in .pub ("pub" stands for "public"):
/home/alice/.ssh/id_ed25519.pub
Copy the contents of that file to your clipboard. One way to do that is to run this command (replacing the filename with the right filename), and then copy the output.
cat /home/alice/.ssh/id_ed25519.pub
Then login to your account on the Git host you chose and paste the SSH key into the appropriate place. Depending on the Git host, it’s likely under "SSH keys" in your settings. If it asks you what type of SSH key, look for something like "Authentication Key".
Once you’ve done this, you’re done! Git will automatically use your SSH key to try to connect.
Go to the Git host’s website and create a new Git repository. If there’s an option to "create a README.md", "add a license", or something similar, make sure to say no to all of them.
WARNING: If you create a public repository and push your repository to it, then your name and email address, as well as the contents of any files you committed and every previous version of those files, will be on the public internet. Some Git hosts have the option to create a private repository instead, to keep your information private.
Now, find the address of the repository on your Git host.
If you followed the Mac/Linux instructions to set up an SSH key, look for an "SSH" address like this:
[email protected]:username/repo.git
If you’re on Windows, look for an "HTTPS" address like this:
https://git.example.com/username/repo.git
(where example.com, username and repo will be replaced with the
actual values).
If you can only find an address starting with https:// and you need
an SSH address then you can often translate it to the SSH format by
replacing https:// with git@ and replacing the / after the domain
name with an :. Here’s how:
https://git.example.com/username/repo ^^^^^^^^ ^ replace with "git@" replace with ":"
Finally, we’re ready to send the information in your local Git repository to the one on the internet!
First, tell Git about the other repository by running git remote add
in your terminal
(replace [email protected]:username/repo.git with the address from Step 13):
$ git remote add origin [email protected]:username/repo.git
This tells Git to save the URL [email protected]:username/repo.git in
your Git repository’s configuration under the name origin, so that you
can use it later. If you make a mistake while doing this, you can run
git remote remove origin and try again.
Then tell Git to send the information, with git push
(if this doesn’t work, read the "Troubleshooting git push" section for advice!):
$ git push --set-upstream origin main
On Windows, this should pop up a dialog from Git Credential Manager asking you to log in to the Git host in your browser.
Let’s break down this git push command:
origin refers to the Git repository that we just added with
git remote add ([email protected]:username/repo.git).
You can use any name (not just origin), but using origin often
makes things easier because it’s the default for git push and git pull.
main is the name of your current Git branch.
We haven’t covered branches yet, but main is the default branch name.
--set-upstream tells Git to make this the default for git push for
the main branch. After you set the upstream, git status will
also tell you if you have commits that haven’t been pushed.
The output should look like this:
Enumerating objects: 7, done. Counting objects: 100% (7/7), done. Delta compression using up to 8 threads Compressing objects: 100% (6/6), done. Writing objects: 100% (7/7), 662 bytes | 662.00 KiB/s, done. Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) To example.com:username/repo * [new branch] main -> main branch 'main' set up to track 'origin/main'.
Any time you want to set up a new repository, you can repeat steps
12-14, with one exception: you only need to pass --set-upstream the first time
you push. Afterwards you can just run the command:
$ git push origin main
git pushThere are 3 main errors you might run into when running git push origin main.
Getting errors when running git push is actually a big part of using
Git, so congratulations! If you get to this part of the tutorial, you
get a little bonus experience in debugging.
Here’s a guide to why they’re happening and how to fix them.
Problem 1: You pushed to main, but your branch actually isn’t called main
Here’s the error:
$ git push origin main error: src refspec main does not match any
This might happen if you didn’t set init.defaultBranch to main in
Step 2.
If you want to check the name of your current branch, you can run git
status. For example, in this output, the current branch is "master".
$ git status On branch master ...
To push your branch, you have a few options:
rename your branch to main, by running git branch -m main
and then run git push origin main
run git push origin master
(where master is the name of your actual current branch)
Problem 2: SSH error
Here’s the error:
$ git push origin main The authenticity of host 'github.com (140.82.116.3)' can't be established. ED25519 key fingerprint is: SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])?
Git uses SSH to connect during git push, and this message happens when
SSH connects to a new site that you haven’t connected to before.
The easiest way to handle this is to type "yes" and press Enter.
Problem 3: the repository already has commits in it
Here’s the error:
$ git push origin main ! [rejected] main -> main (fetch first) error: failed to push some refs to 'example.com:username/repo.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally.
If you see an error like this, it means that when you created the repository on your Git host, it added a commit to try to help you out. That’s the "work that you do not have locally" in the error message.
We’re not going to cover collaboration at all in this tutorial, but here are a few more commands that might be useful when using Git on your own:
To download a Git repository to use on your computer you can run
git clone https://example.com/some/repo (git-clone(1)).
To tell Git to "un-add" a file that you added by accident, run
git rm --cached FILENAME (git-rm(1))
To get a list of past commits, run git log (git-log(1))
Every commit has an ID that looks something like this:
497588f53141b531a59ed888c7d2d9b8f8a6742d.
If you see a commit with git log that you want more details about,
you can run git show COMMIT_ID to see what changes that made since
the previous commit (git-show(1))
To get a list of common Git commands, run git help (git-help(1))
Part of the git(1) suite