手把手指导您使用 Git
Git GitHub
![](/data/attachment/album/201802/07/095020f7a0abz7xpp0u328.png)


如果您从未使用过 [Git](https://opensource.com/resources/what-is-git)，甚至可能从未听说过它。莫慌张，只需要一步步地跟着这篇入门教程，很快您就会在 [GitHub](https://opensource.com/life/15/11/short-introduction-github) 上拥有一个全新的 Git 仓库。


在开始之前，让我们先理清一个常见的误解：Git 并不是 GitHub。Git 是一套版本控制系统（或者说是一款软件），能够协助您跟踪计算机程序和文件在任何时间的更改。它同样允许您在程序、代码和文件操作上与同事协作。GitHub 以及类似服务（包括 GitLab 和 BitBucket）都属于部署了 Git 程序的网站，能够托管您的代码。


### 步骤 1：申请一个 GitHub 账户


在 [GitHub.com](https://github.com/) 网站上（免费）创建一个账户是最简单的方式。


![](/data/attachment/album/201802/07/095020sdrezlrjk3e2ncz2.png)


选择一个用户名（比如说，octocat123），输入您的邮箱地址和密码，然后点击 **Sign up for GitHub**。进入之后，您将看到下方插图的界面：


![](/data/attachment/album/201802/07/095020o5fld520f0zrggcl.png)


### 步骤 2：创建一个新的仓库


一个仓库（ repository），类似于能储存物品的场所或是容器；在这里，我们创建仓库存储代码。在 `+` 符号（在插图的右上角，我已经选中它了） 的下拉菜单中选择 **New Repository**。


![](/data/attachment/album/201802/07/095021ccassasd742umx7i.png)


给您的仓库命名（比如说，Demo）然后点击 **Create Repository**。无需考虑本页面的其他选项。


恭喜！您已经在 GitHub.com 中建立了您的第一个仓库。


### 步骤 3: 创建文件


当仓库创建完毕后，界面将和下方一致：


![](/data/attachment/album/201802/07/095021fm2caykj6j2hfyjk.png)


不必惊慌，它比看上去简单。跟紧步骤。忽略其他内容，注意截图上的 “...or create a new repository on the command line,”。


在您的计算机中打开终端。


![](/data/attachment/album/201802/07/095022lzfy06qrq9v4vbk0.png)


键入 `git` 然后回车。如果命令行显示 `bash: git: command not found`，在您的操作系统或发行版 [安装 Git](https://www.linuxbabe.com/linux-server/install-git-verion-control-on-linux-debianubuntufedoraarchlinux#crt-2) 命令。键入 `git` 并回车检查是否成功安装；如果安装成功，您将看见大量关于使用该命令的说明信息。


在终端内输入：



```
mkdir Demo

```

这个命令将会创建一个名为 Demo 的目录（文件夹）。


如下命令将会切换终端目录，跳转到 Demo 目录：



```
cd Demo

```

然后输入：



```
echo "#Demo" >> README.md

```

创建一个名为 `README.md` 的文件，并写入 `#Demo`。检查文件是否创建成功，请输入：



```
cat README.md

```

这将会为您显示 `README.md` 文件的内容，如果文件创建成功，您的终端会有如下显示：


![](/data/attachment/album/201802/07/095023nie51jid1wjq1wqw.png)


使用 Git 程序告诉您的电脑，Demo 是一个被 Git 管理的目录，请输入：



```
git init

```

然后，告诉 Git 程序您关心的文件并且想在此刻起跟踪它的任何改变，请输入：



```
git add README.md

```

### 步骤 4：创建一次提交


目前为止，您已经创建了一个文件，并且已经通知了 Git，现在，是时候创建一次<ruby> 提交 <rt>  commit </rt></ruby>了。提交可以看作是一个里程碑。每当完成一些工作之时，您都可以创建一次提交，保存文件当前版本，这样一来，您可以返回之前的版本，并且查看那时候的文件内容。无论何时您修改了文件，都可以对文件创建一个上一次的不一样的新版本。


创建一次提交，请输入：



```
git commit -m "first commit"

```

就是这样！刚才您创建了包含一条注释为 “first commit” 的 Git 提交。每次提交，您都必须编辑注释信息；它不仅能协助您识别提交，而且能让您理解此时您对文件做了什么修改。这样到了明天，如果您在文件中添加新的代码，您可以写一句提交信息：“添加了新的代码”，然后当您一个月后回来查看提交记录或者 Git 日志（即提交列表），您还能知道当时的您在文件夹里做了什么。


### 步骤 5: 将您的计算机与 GitHub 仓库相连接


现在，是时候用如下命令将您的计算机连接到 GitHub 仓库了：



```
git remote add origin https://github.com/<your_username>/Demo.git

```

让我们一步步的分析这行命令。我们通知 Git 去添加一个叫做 `origin` （起源）的，拥有地址为 `https://github.com/<your_username>/Demo.git`（它也是您的仓库的 GitHub 地址） 的 `remote` （远程仓库）。当您提交代码时，这允许您在 GitHub.com 和 Git 仓库交互时使用 `origin` 这个名称而不是完整的 Git 地址。为什么叫做 `origin`？当然，您可以叫点别的，只要您喜欢（惯例而已）。


现在，我们已经将本地 Demo 仓库副本连接到了其在 GitHub.com 远程副本上。您的终端看起来如下：


![](/data/attachment/album/201802/07/095024jyrubjj82zf2uqb6.png)


此刻我们已经连接到远程仓库，可以推送我们的代码 到 GitHub.com（例如上传 `README.md` 文件）。


执行完毕后，您的终端会显示如下信息：


![](/data/attachment/album/201802/07/095024fcii0difz0fvgnc8.png)


然后，如果您访问 `https://github.com/<your_username>/Demo`，您会看到截图内显示的情况：


![](/data/attachment/album/201802/07/095025nzttwrljxokj1br9.png)


就是这么回事！您已经创建了您的第一个 GitHub 仓库，连接到了您的电脑，并且从你的计算机推送（或者称：上传）一个文件到 GitHub.com 名叫 Demo 的远程仓库上了。下一次，我将编写关于 Git 复制（从 GitHub 上下载文件到你的计算机上）、添加新文件、修改现存文件、推送（上传）文件到 GitHub。




---


via: <https://opensource.com/article/18/1/step-step-guide-git>


作者：[Kedar Vijay Kulkarni](https://opensource.com/users/kkulkarn) 译者：[CYLeft](https://github.com/CYLeft) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
If you've never used [Git](https://opensource.com/resources/what-is-git), you may be nervous about it. There's nothing to worry about—just follow along with this step-by-step getting-started guide, and you will soon have a new Git repository hosted on [GitHub](https://opensource.com/life/15/11/short-introduction-github).

Before we dive in, let's clear up a common misconception: Git isn't the same thing as GitHub. Git is a version-control system (i.e., a piece of software) that helps you keep track of your computer programs and files and the changes that are made to them over time. It also allows you to collaborate with your peers on a program, code, or file. GitHub and similar services (including GitLab and BitBucket) are websites that host a Git server program to hold your code.

## Step 1: Create a GitHub account

The easiest way to get started is to create an account on [GitHub.com](https://github.com/) (it's free).

![Create a GitHub account](https://opensource.com/sites/default/files/u128651/git_guide1.png)

opensource.com

Pick a username (e.g., octocat123), enter your email address and a password, and click **Sign up for GitHub**. Once you are in, it will look something like this:

![Signed in to GitHub](https://opensource.com/sites/default/files/u128651/git_guide2.png)

opensource.com

## Step 2: Create a new repository

A repository is like a place or a container where something is stored; in this case we're creating a Git repository to store code. To create a new repository, select **New Repository** from the `+`

sign dropdown menu (you can see I've selected it in the upper-right corner in the image above).

![Create a new repository](https://opensource.com/sites/default/files/u128651/git_guide3.png)

opensource.com

Enter a name for your repository (e.g, "Demo") and click **Create Repository**. Don't worry about changing any other options on this page.

Congratulations! You have set up your first repo on GitHub.com.

## Step 3: Create a file

Once your repo is created, it will look like this:

![New repo on GitHub](https://opensource.com/sites/default/files/u128651/git_guide4.png)

opensource.com

Don't panic, it's simpler than it looks. Stay with me. Look at the section that starts "...or create a new repository on the command line," and ignore the rest for now.

Open the *Terminal* program on your computer.

![Terminal](https://opensource.com/sites/default/files/u128651/git_guide5.png)

opensource.com

Type `git`

and hit **Enter**. If it says command `bash: git: command not found`

, then [install Git](https://www.linuxbabe.com/linux-server/install-git-verion-control-on-linux-debianubuntufedoraarchlinux#crt-2) with the command for your Linux operating system or distribution. Check the installation by typing `git`

and hitting **Enter**; if it's installed, you should see a bunch of information about how you can use the command.

In the terminal, type:

```
````mkdir Demo`


This command will create a directory (or folder) named *Demo*.

Change your terminal to the *Demo* directory with the command:

```
````cd Demo`


Then enter:

```
````echo "#Demo" >> README.md`


This creates a file named `README.md`

and writes `#Demo`

in it. To check that the file was created successfully, enter:

```
````cat README.md`


This will show you what is inside the `README.md`

file, if the file was created correctly. Your terminal will look like this:

![Terminal](https://opensource.com/sites/default/files/u128651/git_guide7.png)

opensource.com

To tell your computer that *Demo* is a directory managed by the Git program, enter:

```
````git init`


Then, to tell the Git program you care about this file and want to track any changes from this point forward, enter:

```
````git add README.md`


## Step 4: Make a commit


So far you've created a file and told Git about it, and now it's time to create a *commit*. Commit can be thought of as a milestone. Every time you accomplish some work, you can write a Git commit to store that version of your file, so you can go back later and see what it looked like at that point in time. Whenever you make a change to your file, you create a new version of that file, different from the previous one.

To make a commit, enter:

```
````git commit -m "first commit"`


That's it! You just created a Git commit and included a message that says *first commit*. You must always write a message in commit; it not only helps you identify a commit, but it also enables you to understand what you did with the file at that point. So tomorrow, if you add a new piece of code in your file, you can write a commit message that says, *Added new code*, and when you come back in a month to look at your commit history or Git log (the list of commits), you will know what you changed in the files.

## Step 5: Connect your GitHub repo with your computer

Now, it's time to connect your computer to GitHub with the command:

```
````git remote add origin https://github.com/<your_username>/Demo.git`


Let's look at this command step by step. We are telling Git to add a `remote`

called `origin`

with the address `https://github.com/<your_username>/Demo.git`

(i.e., the URL of your Git repo on GitHub.com). This allows you to interact with your Git repository on GitHub.com by typing `origin`

instead of the full URL and Git will know where to send your code. Why `origin`

? Well, you can name it anything else if you'd like.

Now we have connected our local copy of the *Demo* repository to its remote counterpart on GitHub.com. Your terminal looks like this:

![Terminal](https://opensource.com/sites/default/files/u128651/git_guide8.png)

opensource.com

Now that we have added the remote, we can push our code (i.e., upload our `README.md`

file) to GitHub.com.

Once you are done, your terminal will look like this:

![Terminal](https://opensource.com/sites/default/files/u128651/git_guide9.png)

opensource.com

And if you go to `https://github.com/<your_username>/Demo`

you will see something like this:

![Demo repo on GitHub](https://opensource.com/sites/default/files/u128651/git_guide10.png)

opensource.com

That's it! You have created your first GitHub repo, connected it to your computer, and pushed (or uploaded) a file from your computer to your repository called *Demo* on GitHub.com. Next time, I will write about Git cloning (downloading your code from GitHub to your computer), adding new files, modifying existing files, and pushing (uploading) files to GitHub.

## 10 Comments