git基础知识_在不到10分钟的时间内学习Git基础知识
git基础知识
by Gowtham Venkatesan
通过Gowtham Venkatesan
在不到10分钟的时间内学习Git基础知识 (Learn the Basics of Git in Under 10 Minutes)
Yes, the title is a clickbait. There is no way you can understand the basics of git technology in just 10 minutes. But you can get pretty close in about 25 minutes. And that is the purpose of this article.
是的,标题是一个点击诱饵。 您仅用10分钟就无法理解 git技术的基础知识。 但是,您可以在25分钟左右的时间内接近自己。 这就是本文的目的。
If you want to get started on learning about Git technology, you’ve come to the right place. This is a comprehensive beginner’s guide to Git. There are many clients for Git. The technology is all the same no matter the client. But in this guide we’ll be using GitHub to understand Git.
如果您想开始学习Git技术,那么您来对地方了。 这是Git的综合初学者指南。 Git有很多客户。 无论客户如何,技术都是一样的。 但是在本指南中,我们将使用GitHub来了解Git。
让我们开始吧! (Let’s get started!)
什么是版本控制? (What is Version Control?)
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. So ideally, we can place any file in the computer on version control.
版本控制是一个记录一段时间内对一个文件或一组文件的更改的系统,以便您以后可以调用特定版本。 因此,理想情况下,我们可以将计算机上的任何文件置于版本控制上。
嗯...好吧...但是为什么呢? (Umm… Okay… But Why Tho?)
原因如下: (Here’s Why:)
A Version Control System (VCS) allows you to revert files back to a previous state, revert the entire project back to a previous state, review changes made over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also means that if you screw things up or lose files, you can generally recover easily. And sometimes you just want to know “who wrote this crap”, and having access to that information is worthwhile ?.
版本控制系统(VCS)允许您将文件还原到以前的状态,将整个项目还原到以前的状态,查看一段时间内所做的更改,查看谁最后修改了可能导致问题的内容,谁引入了问题以及何时以及更多。 使用VCS还意味着,如果您弄糟了文件或丢失了文件,通常可以轻松恢复。 有时您只是想知道“谁写了这个废话” ,而获得这些信息值得吗?
那么什么是Git? (So What is Git?)
Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. Git is a Distributed Version Control System. So Git does not necessarily rely on a central server to store all the versions of a project’s files. Instead, every user “clones” a copy of a repository (a collection of files) and has the full history of the project on their own hard drive. This clone has all of the metadata of the original while the original itself is stored on a self-hosted server or a third party hosting service like GitHub.
Git是一个版本控制系统,用于跟踪计算机文件中的更改并在多人之间协调这些文件上的工作。 Git是一个分布式版本控制系统 。 因此,Git不必依赖中央服务器来存储项目文件的所有版本。 相反,每个用户都“克隆”存储库的副本(文件的集合),并在自己的硬盘上拥有项目的完整历史记录。 此克隆具有原始文档的所有元数据,而原始文档本身存储在自托管服务器或第三方托管服务(如GitHub)上。
Git helps you keep track of the changes you make to your code. It is basically the history tab for your code editor(With no incognito mode ?). If at any point while coding you hit a fatal error and don’t know what’s causing it you can always revert back to the stable state. So it is very helpful for debugging. Or you can simply see what changes you made to your code over time.
Git可以帮助您跟踪对代码所做的更改 。 它基本上是代码编辑器的“历史记录”选项卡(无隐身模式?)。 如果在编码过程中的任何时候遇到致命错误,并且不知道是什么原因造成的,您可以随时恢复到稳定状态。 因此对调试非常有帮助。 或者,您可以简单地查看随着时间的推移您对代码进行了哪些更改。
In the example above, all three cards represent different versions of the same file. We can select which version of the file we want to use at any point of time. So I can jump to and fro to any version of the file in the git time continuum.
在上面的示例中,所有三张卡都代表同一文件的不同版本。 我们可以随时选择要使用的文件版本。 因此,我可以在git time连续区中来回跳转到该文件的任何版本。
Git also helps you synchronise code between multiple people. So imagine you and your friend are collaborating on a project. You both are working on the same project files. Now Git takes those changes you and your friend made independently and merges them to a single “Master” repository. So by using Git you can ensure you both are working on the most recent version of the repository. So you don’t have to worry about mailing your files to each other and working with a ridiculous number of copies of the original file. And collaborating long distance becomes as easy as HTML ?.
Git还可以帮助您在多个人之间同步代码 。 想象一下您和您的朋友正在合作一个项目。 你们两个都在同一个项目文件上。 现在,Git接受您和您的朋友独立进行的更改,并将它们合并到单个“ Master ”存储库中。 因此,通过使用Git,您可以确保两者都在使用最新版本的存储库。 因此,您不必担心将文件彼此邮寄并处理大量原始文件的副本。 而长距离协作变得像HTML吗?
Git工作流程: (Git Workflow:)
Before we start working with Git commands, it is necessary that you understand what it represents.
在开始使用Git命令之前,有必要了解它的含义。
什么是存储库? (What is a Repository ?)
A repository a.k.a. repo is nothing but a collection of source code.
存储库又名repo就是源代码的集合。
Git工作流程中包含四个基本元素。 (There are four fundamental elements in the Git Workflow.)
Working Directory, Staging Area, Local Repository and Remote Repository.
工作目录 , 暂存区 , 本地库和远程库 。
If you consider a file in your Working Directory, it can be in three possible states.
如果您在工作目录中考虑文件,则该文件可能处于三种状态。
-
It can be staged. Which means the files with the updated changes are marked to be committed to the local repository but not yet committed.
可以上演。 这意味着具有更新更改的文件被标记为已提交到本地存储库,但尚未提交。
-
It can be modified. Which means the files with the updated changes are not yet stored in the local repository.
可以修改 。 这意味着具有更新的更改的文件尚未存储在本地存储库中。
-
It can be committed. Which means that the changes you made to your file are safely stored in the local repository.
可以承诺 。 这意味着您对文件所做的更改已安全地存储在本地存储库中。
-
git addis a command used to add a file that is in the working directory to the staging area.git add是用于将工作目录中的文件添加到暂存区的命令。 -
git commitis a command used to add all files that are staged to the local repository.git commit是用于将所有暂存的文件添加到本地存储库的命令。 -
git pushis a command used to add all committed files in the local repository to the remote repository. So in the remote repository, all files and changes will be visible to anyone with access to the remote repository.git push是用于将本地存储库中的所有已提交文件添加到远程存储库的命令。 因此,在远程存储库中,所有有权访问远程存储库的人都可以看到所有文件和更改。 -
git fetchis a command used to get files from the remote repository to the local repository but not into the working directory.git fetch是用于将文件从远程存储库获取到本地存储库而不是进入工作目录的命令。 -
git mergeis a command used to get the files from the local repository into the working directory.git merge是用于将文件从本地存储库获取到工作目录的命令。 -
git pullis command used to get files from the remote repository directly into the working directory. It is equivalent to agit fetchand agit merge.git pull是用于将文件从远程存储库直接获取到工作目录中的命令。 它等效于git fetch和git merge。
Now that we know what Git is and it’s basic terminologies, let’s see how we can place a file under git. We’re going to do it the right way and the difficult way. Without any GUI applications.
现在我们知道了什么是Git及其基本术语,让我们看看如何将文件放置在git下 。 我们将以正确的方式和困难的方式来做。 没有任何GUI应用程序。
I’m assuming you already have a file the you want to place under version control. If not create a sample folder named ‘MuskCult’ and place some sample code files in it.
我假设您已经有一个要放置在版本控制下的文件。 如果没有,请创建一个名为“ MuskCult”的示例文件夹,并将一些示例代码文件放入其中。
步骤0:注册GitHub帐户。 咄。 (Step 0: Make a GitHub Account. Duh.)
If you don't already have one, you can make one here.
如果您还没有,可以在这里制作一个。
第1步:确保计算机上已安装Git。 (Step 1: Make sure you have Git installed on you machine.)
If you are on a Mac, fire up the terminal and enter the following command:
如果您使用的是Mac ,请启动终端并输入以下命令:
$ git --version
This will prompt open an installer if you don’t already have git. So set it up using the installer. If you have git already, it’ll just show you which version of git you have installed.
如果您还没有git,这将提示您打开安装程序。 因此,请使用安装程序进行设置。 如果您已经有了git,它将仅显示已安装的git版本。
If you are running Linux(deb), enter the following in the terminal:
如果您正在运行Linux (deb),请在终端中输入以下内容:
$ sudo apt install git-all
If you are on Windows:
如果您使用Windows :
$ get a mac
Just kidding… Relax… The amount of people I triggered… Phew…Go to this link or this link for more info on how to get it.
只是在开玩笑...放松...我触发的人数... ew ...转到此链接或此链接以获取有关如何获得此信息的更多信息。
步骤2:告诉Git您是谁。 (Step 2: Tell Git who you are.)
Introduce yourself. Slide in. Seriously, mention your Git username and email address, since every Git commit will use this information to identify you as the author.
自我介绍。 滑入。请认真提及您的Git用户名和电子邮件地址,因为每次Git提交都将使用此信息将您标识为作者。
$ git config --global user.name "YOUR_USERNAME"
$ git config --global user.email "im_satoshi@musk.com"
$ git config --global --list # To check the info you just provided
第3步:生成/检查计算机中现有的SSH密钥。 (可选的) (Step 3: Generate/check your machine for existing SSH keys. (Optional))
Why you ask? Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each visit.
你为什么问? 使用SSH协议 ,您可以连接并进行身份验证以发布服务器和服务 。 使用SSH密钥,您可以连接到GitHub,而无需在每次访问时都提供用户名或密码。
Follow this link to learn more about SSH.Go here to check if you have an existing SSH key.Go here to generate a SSH Key.Go here to add the SSH key to your GitHub account.And finally go here to test its connection.
点击此链接以了解有关SSH的更多信息。 检查您是否已有SSH密钥。转到此处以生成SSH密钥。转到此处 将SSH密钥添加到您的GitHub帐户中。最后去这里测试其连接。
If you did setup SSH, every git command that has a link you replace it by:
如果您确实设置了SSH,则每个具有链接的git命令都将其替换为:
Instead of : https://github.com/username/reponame
You use : git@github.com/username/reponame.git
Note : You can use both ways alternatively
I’ll be using SSH protocol in this tutorial.
在本教程中,我将使用SSH协议。
步骤4:让我们Git (Step 4: Let’s Git)
Create a new repository on GitHub. Follow this link.Now, locate to the folder you want to place under git in your terminal.
在GitHub上创建一个新的存储库。 点击此链接 。现在,找到要放置在终端中git下的文件夹。
$ cd Desktop/MuskCult
初始化Git: (Initialize Git:)
And to place it under git, enter:
并将其放在git下,输入:
$ touch README.md # To create a README file for the repository$ git init # Initiates an empty git repository
Now go edit the README.md file to provide information about the repository.
现在,编辑README.md文件以提供有关存储库的信息。
将文件添加到暂存区进行提交: (Add files to the Staging Area for commit:)
Now to add the files to the git repository for commit:
现在将文件添加到git存储库中以进行提交:
$ git add . # Adds all the files in the local repository and stages them for commit
OR if you want to add a specific file
$ git add README.md # To add a specific file
在我们提交之前,让我们看看暂存了哪些文件: (Before we commit let’s see what files are staged:)
$ git status # Lists all new or modified files to be committed
提交对Git Repo所做的更改: (Commit Changes you made to your Git Repo:)
Now to commit files you added to your git repo:
现在提交您添加到git repo的文件:
$ git commit -m "First commit"# The message in the " " is given so that the other users can read the message and see what changes you made
取消提交刚刚对Git Repo所做的更改: (Uncommit Changes you just made to your Git Repo:)
Now suppose you just made some error in your code or placed an unwanted file inside the repository, you can unstage the files you just added using:
现在,假设您刚刚在代码中犯了一些错误,或者在存储库中放置了不需要的文件,您可以使用以下方法取消暂存刚添加的文件:
$ git reset HEAD~1# Remove the most recent commit# Commit again!
添加远程原点并推送: (Add a remote origin and Push:)
Now each time you make changes in your files and save it, it won’t be automatically updated on GitHub. All the changes we made in the file are updated in the local repository. Now to update the changes to the master:
现在,每次您对文件进行更改并保存时,它都不会在GitHub上自动更新。 我们在文件中所做的所有更改都会在本地存储库中更新。 现在更新对主服务器的更改:
$ git remote add origin remote_repository_URL# sets the new remote
The git remote command lets you create, view, and delete connections to other repositories.
git remote命令使您可以创建,查看和删除与其他存储库的连接。
$ git remote -v# List the remote connections you have to other repositories.
The git remote -v command lists the URLs of the remote connections you have to other repositories.
git remote -v命令列出了您与其他存储库的远程连接的URL。
$ git push -u origin master # pushes changes to origin
Now the git push command pushes the changes in your local repository up to the remote repository you specified as the origin.
现在, git push命令将本地存储库中的更改推送到您指定为原始存储库的远程存储库中。
And now if we go and check our repository page on GitHub it should look something like this:
现在,如果我们去检查GitHub上的存储库页面,它应该看起来像这样:
And that’s it. You’ve just added the files to the repository you just created on GitHub.
就是这样。 您已将文件添加到刚在GitHub上创建的存储库中。
查看您对文件所做的更改: (See the Changes you made to your file:)
Once you start making changes on your files and you save them, the file won’t match the last version that was committed to git. To see the changes you just made:
一旦开始对文件进行更改并将其保存,文件将与提交给git的最新版本不匹配。 要查看您所做的更改:
$ git diff # To show the files changes not yet staged
返回到Git Repo的最后提交版本: (Revert back to the last committed version to the Git Repo:)
Now you can choose to revert back to the last committed version by entering:
现在,您可以通过输入以下内容来选择还原到上一个提交的版本:
$ git checkout .
OR for a specific file
$ git checkout -- <filename>
查看提交历史记录: (View Commit History:)
You can use the git log command to see the history of commit you made to your files:
您可以使用git log命令查看对文件的提交历史记录:
$ git log
Each time you make changes that you want to be reflected on GitHub, the following are the most common flow of commands:
每次您希望在GitHub上进行更改时,以下是最常见的命令流:
$ git add .$ git status # Lists all new or modified files to be committed$ git commit -m "Second commit"$ git push -u origin master
Now if we go and see our repo, we can identify whether the commit was successful by looking at the commit message for each file.
现在,如果我们去看回购,我们可以通过查看每个文件的提交消息来确定提交是否成功。
步骤5:一切都很好……但是,我如何下载并在GitHub上的其他存储库上工作? (Step 5 : That’s all well and good… But How do I download and work on other repositories on GitHub?)
克隆Git仓库: (Cloning a Git Repo:)
Locate to the directory you want to clone the repo. Copy the link of the repository you want and enter the following:
找到要克隆存储库的目录。 复制所需存储库的链接,然后输入以下内容:
$ git clone remote_repository_URL
Feel free to go ahead and clone the repo I created above using: https://github.com/Gothamv/MuskCult
随意继续,并使用以下命令克隆我上面创建的存储库: https : //github.com/Gothamv/MuskCult
将更改推送到Git存储库: (Pushing Changes to the Git Repo:)
Now you can work on the files you want and commit to changes locally. If you want to push changes to that repository you either have to be added as a collaborator for the repository or you have create something known as pull request. Go and check out how to do one here and give me a pull request with your code file.
现在,您可以处理所需的文件,并在本地提交更改。 如果要将更改推送到该存储库,则必须将其添加为该存储库的协作者 ,或者您已经创建了称为“请求”的东西。 去这里检查如何做,并给我一个拉取请求与您的代码文件。
协作: (Collaborating:)
So imagine you and your friend are collaborating on a project. You both are working on the same project files. Each time you make some changes and push it into the master repo, your friend has to pull the changes that you pushed into the git repo. Meaning to make sure you’re working on the latest version of the git repo each time you start working, a git pull command is the way to go.
想象一下您和您的朋友正在合作一个项目。 你们两个都在同一个项目文件上。 每次进行一些更改并将其推送到主存储库中时,您的朋友就必须将您推送到git存储库中的更改进行拉取。 为了确保您每次开始工作时都在使用最新版本的git repo,可以使用git pull命令。
Now below is an example of a project my friend and I are collaborating on:
现在,下面是我和我的朋友正在合作的项目的示例:
So to make sure those changes are reflected on my local copy of the repo:
因此,请确保这些更改反映在我的仓库的本地副本中:
$ git pull origin master
这是另外两个有用的git命令: (Here’s two more useful git commands:)
$ git fetch AND$ git merge
In the simplest terms, git fetch followed by a git merge equals a git pull. But then why do these exist?
用最简单的术语来说, git fetch加上git merge等于git pull 。 但是为什么这些存在呢?
When you use git pull, Git tries to automatically do your work for you. It is context sensitive, so Git will merge any pulled commits into the branch you are currently working in. git pull automatically merges the commits without letting you review them first.
当您使用git pull ,Git会尝试自动为您完成工作。 它是上下文相关的 ,因此Git会将所有提交的提交合并到您当前正在使用的分支中git pull 自动合并提交,而无需您先对其进行审查 。
When you git fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files. To integrate the commits into your master branch, you use git merge.
当您git fetch ,Git从目标分支中收集当前分支中不存在的所有提交,并将它们存储在本地存储库中 。 但是, 它不会将它们与当前分支合并 。 如果您需要使存储库保持最新状态,但是正在进行某些可能会在更新文件时中断的工作,则此功能特别有用。 要将提交集成到您的master分支中,请使用git merge 。
还有一件事: (One More Thing:)
.gitignore
So what is it?
那是什么
.gitignore tells git which files (or patterns) it should ignore. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc.
.gitignore告诉git它应该忽略哪些文件(或模式)。 通常用于避免从工作目录中提交对其他协作者无用的临时文件,例如编译产品,IDE创建的临时文件等。
So in the above example, files like __pycache__, .DS_Store are used by the system to store information for faster access. This is not useful for other collaborators. So we can tell git to ignore them by adding a .gitignore file.
因此,在上面的示例中,系统使用__pycache __ 、. DS_Store之类的文件来存储信息以加快访问速度。 这对其他协作者没有用。 因此,我们可以通过添加.gitignore文件来告诉git忽略它们。
Use the touch command to create the .gitignore file:
使用touch命令创建.gitignore文件:
$ touch .gitignore
And you can add the following patterns to tell git to ignore such files.
您可以添加以下模式来告诉git忽略此类文件。
/*.cmake/*.DS_Store/.user/buildetc. depending upon the files you want git to untrack
这几乎就是基本知识。 请继续关注第2部分,该部分将重点介绍分支,合并,隐藏,重新设置等。 (And that’s pretty much it for the basics. Stay tuned for Part 2 which will focus on Branch, Merge, Stash, Rebase etc.)
如果您喜欢这篇文章,请不要忘记粉碎Clap按钮并确保您跟随我进入第2部分。 (If you enjoyed the article, don’t forget to smash that Clap button and make sure you follow me for Part 2.)
和平Out️ (Peace Out ✌️)
References :
参考文献:
Adding an existing project to GitHub using the command line - User DocumentationPutting your existing work on GitHub can let you share and collaborate in lots of great ways. If you are migrating your…help.github.comHow to undo (almost) anything with GitOne of the most useful features of any version control system is the ability to "undo" your mistakes. In Git, "undo"…blog.github.comGit on the commandline - Don't be afraid to commit 0.3 documentationThere are other ways of installing Git; you can even get a graphical Git application, that will include the commandline…dont-be-afraid-to-commit.readthedocs.ioStart using Git on the command line | GitLabDocumentation for GitLab Community Edition, GitLab Enterprise Edition, Omnibus GitLab, and GitLab Runner.docs.gitlab.comWhat is the difference between 'git pull' and 'git fetch'?Moderator Note: Given that this question has already had sixty-seven answers posted to it (some of them deleted)…stackoverflow.com
使用命令行-用户文档 将 现有项目添加到GitHub- 将现有工作放在GitHub上可以使您以许多很棒的方式共享和协作。 如果您要迁移… help.github.com 如何使用Git撤销(几乎)任何操作 任何版本控制系统中最有用的功能之一就是能够“撤销”您的错误。 在Git中,“撤消”… blog.github.com 命令行上的 Git- 不要害怕提交0.3文档 。 您甚至可以获取图形化的Git应用程序,该应用程序将包含命令行……不要害怕提交。readthedocs.io 在命令行上开始使用Git | 适用于GitLab社区版,GitLab企业版,Omnibus GitLab和GitLab Runner的 GitLab 文档。 docs.gitlab.com'git pull'和'git fetch'有什么区别? 主持人注意:鉴于此问题已经发布了67个答案(其中一些已删除)… stackoverflow.com
翻译自: https://www.freecodecamp.org/news/learn-the-basics-of-git-in-under-10-minutes-da548267cc91/
git基础知识
更多推荐
所有评论(0)