git 代码推送流程
I'm going to explain Git the way I wish someone had explained to me back when I was first learning.
我将以我希望有人在我第一次学习时向我解释的方式来解释Git。
I'll show how you can get started with just a few commands, and the concepts at work behind those concepts.
我将展示您如何仅需几个命令就可以开始使用,以及这些概念背后的概念。
基本概念 (Basic concepts)
您的本地代码 (Your local code)
This is the work that you do on your computer. Any edits, formats, features or development work that you have in your computer is your local code.
这是您在计算机上所做的工作。 您在计算机中进行的任何编辑,格式,功能或开发工作都是您的本地代码。
上演网站 (Staged site)
Once you are happy with the changes or amount of work done, you can mark it as ready for stage. It means that you are declaring that these lines of code are ready to be committed.
一旦对更改或完成的工作感到满意,就可以将其标记为准备就绪。 这意味着您要声明这些代码行已准备好提交。
服务器 (The server)
Once you are feel ready with the files you have staged, you can send them out to the server which stores all your code so that other people can use it too. Now your files can be viewed by other people and be worked on.
一旦准备好暂存的文件,就可以将它们发送到存储所有代码的服务器,以便其他人也可以使用它。 现在,您的文件可以被其他人查看并进行处理。
最终目标 (The end goal)
The idea behind git is that your code on the remote server should be synced with the code on your machine. When you work with other folks, their code should be synced with your code. So if your co-workers pushed new code, you should be able to easily update your code to reflect their changes.
git的想法是,应该将远程服务器上的代码与计算机上的代码同步。 当您与其他人一起工作时,他们的代码应与您的代码同步。 因此,如果您的同事推送了新代码,则您应该能够轻松地更新代码以反映他们的更改。
The end of goal is that everything on the server should be the same as everything on your local machine. You should send files to the server as soon as possible so that when others view your code, they have the most updated version.
目标的最后是服务器上的所有内容都应与本地计算机上的所有内容相同。 您应该尽快将文件发送到服务器,以便其他人查看您的代码时,它们具有最新版本。
工作流程 (Workflow)
Let’s say you have a folder on your computer where you have your code. You want to put it in GitHub so you can version control it.
假设您在计算机上有一个存放代码的文件夹。 您希望将其放在GitHub中,以便可以对其进行版本控制。
Here’s how you can do it.
这是您的操作方法。
1.获取存储库。 (1. Getting a repository.)
There’s two ways of getting a repository — either you can create one of your own, or you can work off of someone’s repository and contribute to their repository. Which one you choose depends on the project you are working on. If you are working on a project by yourself, you are more likely to create a repository from scratch and start writing code. When you work on open source code or working in a team, you will most likely get a repository that already exists. Let’s talk about how to do both —
获取存储库有两种方法-您可以创建自己的存储库,也可以在某人的存储库中工作并为他们的存储库做出贡献。 选择哪一个取决于您正在处理的项目。 如果您是一个人从事项目,则更有可能从头开始创建存储库并开始编写代码。 当您使用开源代码或团队合作时,很可能会获得一个已经存在的存储库。 让我们来谈谈两者的做法
创建一个仓库 (Create a Repository)
First you will need to create a GitHub account if you don’t already have one. Click on Repositories tab and click “New”
首先,如果您还没有一个GitHub帐户,则需要创建一个。 单击存储库选项卡,然后单击“新建”
Follow instructions to create your new repository. Let’s say we name this repository test-repo
. Once you have created this repo, GitHub gives instruction on how to set it up on your local computer.
按照说明创建新的存储库。 假设我们将此存储库命名为test-repo
。 创建此存储库后,GitHub会提供有关如何在本地计算机上进行设置的说明。
Use the instructions to create a new repository on the command line -
使用说明在命令行上创建新的存储库-
克隆存储库 (Cloning a repository)
git clone <repository>
git clone <repository>
Let’s say you want to get the code I am working on so you can work off of it on your computer. You will need to find my github account and clone a repository that you are interested in. Today we are going to work on this repository —
假设您要获取我正在使用的代码,以便可以在计算机上使用它。 您将需要找到我的github帐户并克隆一个您感兴趣的存储库。今天,我们将在该存储库上进行工作-
git clone git@github.com:shrutikapoor08/devjoke.git
2.创建一个分支 (
2. Create a branch)
Creating a branch helps to keep work in progress separate from a working piece of code. It also helps you modularize your code and keep features separate while you are still working on them. Let’s make a new branch joke-branch
by using
创建分支有助于使工作与代码段分开进行。 它还可以帮助您模块化代码,并在使用功能时将其分开。 让我们通过使用一个新的分支joke-branch
git checkout -b joke-branch
git checkout -b joke-branch
You can push this branch to the server (origin
)by —
您可以通过以下方式将此分支推送到服务器( origin
):
git push origin joke-branch
git push origin joke-branch
Now let’s assume that you and I are teammates. We are working on a project together. Let’s walk through the workflow.
现在,假设您和我是队友。 我们正在共同致力于一个项目。 让我们来看一下工作流程。
3.修改代码 (3. Modifying code)
Once you understand what the code does, you are ready to make edits. If you are working on somebody else’s repository, it is a good idea to make a fork
of their repository. “Forking” is making a copy of a repository. It is like running a repository through a copy machine and getting your own copy. You can do whatever you want to your copy, make changes, edit files, delete files or add new files. If you want, you can send your updates to the original repository and ask for your code to get merged into the original repository.
一旦了解了代码的功能,就可以进行编辑了。 如果您正在使用其他人的存储库,则最好将他们的存储库作为fork
。 “分叉”正在复制存储库。 就像通过复制机运行存储库并获取自己的副本一样。 您可以对副本执行任何操作,进行更改,编辑文件,删除文件或添加新文件。 如果需要,可以将更新发送到原始存储库,并要求您的代码合并到原始存储库中。
We are going to be modifying this code by creating a fork of this repository. To fork a repository, click on the Fork button at the top of the repository.
我们将通过创建此存储库的派生来修改此代码。 要派生存储库,请单击存储库顶部的“分叉”按钮。
Now if you go to your Github profile, you can see your newly created fork repository in your profile. Woot Woot!
现在,如果您转到Github个人资料,则可以在个人资料中看到新创建的fork存储库。 呜呜!
Now we are going to make some changes. Open the README.md
file. To this file, we are going to add a DevJoke! Think of a clever programming joke and add it to the top of this file. Here is a #DevJoke for you —
现在,我们将进行一些更改。 打开README.md
文件。 在这个文件中,我们将添加一个DevJoke! 考虑一个聪明的编程笑话,并将其添加到此文件的顶部。 这是给您的#DevJoke-
Why do Java programmers wear glasses?
为什么Java程序员戴眼镜?
.
。
.
。
.
。
.
。
4.提交代码 (4. Committing code)
Now, let’s save and commit this file. If you are committing a newly created file, you will first have to add it to your stage. You can do so by —
现在,让我们保存并提交该文件。 如果要提交新创建的文件,则首先必须将其添加到舞台。 您可以通过-
git add <filename>
git add <filename>
Once your file is staged, you can commit. Be sure to append your commit with a message that will help you remember in the future what this commit is about.
暂存文件后,即可提交。 确保在提交后附加一条消息,以帮助您将来记住该提交的含义。
git commit -m "Adding DevJoke about C#
git commit -m "Adding DevJoke about C#
5.推送代码 (5. Pushing code)
Remember, when you are working on a repository that other people are also working on, it is possible that while you were working on your local version, some people may have pushed code. So now the server is “one step ahead of you”. You now need to sync your computer to the server to make sure you have the most updated code. To do so, you can pull from the server —
请记住,当您在其他人也正在使用的存储库上工作时,当您在本地版本上工作时,可能会有一些人推送了代码。 因此,现在服务器“比您领先一步”。 现在,您需要将计算机同步到服务器,以确保您具有最新的代码。 为此,您可以从服务器中拉出-
git pull origin <branch-name>
git pull origin <branch-name>
If you have never made a branch, don’t worry. Git gives you master
branch by default. You can also make a new branch by —
如果您从未建立过分支,请不要担心。 Git默认为您提供master
分支。 您还可以通过以下方式建立新分支:
git checkout -b <branch-name>
Now that you have committed your code and your local machine is updated from server, you are ready to push your changes for the world to see. To push to branch master
, you can do so by — git push origin master
现在,您已经提交了代码,并且本地服务器已从服务器更新,您可以将所做的更改推送给全世界。 要推送到分支master
,可以通过以下方式进行操作:git push origin master
YAYY!!! You made your first push! Give yourself a pat on the back. That was the hard part. Now you are ready to commit your ass off.
呀! 您是第一个推动者! 拍拍自己的背部。 那是困难的部分。 现在,您可以准备放弃了。
6.发出拉取请求 (6. Making a Pull Request)
Now, send me that wonderful #DevJoke of yours by creating a pull request. Pull request is a feature by which you — the developer, requests me — the code owner to merge your code into the repository. Creating a pull request is easy. Go to github.com and open the repository you have been working on.
现在,通过创建拉取请求,将您的精彩的#DevJoke发送给我。 拉请求是一项功能,您(开发人员可以向我索取)代码所有者将您的代码合并到存储库中。 创建拉取请求很容易。 转到github.com并打开您一直在使用的存储库。
You will see a Pull request button like this :
您将看到如下所示的“拉取”请求按钮:
Click the button and follow the instructions to create a pull request. Make sure your changes are correctly reflected on the “Comparing changes” screen.
单击按钮,然后按照说明创建拉取请求。 确保您的更改正确显示在“比较更改”屏幕上。
Click on “Create pull request” and that’s it! You are done! You successfully made your first Pull request!
单击“创建请求请求”,仅此而已! 大功告成! 您已成功发出第一个Pull请求!
👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏
👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏
Now, go ahead and send me that DevJoke!
现在,继续发送DevJoke给我!
Did you learn something new? Have comments? Know a DevJoke? Tweet me @shrutikapoor08
你学到新东西了吗? 有意见吗? 知道开发笑话吗? 鸣叫我@ shrutikapoor08
翻译自: https://www.freecodecamp.org/news/git-101-git-workflow-to-get-you-started-pushing-code/
git 代码推送流程