git操作手册

介绍 (Introduction)

Hi! I am Sanjula, and in this guide I hope to teach you a little bit about Git including:

嗨! 我是Sanjula ,在本指南中,我希望教您一些有关Git的知识,包括:

  • What Git is什么是Git
  • Why learn Git为什么要学习Git
  • Setting configuration variables设置配置变量
  • Introduction to the help command in GitGit中的help命令简介
  • How to convert an existing project into a local Git repository如何将现有项目转换为本地Git存储库
  • Things to do before the first commit第一次提交之前要做的事情
  • How to add files to the staging area如何将文件添加到暂存区
  • How to remove files from the staging area如何从暂存区中删除文件
  • Making your first commit进行第一次提交
  • How to clone a remote repository如何克隆远程存储库
  • View information about the remote repository查看有关远程存储库的信息
  • How to push your changes to the remote repository如何将您的更改推送到远程存储库
  • How to create a branch for a specific feature or issue如何为特定功能或问题创建分支
  • Push the branch to the remote repository after committing提交后将分支推送到远程存储库
  • How to merge a branch如何合并分支
  • How to delete a branch如何删除分支

Let’s get started!

让我们开始吧!

什么是Git? (What is Git?)

In straightforward terms, Git is an open-source distributed version control system.

简而言之,Git是一个开源的分布式版本控制系统

Version control systems help any software team to manage changes to source code of a product or service over time. It keeps track of all the modifications to the source code in a database. If a critical mistake has been made to the source code, then the developers on a software team can rewind the source code to a version before the erroneous change was made. As a result, version control systems protects source code from disasters, human errors and unintended consequences (when a bug fix breaks another part of the application, for example).

版本控制系统可帮助任何软件团队管理随时间推移对产品或服务的源代码所做的更改。 它跟踪数据库中对源代码的所有修改。 如果对源代码犯了严重错误,则软件团队的开发人员可以将源代码倒带到错误更改之前的版本。 结果,版本控制系统可以保护源代码免受灾难,人为错误和意外后果的影响(例如,当错误修复破坏了应用程序的另一部分时)。

那为什么要学习Git? (So why learn Git?)

Git is the most widely used version control system in the world today. It is a mature and actively maintained open source project originally developed by Linus Torvalds.

Git是当今世界上使用最广泛的版本控制系统。 这是一个由Linus Torvalds最初开发的成熟且积极维护的开源项目。

An astounding amount of software projects rely on Git for version control, including both commercial and open-source projects, especially using the git repository hosting service, GitHub, which is now owned by Microsoft. Hence, the importance of learning Git.

大量软件项目都依赖Git进行版本控制,包括商业项目和开源项目,尤其是使用git存储库托管服务GitHub(现在由Microsoft拥有)。 因此,学习Git的重要性。

本指南的前提条件 (Prerequisite for this guide)

Download and install git here

在此处下载并安装git

检查git版本 (Check version of git)

git --version

If the version number is returned, then it means that git is successfully installed on your computer.

如果返回版本号,则意味着git已成功安装在您的计算机上。

设置配置值 (Setting config values)

Now we need to set the global configuration variables, which are very important, especially if you are working with other developers. The main advantage of this being it is easier to find out who has committed a certain code block, for example.

现在,我们需要设置全局配置变量,这非常重要,尤其是在与其他开发人员一起工作时。 例如,这样做的主要好处是,更容易找出谁提交了某个代码块。

git config --global user.name “Sanjula Madurapperuma”
git config --global user.email “sanjula@mail.com”
git config --list

帮助命令 (Help Command)

As you may notice, config is a verb that has been used frequently so far in this handbook and verbs can also be used as either a prefix or suffix with the help command. We can use the same example (the verb config) from above to explain these commands.

您可能会注意到, config是迄今为止在本手册中经常使用的动词,并且动词也可以通过help命令用作前缀或后缀。 我们可以使用上面的示例(动词config )来解释这些命令。

git help config
git config --help

Both of the above commands perform the same action. Show the manual page for the verb specified. This will be useful to identify more advanced capabilities of git.

以上两个命令执行相同的操作。 显示指定动词的手册页。 这将有助于识别git的更高级功能。

如何从现有代码初始化存储库 (How to initialize a repository from existing code)

If you have a local repository that you want to convert into a git project to start tracking it, then we can start by running the command below within the project directory.

如果您有本地存储库想要转换为git项目以开始对其进行跟踪,那么我们可以通过在项目目录中运行以下命令来开始。

git init

Done! Just like that you have converted your project into a local git repository. If you open up the project folder you will see that a new directory called .git has been created.

做完了! 就像您已经将项目转换到本地git存储库一样。 如果打开项目文件夹,将看到已创建一个名为.git的新目录。

第一次提交之前要做什么 (What to do before the first commit)

Enter the following command to view untracked files:

输入以下命令以查看未跟踪的文件:

git status

If there are files that you don’t want other people to see in the repository, such as files containing personal preferences or those of the IDE, then do the following:

如果您不希望其他人在存储库中看到文件,例如包含个人首选项或IDE的文件,请执行以下操作:

touch .gitignore

To specify which files are not to be added to the git repository, open up a text editor and view the .gitignore file, which can be edited like a normal text file. Now we can enter the following into the file, for example:

要指定不将哪些文件添加到git存储库,请打开文本编辑器并查看.gitignore文件,该文件可以像普通文本文件一样进行编辑。 现在,我们可以在文件中输入以下内容,例如:

.project
*.java

Wildcard characters can also be used. In this case, it has been used to specify not to add all files ending with the .java extension to the repository.

也可以使用通配符。 在这种情况下,已用于指定不将所有以.java扩展名结尾的文件添加到存储库。

Now run git status again

现在再次运行git status

Now you can see that the files we excluded in the .gitignore file are no longer shown in the list of untracked files. The .gitignore file should be committed to the repository in order to maintain the same exclusions in all other places.

现在您可以看到,我们在.gitignore文件中排除的文件不再显示在未跟踪文件的列表中。 .gitignore文件应提交到存储库,以便在所有其他位置保持相同的排除。

将文件添加到暂存区 (Adding files to the staging area)

All this time we were in the working directory. The staging area is where we organize all the files that are tracked and have to be committed before pushing to the git repository. It is a file that stores what has to be included in the next commit.

一直以来,我们都在工作目录中。 暂存区域是我们组织所有被跟踪并必须提交到git仓库的文件的地方。 它是一个文件,存储下一次提交中必须包含的内容。

If you want to add all the files that are currently untracked and you’ve changed to the staging area, then use the following command:

如果要添加所有当前未跟踪的文件,并且已更改到暂存区域,请使用以下命令:

git add -A

If you want to add files individually, then we can give the file name after git add. For example,

如果您想单独添加文件,那么我们可以在git add之后给出文件名。 例如,

git add .gitignore

Now if you type in git status, you will see that the .gitignore file is now in the staging area.

现在,如果您输入git status,您将看到.gitignore文件现在位于暂存区域中。

从临时区域中删除文件 (Removing files from the staging area)

To remove files individually from the staging area, type in the following (for example):

要从暂存区单独删除文件,请输入以下内容(例如):

git reset simple.py

This will remove the file simple.py from the staging area. To see this change, type in git status again.

这将从登台区域删除文件simple.py。 要查看此更改,请再次输入git status。

If you want to remove all the files from the staging area, then run the following:

如果要从登台区域删除所有文件,请运行以下命令:

git reset

Now if we type in git status, we will see that all the files have been changed to untracked files.

现在,如果我们输入git status,我们将看到所有文件都已更改为未跟踪的文件。

进行第一次提交 (Making the first commit)

Now run the following to add all the files to the staging area to be committed.

现在,运行以下命令将所有文件添加到暂存区域以进行提交。

git add -A

If you want, you can run git status to see all the files that will be committed.

如果需要,可以运行git status来查看将要提交的所有文件。

To commit, type in the following.

要提交,请键入以下内容。

git commit -m “Initial Commit”

“-m” specifies a message to be passed describing the commit. Since this is our first commit, we will say Initial Commit.

“ -m”指定要传递的描述提交的消息。 既然这是我们的第一次提交,我们将说“初始提交”。

As you can see, the files have been committed successfully.

如您所见,文件已成功提交。

If you run git status now, you will see that it says the working directory is clean because we have committed the files and haven’t modified any file since.

如果现在运行git status,您会看到它说工作目录很干净,因为我们已经提交了文件,并且此后未修改任何文件。

If we run the following command:

如果我们运行以下命令:

git log

then we can see the commit that we just made, including the hash number of the commit.

然后我们可以看到我们刚刚进行的提交,包括提交的哈希值。

We are now successfully tracking the local project with git!

我们现在使用git成功跟踪了本地项目!

克隆远程存储库 (Cloning a remote repository)

If we want to track an existing remote project with git, then we have to type in a command in the following format:

如果要使用git跟踪现有的远程项目,则必须以以下格式键入命令:

git clone <url> <where to clone>

For an example, I will be using the git repository at this link.

例如,我将在此链接上使用git存储库。

I will first move into the directory that I want to clone the project in, though you can specify this as shown above as well.

我将首先进入要克隆项目的目录,尽管您也可以如上所述指定它。

Go to the repository link given above and click on the “Clone or Download” button, then copy the link given there.

转到上面给出的存储库链接,然后单击“克隆或下载”按钮,然后复制那里给出的链接。

Then enter:

然后输入:

git clone https://github.com/sanjulamadurapperuma/GitDemoMedium.git

Now we have cloned the repository successfully.

现在,我们已经成功克隆了存储库。

If we enter the following command, we will see all the files that are in the local directory now.

如果输入以下命令,我们现在将看到本地目录中的所有文件。

ls -la

查看有关远程存储库的信息 (Viewing information about the remote repository)

If you type in the following command:

如果键入以下命令:

git remote -v

This command will list the locations where the local repository would fetch external commits from and push your commits to the remote repository.

此命令将列出本地存储库将从中获取外部提交并将您的提交推送到远程存储库的位置。

If you were to type the command

如果您键入命令

git branch -a

This will list all the branches which are in the repository, both locally and remotely.

这将列出本地和远程存储库中的所有分支。

In order to demonstrate updating the remote repository, we will make some changes to the files in the repository we cloned.

为了演示更新远程存储库,我们将对克隆的存储库中的文件进行一些更改。

Now that we’ve made a change to our code, the next action we have to make is to push these changes to the remote repository

既然我们已经对代码进行了更改,那么我们下一步要做的就是将这些更改推送到远程存储库中

将更改推送到远程存储库 (Pushing changes to the remote repository)

The following command will show all the changes that have been made to the files.

以下命令将显示对文件进行的所有更改。

git diff

If we enter git status again, we can see that changes have been tracked and that simple.py has been modified.

如果再次进入git status,我们可以看到已跟踪了更改,并且simple.py已被修改。

Now add them to the staging area

现在将它们添加到暂存区

git add -A

Run git status again

再次运行git status

Now we can see that simple.py is ready to be committed.

现在我们可以看到simple.py已准备好提交。

Then enter the commit command with a message

然后输入带有消息的commit命令

git commit -m “Updated hello function”

Now we have to push the committed changes to the remote repository so other people have access to them.

现在,我们必须将已提交的更改推送到远程存储库,以便其他人可以访问它们。

Since the common case is that there are several developers working on a single project, we have to first pull any changes that have been done in the remote repository before pushing our changes to it to avoid conflicts.

由于常见的情况是有多个开发人员在同一个项目上工作,因此我们必须先拉出远程存储库中所做的所有更改,然后再将更改推送到它,以避免冲突。

Run the following command:

运行以下命令:

git pull origin master

Since we are already up-to-date, we can now push our changes to the remote repository.

由于我们已经是最新的,因此现在可以将更改推送到远程存储库。

Now run the following:

现在运行以下命令:

git push origin master

We have successfully pushed our changes to the master branch of the remote repository!

我们已成功将更改推送到远程存储库的master分支!

为功能或问题创建分支 (Creating a branch for a feature or issue)

So far we have been working on our master branch, but this is not how you should be working in git as a developer because the master branch should be a stable release of the project that you are working on. So for every feature or issue, it is usually the norm to create your own branch and then work off that branch.

到目前为止,我们一直在开发master分支,但这并不是您作为开发人员在git中工作的方式,因为master分支应该是您正在处理的项目的稳定版本。 因此,对于每个功能或问题,通常创建一个自己的分支然后再使用该分支是正常的做法。

The command to create a new branch called simple-greeting is as follows:

创建一个称为简单问候的新分支的命令如下:

git branch simple-greeting

Now if you run

现在,如果您运行

git branch

then you will see all the branches in the repository, and the current branch that you are in is highlighted with an asterisk on the left side.

那么您将看到存储库中的所有分支,并且您当前所在的分支将在左侧突出显示一个星号。

If you want to switch to the branch that you just created, then type the following:

如果要切换到刚创建的分支,请键入以下内容:

git checkout simple-greeting

Now if you type git branch you will see that you are now on the simple-greeting branch.

现在,如果您输入git branch,您将看到您现在位于简单问候分支中。

Now we have to make the changes in the project. We’ll switch to the project and define the greeting function.

现在我们必须在项目中进行更改。 我们将切换到项目并定义欢迎功能。

Now we will repeat the process of committing these changes:

现在我们将重复提交这些更改的过程:

git status
git add -A
git commit -m “Greeting Function”

This commit will only change the files in the local branch simple-greeting and it has had no effect on the local master branch or remote repository yet.

此提交只会更改本地分支的简单问候中的文件,并且尚未对本地master分支或远程存储库产生任何影响。

提交后将分支推送到远程存储库 (Pushing branch to remote repository after committing)

Enter the following command:

输入以下命令:

git push -u origin simple-greeting

where origin is the name of the repository and simple-greeting is the branch that we want to push to.

其中origin是存储库的名称,简单问候语是我们要推送到的分支。

Now we have pushed the simple-greeting branch to the remote repository. If you type:

现在,我们将简单问候分支推送到了远程存储库。 如果输入:

git branch -a

We can see that, in our remote repository we now have the simple-greeting branch. Why do we need to push the branch to the remote repository? Because in some companies that’s where they run their unit testing and various others to make sure the code runs well before it’s merged with the master branch.

我们可以看到,在我们的远程存储库中,我们现在有了简单问候分支。 为什么我们需要将分支推送到远程存储库? 因为在某些公司中,他们进行单元测试,而在其他公司中,则要确保代码在与master分支合并之前能很好地运行。

Given that all the testing has come out well (we won’t go into detail here), we can now merge the branch simple-greeting with master branch.

鉴于所有测试都进行得很顺利(我们在这里将不做详细介绍),我们现在可以将分支简单问候语与master分支合并。

合并分支 (Merging a branch)

First, we have to checkout into the local master branch

首先,我们必须结帐到本地master分支

git checkout master

Pull all the changes in the remote master branch:

拉出远程主分支中的所有更改:

git pull origin master

We’ll now see the branches we have merged in so far:

现在,我们将看到到目前为止已合并的分支:

git branch —-merged

simple-greeting branch will not appear here because we haven’t merged it yet.

简单问候分支将不会在这里出现,因为我们尚未将其合并。

To merge simple-greeting with master, enter:

要将简单问候语与master合并,请输入:

git merge simple-greeting

(Keep note that we are in the master branch right now)

(请注意,我们现在位于master分支中)

Now that it’s been merged, we can push the changes to the remote repository master branch.

现在已经合并了,我们可以将更改推送到远程存储库主分支。

git push origin master

Now the changes have been pushed to the master branch in the remote repository.

现在,更改已推送到远程存储库中的master分支。

删除分支 (Deleting a branch)

Since the feature has now been deployed, we can delete the simple-greeting branch. To double check the merge done in the previous section, we can run:

由于该功能现已部署,因此我们可以删除简单问候分支。 要仔细检查上一节中完成的合并,我们可以运行:

git branch --merged

If simple-greeting shows up here, then that means that we have merged all the changes and that the branch can now be discarded.

如果在这里显示简单问候,则意味着我们已经合并了所有更改,并且分支现在可以被丢弃。

git branch -d simple-greeting

Now the branch has been deleted locally.

现在,该分支已在本地删除。

But since we pushed the branch to the remote repository it is still there in the remote repository. This can be seen by running:

但是,由于我们将分支推送到远程存储库,所以它仍在远程存储库中。 通过运行可以看到:

git branch -a

To remove the branch from the remote repository, enter:

要从远程存储库中删除分支,请输入:

git push origin --delete simple-greeting

If we re-run

如果我们重新运行

git branch -a

we can see that the branch has now been deleted from the remote repository as well.

我们可以看到该分支现在也已从远程存储库中删除。

Congratulations!!! You are now a master in basic but critical Git commands!

恭喜!!! 您现在已经掌握了基本但至关重要的Git命令!

For reference or use of this tutorial, here is the public GitHub Repository Link

供参考或使用本教程,这里是公共的GitHub Repository Link

In the meantime, give as many claps as you like to this article if you liked it, comment down below for any concerns. Also please checkout my profile at LinkedIn and follow me on Twitter!

同时,如果您喜欢,可以根据自己的喜好为您提供尽可能多的掌声,如果有任何疑问,请在下面注释掉。 另外,请在LinkedIn上查看我的个人资料,并在Twitter上关注我!

翻译自: https://www.freecodecamp.org/news/the-essential-git-handbook-a1cf77ed11b5/

git操作手册

git操作手册_基本的Git手册相关推荐

  1. fetch git pull 切换_每天提交 Git 太烦?直接用 Python 就好了!

    作者:匿蟒 链接:https://note.qidong.name/2018/01/gitpython 对于协作开发的项目,每天开发前后更新和提交 Git 仓库是基本操作.但作为总是想偷懒的程序员,一 ...

  2. 台达伺服电机选型手册_机械加工工艺师手册_打包下载

    如何[设为星标★],优先推送资料信息? Ta们都在看咱们:机械大佬群                                注意及时保存和下载,资料若失效请拉到本页底部留言,我们将不定时补发! ...

  3. Git操作失败并提示Another git process seems to be running in this......

    问题: Git操作的过程中突然显示Another git process semms to be running in this repository, e.g. an editor opened b ...

  4. git 32位_完整的GIT笔记 快速上手小白教程

    GIT 是什么? Git 是目前世界上最先进的分布式版本控制系统.并且它是一个免费的.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 什么是版本控制系统 版本控制是一种系统,它跟踪一 ...

  5. git 查看分支_系统掌握Git之—探索.git

    文章概述 上文中,我们聊了聊git的配置与一些基本概念,今天来看看.git文件夹里面有什么内容.通过阅读本文,你将获得下面的知识: .git的内部信息. git的分支操作. 图形化git工具. 新建分 ...

  6. idea查看git分支快捷键_开发常用Git/Linux/idea命令快捷键总结(持续更新)

    在开发过程中,会使用越来越多的命令,或快捷键,来帮助我们提高工作效率.本文记录了我在平时积累的常用命令,分享给大家. git命令 基本命令 set LESSCHARSET=utf-8 --idea T ...

  7. git操作提示warning: redirecting to git@github.com:XXXXX

    一.前提: 常用git clone 有两种方式 ssh方式:git@github.com:Nehic/demo.git https方式:https://github.com/Nehic/demo.gi ...

  8. git .git目录提交_压扁Git提交

    git .git目录提交 I use Git every day and I wrote a Git guide and a Git Cheat Sheet in the past. 我每天都使用Gi ...

  9. git 删除分支_深入浅出图解Git,入门到精通(保姆级教程)

    原文链接:https://mp.weixin.qq.com/s/_G3l9urASsFjyrYAZBWhDg 分支管理 Git中比较最重要的一点就是分支的概念,有了分支就有了合并和衍合的操作,「合并」 ...

最新文章

  1. 假如有Thread1、Thread2、ThreaD3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?...
  2. subplot subplots绘制子图
  3. linux screen 命令详解
  4. Nginx(一):概念基础
  5. windows虚拟机_iOS 版虚拟机:在 iPhone 上运行 Windows 系统
  6. Python学习 之 OS模块
  7. Flutter之Stepper源码浅析
  8. 优客365 v2.9版本 后台存在SQL注入
  9. 在udp聊天器里如何给飞秋发消息
  10. 3D控件Aspose.3D 8月新版V17.8发布 | 支持6面多维地图
  11. 大学四年毕业季我用Python写了一个论文降重工具(希望能帮助到学弟学妹)
  12. 速达 从xp系统换成win7
  13. numpy数组切片操作之[:,2]、[-1:,0:2]、[1:,-1:]等都是啥?
  14. 他们联手造了个抢票节”
  15. 三体归零者和盘龙鸿蒙,《三体》中归零者这样的大神级文明已经脱离黑暗森林和猜疑链了吗,为什么?...
  16. postgresql 使用处理 like 'xxoo' 、like 'xxoo%' 、like '%xxoo'、like '%xxoo%'
  17. Linux_常用的磁盘列阵(RAID)
  18. 【kubernetes/k8s源码分析】calico node felix源码分析之一
  19. bzoj3039 玉蟾宫【单调栈】
  20. Python编程从入门到实践 动手试一试 代码合集

热门文章

  1. python表格筛选打印_按行名进行表格筛选:awkpythonR
  2. hash 值重复_程序员:判断对象是否重复,不重写equals和hashcode不行吗?
  3. Invalid Host header 问题解决
  4. iOS基础问答面试题连载-附答案
  5. 遇到大容量磁盘你该怎么办?
  6. 从特急到难产 光伏增补项目抢不抢630?
  7. unity3d教程运行物理机制
  8. 如何让phpmyadmin输入密码再进入
  9. compass安装使用960 Grid System
  10. git revert和git reset的区别