git基本概念与核心命令_Git:了解核心版本控制概念的初学者指南

git基本概念与核心命令

Git is a vital tool in the toolbelt of any developer.

Git是任何开发人员的重要工具。

For example, just the other day I was able to fix a major problem I had pushed to production (totally my fault) in about 20 minutes. This would have probably taken me days to fix without Git.

例如,前几天,我能够解决大约20分钟内推向生产的主要问题(完全是我的错)。 如果没有Git,这可能要花我几天的时间才能解决。

So let's spend some time to really understand the most basic of Git's features: staging and committing.

因此,让我们花一些时间来真正了解Git最基本的功能: 暂存和落实

Note: this writeup doesn’t cover anything relating to GitHub, which is a third-party online web service that allows you to back up the code you’re saving with Git to the cloud. Git is local, GitHub is a cloud-based application, and they are two completely different things with a common purpose.

注意:本文不涉及与GitHub有关的任何内容,后者是第三方在线Web服务,允许您将使用Git保存的代码备份到云中。 Git是本地的, GitHub是基于云的应用程序,它们是两个完全不同的事物,具有共同的目的。

什么是版本控制? (What is Version Control?)

If you're old enough, you likely remember a world before Google Drive/Docs/Sheets where you'd have a situation like this:

如果年龄足够大,您可能还记得Google Drive / Docs / Sheets之前的世界,您会遇到以下情况:

Working on a group project would lead to multiple people trying to make edits to multiple copies of an original document, which led to many duplicates. If two people made edits at the same time, someone would have to go through everything manually and combine those edits together.

在一个小组项目上工作将导致多个人试图对原始文档的多个副本进行编辑,从而导致许多重复。 如果两个人同时进行编辑,则必须有人手动进行所有操作并将这些编辑组合在一起。

There was no real way to control the different versions of the project. It was basically the Wild West. 🤠+😢

没有真正的方法来控制项目的不同版本。 基本上是狂野的西部。 🤠+😢

Git solves this problem 💡

Git解决了这个问题

Maybe you've been coding a new feature in your project, broke something that had been working just fine before, but have no idea where to find the bug or how to fix it. You've since closed the file in your editor, so using "undo" is no longer an option.

也许您已经在项目中编写了一个新功能,破坏了以前可以正常工作的某些功能,但是却不知道在哪里可以找到错误或如何修复它。 此后,您已经在编辑器中关闭了文件,因此不再可以选择“撤消”。

Git solves this problem 💡

Git解决了这个问题

好的,但是Git 如何解决这些问题? (Okay, but how does Git solve these problems?)

The core functionality of Git is to create save points in your files. I like to think of these save points just like they are in video games - checkpoints you reach where, even if you screw everything up after that point, you can always come back and try again without having to start all over.

Git的核心功能是在文件中创建保存点。 我喜欢想到这些保存点,就像它们在视频游戏中一样-检查点到达的位置,即使您在该点之后把所有东西搞砸了,也可以随时回来重试,而不必重新开始。

There are a lot of additional awesome aspects of Git, but at its core this is what it’s all about: creating save points in your code you can come back to later if you want.

Git还有许多其他令人敬畏的方面,但其本质就是这些:在代码中创建保存点,如果需要,您可以稍后再讲。

Git如何工作? (How does Git work?)

This core functionality of Git (creating save points in your project) works in 2 phases:

Git的这一核心功能(在项目中创建保存点)分两个阶段工作:

  1. Adding things (changed code and files) to a staging area to be committed (saved) in a timeline, and

    将事物(已更改的代码和文件)添加到要在时间轴中提交(保存)的暂存区域,以及
  2. Actually committing (saving) those things.

    实际上承诺(保存)这些东西。

强制类比 (Obligatory Analogy)

Think of these two phases as if you were creating an old-school photo album, one where you printed photos and put them in an actual book. In case you're too young, this is what I'm talking about:

将这两个阶段想像成要创建一本老式的相册,即在其中打印照片并将其放入一本实际的书中。 如果您太年轻,这就是我在说的:

First, you take a bunch of photos with your camera. Taking a photo alone doesn't affect the photo album because you can always pick and choose which photos you want to include in the album. You can take bad photos and just re-take them if necessary.

首先 ,您用相机拍摄了一堆照片。 单独拍摄照片不会影响相册,因为您可以随时选择要包含在相册中的照片。 您可以拍摄不良照片,并在必要时重新拍摄。

Next, you choose which of all the photos you’ve taken you want to save in the photo album. Imagine you’ve printed them out already and you set them next to the empty page in your photo album. You’re creating a sort of “staging area” where you haven’t yet glued the photos down to the page in the photo album, but you’re preparing to do so soon.

接下来 ,您选择要拍摄的所有照片中的哪一个要保存在相册中。 假设您已经将它们打印出来,然后将它们放在相册中的空白页旁边。 您正在创建一种“暂存区”,尚未将照片粘贴到相册中的页面上,但您准备尽快这样做。

Lastly, once you’re ready, you glue your photos down to the page and essentially cement them in time. An important part of a good photo album is including some sort of message or title that describes the events surrounding those photos.

最后 ,一旦准备好,就可以将照片粘贴到页面上,并基本上将它们固定在一起。 好的相册的重要部分是包含描述这些照片周围事件的某种消息或标题。

Once you’ve done this, you can always come back to this page in your photo album and remember what things were like at that time in your life (for better or worse).

完成此操作后,您随时可以返回相册中的此页面,并记住当时的生活状况(好坏)。

这与Git有什么关系? (How does this relate to Git?)

Let’s relate this analogy to Git.

让我们将此类比与Git联系起来。

  • Taking photos is like modifying project files (writing code, creating files, or deleting files).

    拍照就像修改项目文件(编写代码,创建文件或删除文件)。
  • Choosing the photos you want in your photo album is like adding your changes to a “staging area.”

    在相册中选择想要的照片就像将更改添加到“暂存区”。
  • Gluing the photos into the photo album page is like committing (saving) your changes into a timeline of changes.

    将照片粘贴到相册页面就像将更改提交(保存)到更改时间轴中。

Let's break these down a bit more.

让我们进一步分解这些。

拍照就像修改项目文件一样 (Taking the photos is like modifying your project files)

Taking the photos is like making changes to your project - writing new code, adding images, deleting old files, and so forth. You’re creating the content you eventually want to save in the Git commit (“save point”). It’s still a work in progress, and you can always write, re-write, and delete anything you want without anything being "permanently" saved quite yet.

拍照就像对您的项目进行更改-编写新代码,添加图像,删除旧文件等等。 您正在创建最终要保存在Git提交中的内容(“保存点”)。 它仍在进行中,您可以随时编写,重写和删除所需的任何内容,而无需“永久”保存任何内容。

The only thing Git is doing at this point in time is watching to see if anything has changed since the last time you committed (saved) your code. If you add a line of code then delete it, Git will see that nothing overall has changed. If you write 500 lines of code across dozens of files, Git knows exactly which lines of code were added to which files and keeps track in its memory of those changes. It won’t commit anything to the timeline of changes until you tell it to, but it’s watching you closely 👀

Git此时唯一要做的就是观察自上次提交(保存)代码以来是否发生了任何变化。 如果添加一行代码然后将其删除,Git将看不到总体上没有任何变化。 如果您在数十个文件中编写500行代码,则Git会确切知道将哪些代码行添加到了哪些文件中,并会在内存中跟踪这些更改。 除非您告知更改,否则它不会对更改的时间轴作出任何承诺,但会密切注视着您👀

Note: remember we're still referring to a process totally separate from saving your changes to your hard drive. Modern text editors can save your code every second or so, but that's not what we're referring to here. When I refer to "saving" with Git, I mean creating a commit that saves your changes to a timeline.
注意:请记住,我们仍然将流程与将更改保存到硬盘完全分开。 现代的文本编辑器可以每秒钟左右保存您的代码,但这不是我们在这里指的。 当我提到Git的“保存”时,我的意思是创建一个提交来将您的更改保存到时间轴。

打印/准备所选照片就像将项目更改添加到“临时区域” (Printing/prepping the chosen photos is like adding your project changes to the “staging area”)

With Git, this is the phase that happens before creating a new commit in your code. This process is called “adding to the staging area.” Adding to the staging area doesn’t create the commit, it just prepares the commit.

使用Git,这是在代码中创建新提交之前发生的阶段。 此过程称为“添加到暂存区”。 添加到暂存区域不会创建提交,而只是准备提交。

After adding some files to the staging area, you may realize you still have some changes to be made. No problem! Since Git hasn’t actually saved (committed) anything yet, you can simply make the new changes you want and then add those changes to the staging area too, even if those changes happen in the same file as a previously-added file. This would be like taking some new photos you decided you want to add to the photo album page and printing them.

在将一些文件添加到暂存区后,您可能会意识到仍然需要进行一些更改。 没问题! 由于Git尚未真正保存(提交)任何内容,因此您可以简单地进行所需的新更改,然后将这些更改也添加到登台区域,即使这些更改与先前添加的文件在同一文件中也是如此。 这就像拍摄一些您决定要添加到相册页面的新照片并打印出来。

将照片粘贴到相册中就像提交代码一样 (Gluing photos into the album is like committing your code)

This is the second (and final) phase to creating your “save point” (commit). There’s one major requirement when you create a commit: you must include a message. In the photo album, you can write a title or message to give some information to a future on-looker about what those photos meant to you. In Git, you write a message to describe the changes you’re saving to your code base.

这是创建“保存点”(提交)的第二个(也是最后一个)阶段。 创建提交时有一个主要要求: 必须包含message 。 在相册中,您可以写标题或消息,以向将来的旁观者提供有关这些照片对您意味着什么的信息。 在Git中,您编写了一条消息来描述要保存到代码库中的更改。

If you write a poor commit message, looking back at your code history won’t be very helpful to anyone including yourself. (What good is a message like “made some changes” if you have no idea what those changes are? Imagine finding a page in a photo album that just said “Here are some people”…) Do yourself and others a favor and always use good, descriptive commit messages that describe the feature or fix you’re adding to the code base.

如果您写的提交信息很差,那么回顾您的代码历史记录对包括您自己在内的任何人都不会有太大帮助。 (什么善若水“做了一些改动,”如果你不知道这些变化的消息?想象一下,在一个相册找到一个页面,只是说:“这里有一些人” ......)你自己和别人一个忙, 总是使用好的描述性提交消息 ,描述您要添加到代码库中的功能或修复程序。

建立 (Set up)

Enough with the analogies, let's get moving!

有了足够的类比,让我们开始吧!

安装 (Installation)

First, you might already have Git installed. Open a Terminal/Command Prompt and try running git --version. If it spits out a version number, skip ahead. If it doesn’t know what you mean by git, you need to install it. Follow these instructions to do so for your operating system.

首先,您可能已经安装了Git。 打开终端/命令提示符,然后尝试运行git --version 。 如果显示版本号,请跳过。 如果它不知道git意思,则需要安装它。 请按照以下说明对您的操作系统执行此操作。

创建一个Git仓库 (Create a Git repo)

Git only knows to track projects that you’ve set up to be Git repositories. In the above analogy, we couldn’t glue photos to a photo album if we didn’t have a photo album in the first place.

Git只知道跟踪您已设置为Git存储库的项目。 在上述类比中,如果我们首先没有相册,则无法将照片粘贴到相册上。

When you’re ready to begin a new project, one of the first steps you should do (after creating the project folder) is to run:

当您准备开始一个新项目时,应该执行的第一步(在创建项目文件夹之后)是运行:

git init

This allows Git to start tracking any changes you make to your project right away. Under the hood, it creates a new hidden .git folder with all the stuff inside it needs to track your changes. You’ll rarely need to go inside this folder unless you’re setting up some advanced stuff, so for now just plan on leaving it alone.

这使Git可以立即开始跟踪您对项目所做的任何更改。 在后台,它将创建一个新的隐藏的.git文件夹,其中包含跟踪更改的所有内容。 除非您要设置一些高级内容,否则您几乎不需要进入该文件夹,因此,现在就计划将其保留。

对您的项目进行更改 (Make changes to your project)

For the tutorial below, I’m going to keep things as simple as possible - a project folder that is a Git repo with a single README.md markdown file inside. If it helps, you can imagine that each change I make to the README file represents some new feature and dozens or hundreds of lines of new code. That’ll make me seem more impressive, too. 👌🏻😉

对于下面的教程,我将使事情尽可能简单-一个项目文件夹,它是一个Git存储库,其中包含单个README.md markdown文件。 如果有帮助,您可以想象我对自述文件所做的每项更改都代表了一项新功能以及数十行或数百行新代码。 那也会让我看起来更令人印象深刻。 👌🏻😉

基本命令 (Basic Commands)

git status (git status)

I like to think of this as a "sanity check" to help me know what Git believes is going on right now. (What changes it has noticed, if everything is working as it should, etc.)

我喜欢将其视为“健全性检查”,以帮助我了解Git认为正在发生的事情。 (它注意到的变化是什么,如果一切都按预期进行,等等。)

It tells me I'm on the master branch (I'll be creating a separate article about branching at a later time), that I have no previous commits yet, and that I don't have anything to commit now. (Meaning, Git doesn't see anything about this folder to save at all).

它告诉我我在master分支上(稍后我将创建有关分支的另一篇文章),我以前没有提交,现在没有任何提交。 (意味着,Git根本看不到要保存的有关此文件夹的任何内容)。

Now I’ll add my README.md file and run git status again:

现在,我将添加我的README.md文件并再次运行git status:

Git sees that I added a new file to my project! Cool. Now that this awesome new project is underway, let’s create a save point. Because, you know, it’d be hard to get back to this point from scratch!

Git看到我在项目中添加了一个新文件! 凉。 现在,这个很棒的新项目正在进行中,让我们创建一个保存点。 因为,您很难从头回到这一点!

git add (git add)

The git add command is how we put things in the staging area. Like printing the photos we took before gluing them in our photo album page. However, we need to tell Git what we want to add to the staging area. (Just entering git add will tell you that nothing was specified so nothing was added.) I'll put the filename of the file I want to add using Git:

git add命令是我们将内容放入暂存区的方式。 就像打印我们拍摄的照片一样,然后将其粘贴到相册页面中。 然而,我们需要告诉Git的我们要添加到临时区域。 (仅输入git add会告诉您未指定任何内容,因此未添加任何内容。)我将使用Git放入要添加的文件的文件名:

git add README.md
git status

Using git status again tells us we have a new file in the staging area, but it’s turned green now! That's my easy way of knowing it has been added to the staging area.

再次使用git status告诉我们暂存区中有一个新文件,但是现在变成绿色了! 这是我很容易知道它已添加到暂存区的方法。

Basically what git add README.md did was tell Git “I want to include any changes made to README.md since the last commit to be included in the upcoming commit.”

基本上, git add README.md所做的是告诉Git“我希望包括自上次提交以来对README.md所做的任何更改。”

However, adding files to the staging area one at a time like this is burdensome, especially since many tasks require you to work with many files. Instead of having to remember and specify every file you’re working on, you can use a sort of “catchall” that will automatically add every file in which you’ve made changes to the staging area. My preferred way to do this is to use:

但是,像这样一次一次将文件添加到暂存区很麻烦,尤其是因为许多任务需要您处理许多文件。 您不必记住并指定要处理的每个文件,而可以使用一种“ catchall”,该文件将自动将您在暂存区域中进行了更改的每个文件添加到其中。 我这样做的首选方法是使用:

git add -A

(The -A flag says to add all the files with changes to them to the staging area).

( -A标志表示将更改后的所有文件添加到登台区域)。

Note: you’ll often see people use git add . to accomplish the same goal of adding all changes to the staging area. While this works, it requires you to be in the project root directory to ensure you grab all the changes. (The . is shorthand for “the current directory”). So if you cd into a nested directory but made changes to a file outside that directory and try to use git add ., you’ll miss those changed files when trying to add them to the staging area. git add -A, however, works for the whole project no matter where you are currently located in the Terminal.

注意:您会经常看到人们使用git add . 完成将所有更改添加到登台区域的相同目标。 在此过程中,它要求您位于项目的根目录中,以确保获取所有更改。 ( .是“当前目录”的简写)。 所以,如果你cd到嵌套目录,但所做的更改到该目录以外的文件,并尝试使用git add . ,尝试将它们添加到暂存区时会错过这些更改的文件。 git add -A不管您当前在终端在哪里,它都适用于整个项目。

git commit (git commit)

Once you’re ready to create a commit, you’ll do so with the git commit command. However, remember how you’re required to leave a message? If you just run git commit and hit Enter, you’ll be popped into a Terminal-based editor like Vi or Nano to fill in a message for that commit.

一旦准备好创建提交,就可以使用git commit命令来完成。 但是,还记得您需要如何留言吗? 如果您只运行git commit并按Enter,您将被弹出到基于终端的编辑器(如Vi或Nano)中,以填写有关该提交的消息。

Instead, you can leave a message right in-line with your git commit by using the -m flag followed by a string message in quotation marks. Something like this:

取而代之的是,您可以通过-m标志后跟带引号的字符串消息,使消息与git commit 。 像这样:

git commit -m "Added some really important information to the README"

Assuming everything else was in order before you committed, you’ve now successfully committed your code with Git! Now you have a checkpoint that you can always return back to if everything goes wrong from here on.

假设在提交之前所有其他操作都已按顺序进行,那么现在您已成功使用Git提交了代码! 现在,您有了一个检查点,如果从此处开始出现所有错误,您始终可以返回。

Let's take another look at this process in the form of a gif:

让我们再以gif的形式看一下这个过程:

Note: the gif uses git add, but I should have used git add -A to be more precise. Also, use better commit messages! Please pardon my mistakes 🙏🏻

注意:gif使用git add ,但是我应该使用git add -A更精确。 另外,使用更好的提交消息! 请原谅我的错误🙏🏻

git log (git log)

You can take a peek at your commit history by running git log. Using the arrow keys, you can scroll forward and backward in time to check commit dates, messages, and authors (the person who made the commit). Along with each of these comes a “commit hash”, which is essentially a (long) unique ID for the commit which can be used to reference it later if needed.

您可以通过运行git log查看提交历史git log 。 使用箭头键,您可以在时间上向前和向后滚动,以查看提交日期,消息和作者(进行提交的人)。 伴随着这些,每个都有一个“提交哈希”,它实质上是提交的(长)唯一ID,可以在以后需要时用来引用它。

时间旅行 (Time Travel)

"So you keep talking about how Git can let me jump forward and backward in time… how do I actually do that?"

“所以您一直在谈论Git如何让我及时向前和向后跳跃……我实际上是如何做到的?”

git checkout (git checkout)

The term “checkout” refers to the process of switching from one commit to another. Remember the unique ID ("hash") each commit receives? I can look back at my commit history, choose one of those unique commit hashes, and check it out with the git checkout command. If the commit I want to look at has a hash of a2 (in full they’re much longer than this - like 0c9b8f7c23dea4cf0a0285f14211124b6d5891e9), I can run:

术语“签出”是指从一个提交切换到另一个提交的过程。 还记得每个提交接收的唯一ID(“哈希”)吗? 我可以回顾一下我的提交历史,选择那些唯一的提交哈希,然后使用git checkout命令将其git checkout 。 如果我要查看的提交具有a2的哈希值(完全比这0c9b8f7c23dea4cf0a0285f14211124b6d5891e9 )-像0c9b8f7c23dea4cf0a0285f14211124b6d5891e9 ),我可以运行:

git checkout a2

Suddenly, my whole code base will zoom back in time and everything will look like it did right after I made that commit. (This can be scary because it may look like you’ve lost all the updates since this commit, but don’t worry! They’re still around, waiting for you... in the future!)

突然,我的整个代码库都会随着时间的推移而缩小,并且一切看起来都像在我提交该内容之后所做的一样。 (这可能会令人恐惧,因为自从提交以来,您似乎已经丢失了所有更新,但是请放心!它们还在附近,等着您…… 将来! )

In gif form:

gif格式:

Now that you're back in time, you'll see a message from Git. Something like:

现在您回到了过去,您将看到来自Git的消息。 就像是:

In this state, you're no longer on the master branch, which is to say you can make experimental changes here and even create new commits, all without losing your code on the master branch (commit hash a3 in the above example).

在这种状态下,您不再位于master分支上,也就是说,您可以在此处进行实验性更改,甚至创建新的提交,而所有操作都不会丢失您在master分支上的代码(在上例中,提交哈希a3 )。

Again, I plan on covering branching at another time, but this is just to drive home the point that Git is a really powerful tool when it comes to saving multiple versions of your code.

同样,我计划在另一时间介绍分支,但这只是为了说明Git在保存代码的多个版本时是一个非常强大的工具。

结论 (Conclusion)

There's a million things you could learn about Git, but without understanding the core concepts, it will always be a bit of a mystery.

您可以学习有关Git的上百万种内容,但是如果不了解核心概念,这将永远是一个谜。

However, if you really want to learn and become familiar with Git, the best thing you can do is play around and experiment with it. On orientation day at V School, I always tell our new students that the easiest way to learn something new is to do it the hard way.

但是,如果您真的想学习并熟悉Git,那么最好的办法就是尝试并尝试一下。 在V学校的迎新日,我总是告诉我们的新学生, 学习新东西的最简单方法是艰苦的学习

This means forcing yourself to do more than just read an article and hope to learn the content. So go spin up a new Git repository in an empty folder, start adding files, use git status and git log a bunch to see what things look like, and consider downloading Sourcetree by Atlassian to visualize the state of your repository while you're messing around with things.

这意味着强迫自己做更多的事情,而不仅仅是阅读文章并希望学习其中的内容。 因此,在一个空文件夹中旋转一个新的Git存储库,开始添加文件,使用git statusgit log一堆来查看情况,并考虑下载Atlassian的Sourcetree以便在混乱时可视化存储库的状态周围的东西。

Once you get over the learning curve with Git, you'll wonder how you ever did any development without it!

一旦您克服了Git的学习难题,您会想知道如果没有它,您将如何进行开发!

翻译自: https://www.freecodecamp.org/news/git-the-laymans-guide-to-understanding-the-core-concepts/

git基本概念与核心命令

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值