git教学指南_完整的Git指南

git教学指南

Git is a free and Open Source version control system (VCS), a technology used to track older versions of files, providing the ability to roll back and maintain separate different versions at the same time.

Git是一个免费的开源版本控制系统 (VCS),该技术用于跟踪文件的较早版本,提供了同时回滚和维护单独的不同版本的能力。

Git is a successor of SVN and CVS, two very popular version control systems of the past. First developed by Linus Torvalds (the creator of Linux), today is the go-to system which you can’t avoid if you make use of Open Source software.

Git是SVN和CVS(过去两个非常流行的版本控制系统)的继承者。 最初由Linus Torvalds(Linux的创建者)开发,如今已成为使用系统,如果您使用开放源代码软件,您将无法避免该系统。

分布式VCS (Distributed VCS)

Git is a distributed system. Many developers can clone a repository from a central location, work independently on some portion of code, and then commit the changes back to the central location where everybody updates.

Git是一个分布式系统。 许多开发人员可以从中央位置克隆存储库,在代码的某些部分独立工作,然后将更改提交回每个人都更新的中央位置。

Git makes it very easy for developers to collaborate on a codebase simultaneously and provides tools they can use to combine all the independent changes they make.

Git使开发人员可以轻松地同时在代码库上进行协作,并提供了可用于组合他们所做的所有独立更改的工具。

A very popular service that hosts Git repositories is GitHub, especially for Open Source software, but we can also mention BitBucket, GitLab and many others which are widely used by teams all over the world to host their code publicly and also privately.

托管Git存储库的一种非常流行的服务是GitHub ,尤其是对于开源软件,但是我们还可以提及BitBucket,GitLab和许多其他工具,它们被全世界的团队广泛使用以公开和私有地托管其代码。

如何安装Git (How to install Git)

Installing Git is quite easy on all platforms:

在所有平台上安装Git都很容易:

OSX (OSX)

Using Homebrew, run:

使用Homebrew ,运行:

brew install git

视窗 (Windows)

Download and install Git for Windows.

下载并安装Windows版Git

的Linux (Linux)

Use the package manager of your distribution to install Git. E.g.

使用发行版的软件包管理器来安装Git。 例如

sudo apt-get install git

or

要么

sudo yum install git

如何初始化存储库 (How to initialize a repository)

Once Git is installed on your system, you are able to access it using the command line by typing git.

在您的系统上安装Git之后,您可以通过输入git使用命令行来访问它。

Initializing a repository

Suppose you have a clean folder. You can initialize a Git repository by typing

假设您有一个干净的文件夹。 您可以通过键入以下内容来初始化Git存储库

git init

Running git init

What does this command do? It creates a .git folder in the folder where you ran it. If you don’t see it, it’s because it’s a hidden folder, so it might not be shown everywhere, unless you set your tools to show hidden folders.

该命令的作用是什么? 它在运行它的文件夹中创建一个.git文件夹。 如果您看不到它,那是因为它是一个隐藏文件夹,因此除非您设置工具来显示隐藏文件夹,否则它可能不会在所有地方显示。

The content inside .git

Anything related to Git in your newly created repository will be stored into this .git directory, all except the .gitignore file, which I’ll talk about in the next article.

新创建的存储库中与Git相关的所有内容都将存储在此.git目录中,除了.gitignore文件外,所有文件都将存储在该文件中,我将在下一篇文章中讨论该文件。

如何将文件添加到存储库 (How to add files to a repository)

Let’s see how a file can be added to Git. Type:

让我们看看如何将文件添加到Git。 类型:

echo "Test" > README.txt

to create a file. The file is now in the directory, but Git was not told to add it to its index, as you can see what git status tells us:

创建一个文件。 该文件现在位于目录中,但是并未告知Git将其添加到索引中,因为您可以看到git status告诉我们:

Adding files to a repository

将文件添加到暂存区 (Add the file to the staging area)

We need to add the file with

我们需要添加文件

git add README.txt

to make it visible to Git, and be put into the staging area:

使它对Git可见,并放入暂存区域

Add the file to the staging area

Once a file is in the staging area, you can remove it by typing:

一旦文件位于暂存区中,您可以通过键入以下内容将其删除:

git reset README.txt

But usually what you do once you add a file is commit it.

但是通常在添加文件后执行的操作就是提交它。

如何提交变更 (How to commit changes)

Once you have one or more changes to the staging area, you can commit them using

一旦您对暂存区进行了一个或多个更改,就可以使用

git commit -am "Description of the change"

Commit changes

This cleans the status of the staging area:

这将清理暂存区的状态:

Commit changes status

and permanently stores the edit you made into a record store, which you can inspect by typing git log:

并将您所做的修改永久存储到记录存储中,您可以通过输入git log进行检查:

Run git log

分行 (Branches)

When you commit a file to Git, you are committing it into the current branch.

将文件提交到Git时,就是将其提交到当前分支中。

Git allows you to work simultaneously on multiple, separate branches, different lines of development which represent forks of the main branch.

Git允许您在多个独立的分支上同时工作,这些不同的开发线代表主分支的分支。

Git is very flexible: you can have an indefinite number of branches active at the same time, and they can be developed independently until you want to merge one of them into another.

Git非常灵活:您可以同时激活无限多个分支,并且可以独立开发它们,直到您要将其中一个合并到另一个中为止。

Git by default creates a branch called master. It’s not special in any way other than it’s the one created initially.

默认情况下,Git创建一个名为master的分支。 除了最初创建的方法外,它在任何方面都没有特殊之处。

You can create a new branch called develop by typing

您可以通过键入以下内容创建一个新的分支,称为develop

git checkout -b develop

The git branch command lists the branches that the repository has.

git branch命令列出存储库具有的分支。

When creating the new branch, that branch points to the latest commit made on the current branch. If you switch to it (using git checkout develop) and run git log, you’ll see the same log as the branch that you were previously.

创建新分支时,该分支指向当前分支上的最新提交。 如果您切换到它(使用git checkout develop )并运行git log ,则会看到与先前分支相同的日志。

推和拉 (Push and pull)

In Git you always commit locally. This is a very nice benefit over SVN or CSV where all commits had to be immediately pushed to a server.

在Git中,您总是在本地提交。 与SVN或CSV相比,这是一个非常不错的好处,在SVN或CSV中,所有提交都必须立即推送到服务器。

You work offline, do as many commits as you want, and once you’re ready you push them to the server, so your team members, or the community if you are pushing to GitHub, can access your latest and greatest code.

您可以脱机工作,根据需要进行任意数量的提交,一旦准备好它们送到服务器,那么您的团队成员或社区(如果您要推送到GitHub)就可以访问最新和最出色的代码。

Push sends your changes.

推送发送您的更改。

Pull downloads remote changes to your working copy.

拉下载远程更改到您的工作副本。

Before you can play with push and pull, however, you need to add a remote!

但是,在使用推拉功能之前,您需要添加遥控器

添加遥控器 (Add a remote)

A remote is a clone of your repository, positioned on another machine.

远程是您存储库的克隆,位于另一台计算机上。

I’ll do an example with GitHub. If you have an existing repository, you can publish it on GitHub. The procedure involves creating a repository on the platform, through their web interface, then you add that repository as a remote, and you push your code there.

我将以GitHub为例。 如果您有现有的存储库,则可以将其发布在GitHub上。 该过程涉及通过平台的Web界面在平台上创建存储库,然后将该存储库添加为远程存储,然后将代码推送到该存储库。

To add the remote type

添加远程类型

git remote add origin https://github.com/YOU/REPONAME.git

An alternative approach is creating a blank repo on GitHub and cloning it locally, in which case the remote is automatically added for you

一种替代方法是在GitHub上创建一个空白存储库并在本地克隆它,在这种情况下,将自动为您添加远程

(Push)

Once you’re done, you can push your code to the remote, using the syntax git push <remote> <branch>, for example:

完成后,可以使用git push <remote> <branch>语法将代码推送到远程,例如:

git push origin master

You specify origin as the remote, because you can technically have more than one remote. That is the name of the one we added previously, and it’s a convention.

您将origin指定为远程,因为从技术上讲,您可以有多个远程。 那是我们之前添加的名称,这是一个约定。

(Pull)

The same syntax applies to pulling:

相同的语法适用于拉取:

git pull origin master

tells Git to pull the master branch from origin, and merge it in the current local branch.

告诉Git从origin拉出master分支,并将其合并到当前本地分支中。

矛盾冲突 (Conflicts)

In both push and pull there is a problem to consider: if the remote contains changes incompatible with your set of commits, the operation will fail.

在推和拉中都需要考虑一个问题:如果远程包含与您的提交集不兼容的更改,则该操作将失败。

This happens when the remote contains changes subsequent to your latest pull, which affects lines of code you worked on as well.

当遥控器在您的最新请求之后包含更改时,会发生这种情况,这也会影响您工作的代码行。

In the case of push this is usually solved by pulling changes, analyzing the conflicts, and then making a new commit that solves them.

在推送的情况下,通常可以通过以下方式解决问题:拉出更改,分析冲突然后进行新的提交以解决冲突。

In the case of pull, your working copy will automatically be edited with the conflicting changes, and you need to solve them, and make a new commit so the codebase now includes the problematic changes that were made on the remote.

如果是pull,您的工作副本将自动编辑有冲突的更改,您需要解决它们,并进行新的提交,因此代码库现在包括在远程上进行的有问题的更改。

命令行与图形界面 (Command Line vs Graphical Interface)

Up to now I talked about the command line Git application.

到目前为止,我谈到了命令行Git应用程序。

This was key to introduce you to how Git actually works, but in the day-to-day operations, you are most likely to use an app that exposes you those commands via a nice UI, although many developers I know like to use the CLI.

这是向您介绍Git实际工作方式的关键,但是在日常操作中,您很可能会使用一个通过漂亮的UI向您展示这些命令的应用程序,尽管我知道许多开发人员都喜欢使用CLI 。

The CLI (command line) commands will still prove themselves to be useful if you need to setup Git using SSH on a remote server, for instance. It’s not useless knowledge at all!

例如,如果您需要在远程服务器上使用SSH设置Git,则CLI(命令行)命令仍然会非常有用。 根本不是无用的知识!

That said, there are many very nice apps that are made to simplify the life of a developer that turn out very useful especially when you dive more into the complexity of a Git repository. The easy steps are easy everywhere, but things could quickly grow to a point where you might find it hard to use the CLI.

就是说,有许多非常出色的应用程序可以简化开发人员的工作,而这些应用程序非常有用,尤其是当您深入研究Git存储库的复杂性时。 简单的步骤在任何地方都很容易,但是事情可能很快发展到您可能难以使用CLI的地步。

Some of the most popular apps are

一些最受欢迎的应用是

GitHub桌面 (GitHub Desktop)

https://desktop.github.com

https://desktop.github.com

Free, at the time of writing only available for Mac and Win

免费,撰写本文时仅适用于Mac和Win

(Tower)

https://www.git-tower.com

https://www.git-tower.com

Paid, at the time of writing only available for Mac and Win

付费,在撰写本文时仅适用于Mac和Win

吉特·克拉肯 (GitKraken)

https://www.gitkraken.com

https://www.gitkraken.com

Free / Paid depending on the needs, for Mac, Win and Linux

对于Mac,Win和Linux,根据需要免费/付费

良好的Git工作流程 (A good Git workflow)

Different developers and teams like to use different strategies to manage Git effectively. Here is a strategy I used on many teams and on widely used open source projects, and I saw used by many big and small projects as well.

不同的开发人员和团队喜欢使用不同的策略来有效地管理Git。 这是我在许多团队和广泛使用的开源项目中使用的策略,并且在许多大型项目和小型项目中也都使用过。

The strategy is inspired by the famous A successful Git branching model post.

该策略的灵感来自著名的A成功Git分支模型帖子。

I have only 2 permanent branches: master and develop.

我只有两个常驻分支机构: 掌握发展

Those are the rules I follow in my daily routine:

这些是我日常工作中要遵循的规则:

When I take on a new issue, or decide to incorporate a feature, there are 2 main roads:

当我提出一个新问题或决定合并一项功能时,有两条主要道路:

该功能是快速的 (The feature is a quick one)

The commits I’ll make won’t break the code (or at least I hope so): I can commit on develop, or do a quick feature branch, and then merge it to develop.

我将进行的提交不会破坏代码(或者至少希望如此):我可以进行开发,也可以进行快速功能分支,然后将其合并以进行开发。

该功能将需要多个提交才能完成 (The feature will take more than one commit to finish)

Maybe it will take days of commits before the feature is finished and it gets stable again: I do a feature branch, then merge to develop once ready (it might take weeks).

也许要花几天的时间才能完成功能,然后又变得稳定:我做一个功能分支,然后准备就绪就合并到开发中(可能要花几周的时间)。

修补程序 (Hotfix)

If something on our production server requires immediate action, like a bugfix I need to get solved ASAP, I do a short hotfix branch, fix the thing, test the branch locally and on a test machine, then merge it to master and develop.

如果生产服务器上的某些内容需要立即采取行动,例如我需要尽快解决错误修正,请执行简短的修补程序分支,修复该问题,在本地和测试计算机上测试该分支,然后将其合并以进行开发。

显影不稳定。 Master是最新的稳定版本 (Develop is unstable. Master is the latest stable release)

The develop branch will always be in a state of flux, that’s why it should be put on a ‘freeze’ when preparing a release. The code is tested and every workflow is checked to verify code quality, and it’s prepared for a merge into master.

开发分支始终处于不断变化的状态,这就是为什么在准备发布时应将其置于“冻结”状态的原因。 对代码进行测试,并检查每个工作流程以验证代码质量,并准备将其合并到主代码中。

Every time develop or another hotfix branch is merged into master, I tag it with a version number, and if on GitHub I also create a release, so it’s easy to move back to a previous state if something goes wrong.

每次将development或另一个hotfix分支合并到master中时,我都用一个版本号对其进行标记 ,如果在GitHub上我还创建了一个release ,那么如果出现问题,可以很容易地移回先前的状态。

翻译自: https://flaviocopes.com/git/

git教学指南

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值