git入门教程_git入门

git入门教程

Ever worked on a project and wanted to try something but dreaded messing up what you already had? Ever wish you had a history of all your changes so you can see what you were thinking the time you added that cryptic hack? Well worry no more. That’s what Git was made for and that’s what this guide was made for as well!

曾经在一个项目上工作过,想尝试一下但又害怕弄乱您已有的东西吗? 曾经希望您拥有所有更改的历史记录,以便在添加该隐秘hack时可以看到您的想法吗? 好了,不用担心了。 这就是Git的目的,也是本指南的目的!

I’m using a Mac for this guide. If you’re using Windows, you’ll need to set yourself up with a terminal like Commander or Hyper to follow along.

我正在使用Mac作为本指南。 如果您使用的是Windows,则需要使用Commander或Hyper等终端进行设置。

First, let’s make sure you have git installed. Macs should have Git installed by default. You can check if you have it by opening up your terminal and entering the command git --version. This will print out the version of Git you have installed. If it doesn’t, then you probably don’t have it installed, and will need to install it from https://git-scm.com/. Now that we know we have git, we can get started!

首先,请确保您已安装git 。 Mac默认情况下应安装Git。 您可以通过打开终端并输入命令git --version来检查是否具有它。 这将打印出您已安装的Git的版本。 如果没有安装,则可能您没有安装它,需要从https://git-scm.com/安装它 现在我们知道我们已经有了git,就可以开始了!

Let’s create our first repository! But wait, what is a repository you ask? It’s a way for Git to track what you have worked on. Think of it as a database for all the changes that you will be making in your project. Now, let’s get started! Create a directory that you will be using for your project. I’ll make mine in ~/code/git-starter. In your terminal, do the following

让我们创建第一个存储库! 但是,等等,您要求的存储库是什么? 这是Git跟踪您所做的工作的一种方式。 可以将其视为将在项目中进行的所有更改的数据库。 现在,让我们开始吧! 创建将用于项目的目录。 我将在~/code/git-starter 。 在您的终端中,执行以下操作

~ > cd code
~/code > mkdir git-starter
~/code/git-starter > cd git-starter

Now that you have your project directory, let’s create the repository.

现在您有了项目目录,让我们创建存储库。

~/code/git-starter ❯ git init
Initialized empty Git repository in /Users/shital/code/git-starter/.git/

And, that’s it! You now have a Git repository! If you don’t believe me, you can use git status to see. You’ll see that your directory now has a Git repository.

而且,就是这样! 您现在有了一个Git存储库! 如果您不相信我,可以使用git status进行查看。 您将看到您的目录现在具有一个Git存储库。

~/code/git-starter ❯ git status
On branch masterNo commits yetnothing to commit (create/copy files and use "git add" to track)

Before we proceed with the next steps, you should probably add a .gitignore file at this point. When you’re working on projects, you will have files that you don’t want checked into the repository. For example, you may have design documents or secret files that you don’t really want other people to see. That is where .gitignore will help us out. It’s a file that you place in your repository to tell Git to not bother with the specified files. Here’s a sample that I’ve placed in my project.

在进行下一步之前,您可能应该在此时添加.gitignore文件。 当您处理项目时,您将拥有不想放入存储库中的文件。 例如,您可能拥有您确实不希望其他人看到的设计文档或机密文件。 那就是.gitignore将帮助我们的地方。 这是一个放置在存储库中的文件,告诉Git不必理会指定的文件。 这是我放入项目中的示例。

.DS_Store
node_modules
logs
*.zip

Now, let’s see how Git sees this change.

现在,让我们看看Git如何看待这一变化。

~/code/git-starter ❯ git status
On branch masterNo commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
.gitignorenothing added to commit but untracked files present (use "git add" to track)

As you can see, Git sees that you’ve added .gitignore to your project folder. Now, lets do as Git recommends add this file to staging, which lets Git know that we want it around. You can do this by adding the file.

如您所见,Git看到您已将.gitignore添加到项目文件夹中。 现在,让我们按照Git的建议将其添加到staging中 ,这使Git知道我们想要它。 您可以通过添加文件来做到这一点。

~/code/git-starter ❯ git add .gitignore

We’re not done yet though. Once you’ve staged a file (or files), you have to commit it so Git knows you’re serious about saving it. You do this by, you guessed it, git commit

我们还没有完成。 暂存一个文件(或多个文件)后,必须提交该文件,这样Git知道您很认真地保存它。 您通过猜测来做到这一点, git commit

~/code/git-starter ❯ git commit -m "Adding .gitignore"
[master (root-commit) 3fa3613] Adding .gitignore
1 file changed, 4 insertions(+)
create mode 100644 .gitignore

Note how I added the -m "Adding .gitignore" parameter. This is a shortcut for specifying the commit message so that you aren’t prompted by Git to enter one.

请注意如何添加-m "Adding .gitignore"参数。 这是用于指定提交消息的快捷方式,这样Git不会提示您输入一个。

Now, Git will remember what you’ve changed! Let’s make more changes and see how to connect Git to GitHub in the next part of this series!

现在,Git会记住您所做的更改! 让我们进行更多更改,并在本系列的下一部分中了解如何将Git连接到GitHub!

翻译自: https://medium.com/@shitalgiri95/getting-started-with-git-20756fa6c02f

git入门教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值