git 命令git 地址_您只需要入门的四个git命令

Git是源代码管理的事实标准,对于开发者来说至关重要。本文介绍了Git的基本工作原理,通过将代码变化比作照片快照来理解。文章强调了四个关键的Git命令:`git add`用于暂存更改,`git commit`创建快照并附带提交消息,`git push`将更改推送到远程仓库,以及`git pull`从远程仓库拉取更新。这四个命令足以让新手开始使用Git进行基本操作。
摘要由CSDN通过智能技术生成

git 命令git 地址

Source control is one of the most important tools you can learn when becoming a developer. Source control is used for many things. It’s used to keep a history of your code. It is used as a collaboration tool for team members working on the same code base. More and more, it has become the standard way to trigger automated code integration and deployment. Though there are several source control tools out there, Git has become the defacto standard.

源代码控制是成为开发人员时可以学习的最重要的工具之一。 源代码管理用于许多事情。 它用于保留代码的历史记录。 它用作在同一代码库上工作的团队成员的协作工具。 越来越多地,它已成为触发自动化代码集成和部署的标准方法。 尽管有多种源代码控制工具,但Git已成为事实上的标准。

Despite this, Git can be intimidating, especially for new developers. Luckily, Just like how you can get by very well only learning the most common words in a spoken language, you can be very successful at Git with just four commands.

尽管如此,Git还是令人生畏,尤其是对于新开发人员而言。 幸运的是,就像仅通过学习口语中最常见的单词就可以很好地获得一样,您只需四个命令就可以在Git上取得成功。

Git如何工作 (How Does Git Work)

The best way to understand git is to think of a stack of photos. Each photo represents a moment in history. The top photo is the most recent snapshot and to go look at previous snapshots, you have to take off the photos to get to it.

理解git的最好方法是考虑一堆照片。 每张照片代表着历史的一刻。 最上面的照片是最新的快照,要查看以前的快照,您必须将照片取下才能拍到。

Git, more or less, works the same way. It is a series of snapshots called commits that show what your code looked like at any given point. You can use Git to travel through time and see what your code looked like at any given snapshot and even compare any two snapshots together and see how they differ, which is called getting a dif of the two commits.

Git或多或少地以相同的方式工作。 这是一系列称为commits的快照,它们显示在任何给定时间点的代码外观。 您可以使用Git穿越时间,查看代码在给定快照中的外观,甚至可以将任意两个快照进行比较,并了解它们之间的区别,这称为获取两个提交的dif

您如何开始使用Git (How Do You Start Using Git)

First of all, you need to install git on your computer. If you have a Mac, I would recommend installing XCode from the app store. XCode will install git automagically for you. For Windows machines, I would recommend following the instructions over at https://gitforwindows.org/

首先,您需要在计算机上安装git。 如果您使用的是Mac,我建议您从应用商店中安装XCode。 XCode将自动为您安装git。 对于Windows计算机,我建议按照https://gitforwindows.org/上的说明进行操作。

After that, you need to have a git project called a repository or repo for short. You can manually turn any directory into a git repo, but when you are just starting with git I would recommend using Github’s new repository wizard. They make it simple to get started and offer many common start files you will want to have for certain types of projects. What it will do is create the repository and then give you the commands that you can paste into your terminal to clone the repository on your local machine. From there you are ready to learn the four most important commands

在那之后,您需要有一个称为存储库或仓库的git项目。 您可以手动将任何目录转换为git repo,但是当您刚开始使用git时,我建议使用Github的新存储库向导 。 它们使上手变得简单,并提供了某些类型项目需要的许多常见的开始文件。 它将要做的是创建存储库,然后为您提供可以粘贴到终端中的命令,以在本地计算机上克隆存储库。 您可以从那里开始学习四个最重要的命令

四个命令 (The Four Commands)

So now that we are in a git repo and we have made some code changes that we want to take a snapshot of, how do we do that? The first thing we need to do is stage our files. Staging is nothing more than deciding which changes will go in this snapshot. I like to think of staging kind of like taking a picture and you are going to decide who and what is in the shot before you take the picture itself.

现在,我们进入了git repo,并做了一些想要快照的代码更改,我们该怎么做? 我们要做的第一件事是暂存文件。 暂存无非是决定将在此快照中进行哪些更改。 我喜欢考虑分阶段进行拍摄,就像拍摄照片一样,您将在拍摄照片之前决定照片中的人物和内容。

To stage the changes, we use the first command-line command: git add . which simply stages all the changes. If you read my Command-Line Basics post, you will remember that whenever you use the period, . , at the command-line, it means the current directory. So typing in git add . means add all changes in the current directory to the stage. If you were looking to do just some of the changes, you could instead type git add <file.name> and it will only stage the single file. This is a cool thing to learn, but when you are starting, I feel it’s best to just use git add . and that way you don’t miss any changes.

为了进行更改,我们使用第一个命令行命令: git add . 只是简单地分阶段进行所有更改。 如果您阅读了《 命令行基础知识》一文 ,您会记住,每当使用句点时, . ,在命令行中,它表示当前目录。 因此输入git add . 表示将当前目录中的所有更改添加到阶段。 如果您只想进行一些更改,则可以输入git add <file.name> ,它将仅git add <file.name>单个文件。 这是一件很酷的事情,但是当您开始学习时,我觉得最好只使用git add . 这样您就不会错过任何更改。

Now the files are staged, but we still don’t have a snapshot of our code. To do that we need to run our next command: git commit -m "<message>". This command will take a snapshot of all the file changes and attach to it a message. That is what the -m flag is for, to add a message to the commit. There are several opinions out there on what should go in your message, but I say don’t worry about those. Each team you work on will have various levels of opinions on what you should do, but in your projects, you should do whatever you want.

现在文件已暂存,但是我们仍然没有代码快照。 为此,我们需要运行下一个命令: git commit -m "<message>". 此命令将为所有文件更改拍摄快照,并在其上附加一条消息。 这就是-m标志的作用,用于向提交中添加一条消息。 关于您的邮件应该包含哪些内容,有几种意见,但是我说不用担心。 您工作的每个团队都会对您应该做的事情有不同程度的意见,但是在您的项目中,您应该做任何您想做的事情。

At this point, we are good. We have staged our changes and put those changes into a single commit. We can keep working, staging, and then committing our changes over and over again. It’s all good and fun to write code on our machine, but the code that is only on our machine is not very valuable. This is where our next two commands come in handy.

在这一点上,我们很好。 我们已经进行了更改,并将这些更改放入单个提交中。 我们可以继续工作,暂存,然后一次又一次地提交更改。 在我们的机器上编写代码既好又有趣,但是仅在我们的机器上编写的代码不是很有价值。 这是我们接下来的两个命令派上用场的地方。

What you will want to do is push your changes from the local repository to the remote repository. If you started your repository on GitHub and cloned it down to your machine, then you don’t have to set up the remote repository. Otherwise, you will have to set that up yourself, which is honestly very easy to do.

您要做的就是push更改从本地存储库push送到远程存储库。 如果您是在GitHub上启动存储库并将其克隆到您的计算机上的,则不必设置远程存储库。 否则,您将必须自己进行设置 ,这实际上很容易做到。

To push your changes up to your remote repository, you simply type: git push at the command prompt and git will push those changes up for you. Then you can go to Github and see all those changes will be reflected in the code there.

要将更改推送到远程存储库,只需在命令提示符下键入: git push ,git就会为您推送这些更改。 然后,您可以转到Github,查看所有这些更改将反映在该代码中。

What is cool is that many of the deployment pipelines, like Netlify or Azure, can listen for changes on your remote repo and automatically build and deploy your code for you making deploying your code as simple as typing git push

很棒的是,许多部署管道(如Netlify或Azure)都可以侦听远程仓库上的更改,并自动为您构建和部署代码,从而使您像键入git push一样简单地部署代码

The final command-line you need to get started is the inverse of the previous one. Often we will want to work on the same code base on multiple machines. If we make changes on machine A and push those changes up to the remote repository, machine B doesn’t automatically get those changes. For the other machine to get those changes on to its local repo you need to type git pull at the command prompt. This command does exactly what it says it does, it pulls the changes from the remote repository back to your local machine.

您需要开始的最后一个命令行与上一个相反。 通常,我们希望在多台计算机上使用相同的代码库。 如果我们在机器A上进行更改并将这些更改推送到远程存储库,则机器B不会自动获得这些更改。 为了使另一台机器将这些更改传递到其本地存储库,您需要在命令提示符下键入git pull 。 该命令完全按照其说的去做,将更改从远程存储库拉回到本地计算机。

That’s it! With those four commands, you can do most of the heavy lifting that you need to maintain a codebase. Once you get comfortable with those 4 commands, you can look at learning about branching, rebasing, and merging but for now those are not important. I will leave you with one piece of advice that has served me well: “Commit early, commit often.” It is an easier task to look back on smaller commits than large ones. There is never a bad time to commit.

而已! 使用这四个命令,您可以完成维护代码库所需的大部分繁重工作。 一旦熟悉了这4个命令,就可以了解分支,重新定级和合并的知识,但目前这些都不重要。 我将为您提供一条对我很有帮助的建议:“尽早提交,经常提交。” 与大提交相比,回顾小提交是一件容易的事。 永远不会有一个糟糕的时间提交。

翻译自: https://medium.com/the-non-traditional-developer/the-only-four-git-commands-you-need-to-get-started-89e88855d348

git 命令git 地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值