如何正确工作机智Git? [英] How to work wit Git correctly?

查看:99
本文介绍了如何正确工作机智Git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过Git进行尝试,但我很确定我没有正确使用它。

我想知道如何使用Git来变得更专业。

我想从没有安装Git的情况下开始。


当然,我已经阅读了手册并Google搜索了很多,但我不确定如何正确使用它。

现在我们有一个专门用于网站的unix服务器。

我在Windows上使用PhpStorm进行web开发,并且我有一个大学, 301-重定向.htaccess中的变化

$ b $也许将来我们会与另一个程序员一起扩展。

我希望能够追踪他所做的更改,或者是Git矫枉过正只需添加一些重定向到.htaccess?

在这种情况下使用git的最佳方式是什么?

主人应该在哪里等。

I '对于Git来说很新,所以请具体说明一下。

我也听说有可能我必须在我的大学上线之前审查提交的内容,我希望以这种方式工作。

我也需要使用新代码更新master,或者它将全部是单独的分支等。

解决方案

让我以简单和最简单的话来解释。


  1. Git不一定只用于多用户环境,但它对于单个开发人员来说是相同或更有用的独立。开始之前,我们应该首先利用其作为单个开发人员的所有功能,而不是协作方式。

  2. 我们在本地系统上创建了git存储库(使用git init) ,开发人员(无论是主人还是分支,以后都会......),在开发周期中,他/她应该识别非常小但完整的工作单元,即添加按钮并执行一些基本功能,以及它应该上演(git add)并提交(git commit)并加上适当的细节。作为一名开发人员,我曾经每天在本地存储库中进行5到10次提交。 第二阶段的优势是,我们可以随时回溯。如果在几秒内发生任何问题......在提交编号期间创建本地存储库。 3或提交否。 10(git checkout .. commit no。)在调试过程中非常有用,并且具有清晰的图像供进一步规划。即使在任何特定提交(git diff将显示任何2次提交之间的差异)后引发的任何问题都会很容易。所以我们通过git控制我们的文件。


  3. 现在,不用在master中工作,我们可以更喜欢在分支中工作。例如,在每个新功能之前,可以创建一个分支(git branch branch_xyz,git checkout branch_xyz),并且我们可以使某些规则(不是通过git)像(代码审查完成,单元测试,组件测试)未完成,代码将不会合并为主..审阅者,测试人员或感兴趣的项目团队,可以将他们系统中的特定分支拉到他们的活动中。无论是其主分支还是分支,提交逻辑都保持不变,第2点中提到的所有内容都将保持不变。我们想要标识为里程碑的一些提交可以被标记,即(git tag -a build_3.4 -mbuild description)。毕竟验证后,代码可以合并到master(git checkout master,git merge branch_xyz)。

  4. 本身,没有任何服务器。现在在多用户环境中可以选择在我们自己的服务器上存储库,或者我们可以使用github或bitbucket之类的资源库。我使用bitbucket作为我的代码。它不需要只将最终更改推送到主存储库(不要等待)...我们可以继续推动我们的变化..到主存储库(git push -all -u remotename),就像我们在本地做的那样,如果在本地它是分支的一部分,在main中它也将是同一分支的一部分,如果在本地它被合并为主,则只有它将被合并到主服务器上。


  5. 想要工作的其他团队成员......他们可以使用git clone或git pull命令从主存储库获取副本..并发布他们可以遵循第2点中提及的相同步骤。


Git在多用户环境中



Q - 创建主目录(使用git)的最佳位置是它位于专门的服务器上,该服务器上还托管了网站,或者位于单独安装的服务器上正屋?


  • 您不需要将它称为master,因为git server和git node,它们都将拥有主(主代码)和分支(WIP代码),它在下面进行了解释


  • 主Git服务器不必是专用服务器,也不必位于网站所在的同一台计算机上主办。只要您可以下载
    并准备用于部署的构建,它就可以在任何地方使用。然而,git主服务器/存储库不应该用于开发目的,但不是必需的。




主人我需要将主人克隆到我自己的电脑并开始开发。之后,我需要将更改提交给git-server?




  • 让我们假设您的团队中有3名成员1)Ron - 开发人员2)Gerard-开发人员3)Sytse - 测试人员,您当前的版本是build_4_01,位于主分支中的主要git服务器上,截至今天,您的团队成员都没有在他们的系统中获得代码。


  • 首先,所有这三个人都在他们的本地系统上安装了git,并从主服务器克隆它。现在他们在master分支下的本地git仓库中拥有了build_4_01接下来,Ron被要求开发下一个将被命名为build_4_02的功能

  • li>
  • Ron首先会在其本地git仓库中创建一个分支,即branch_build_4_02,然后在branch_build_4_02下完成代码。

  • 在构建build_4_02的过程中,共有20个小功能,Ron在10天内完成了它。

    在完成每个小功能后,Ron用来执行在每次提交后,或者在一天结束时,Ron会将代码(git push)推送到主服务器(无需担心,主服务器不会混淆现有的代码,Ron所推送的内容是在branch_build_4_02分支内部,而主服务器则使master和branch保持高效分离) 在这10天内,
  • 在他的开发时间里,Ron多次希望Gerard帮忙检查代码或帮助他在技术上是这样的。因此,Gerard需要Ron提供的所有最新代码。每天早上,Gerard都会执行git pull(命令),并且会将他的本地git存储库与Ron进行的所有最新更改同步,Gerard可以在branch_4_02中看到它们。


  • 编码完成后(build_4_02)Sytse(测试人员)被告知,他也喜欢杰拉德在他的本地系统中执行了一个拉取请求,并得到了最新的代码


  • 他们,并在git服务器上推送代码,Sytse将他们拉到本地git上进行重新测试。
  • 在收到测试并收到测试签名后,用于生产部署。代码仍然在分支4_02 现在罗恩指出他的本地git在10天内第一次掌握(git checkout master)合并branch_4_02),他还给出了一个特定的标记,用于最后一次提交,名为build_4_02,因此每个人都可以通过名称进行标识。
  • 具体提交将类似于 3003b9fe441dd6a2e3c1410880c3a86b496fcb27 但可以标记为用户友好名称,即4_01_code_completed,test_completed,4_01_prod

  • 。)同样的变化也会在服务器上反映出来。
  • 在Master分支中,他为特定的提交(Ron完成)执行prod部署并将其命名为build_4_02。

  • 团队中的每个人都执行了git pull请求在他们的本地git仓库中,与prod同步。


  • Syts有时候需要处理生产问题,因此想回到构建401,他只需签出具体的提交,并将在其系统中构建4_01代码。


>

Q - 之后,我需要与git-server上的主服务器合并?

合并将在Sytse收到签名后在本地git上发生,但会在合并后再次被推送到服务器上。



Q - 我如何将更改从git-server推送到网站上?

它与您当前的流程相同,唯一不同的是推送网站更改的人,他的本地PC上会有git,并且在
推送改变网站,他会确保他得到最新的代码,并且有正确的提交(tag build_4_02),与Gerard上面的一样


Q& A

所以要确保git主服务器仅用于接收和发送构建。我可以简单地使用单独的电脑作为git主服务器,只安装了git和centos?

是可以使用单独的服务器,虽然不是强制性的,但是不是调用它的作业来接收和发送构建,而是说它将被集中代码仓库,其中团队可以获得任何版本的代码。



- 当Ron完成git合并branch_4_02时,更改与本地git和git主服务器合并在一起?而对这些合并的感谢git主服务器将始终是最新的?


不,合并只发生在本地系统,从一个分支到另一个分支,或从子分支到主分支,反之亦然。要发送给主服务器,git push命令应该在本地服务器上执行,它给予开发人员更多的控制权,只有当开发人员要求时才会将提交发送到服务器。进一步使用push命令,开发人员也必须提及远程服务器名称。



Q - 提交开发人员提供标签4_02_prod的最佳方式,以及谁将部署变更拉代码,并给它一个标签4_02_build?



理想情况下代码完成并由开发人员(罗恩)标记为4_02_prod,应该像原来一样去Prod更改,在这种情况下,部署者不需要标签。
但是在某些情况下,部署者(Gerard)在Ron更改之上进行更改时,他必须标记好标题,即4_02_deployed。好处是在生产问题期间,一个简单的差异(git diff)将清晰地描述2个提交/标记之间的变化,以缩小问题的范围,进一步提交/标记可以被测试人员拉取以供进一步调查。 p>

Q-当一切都完成后,你应该删除分支还是保留所有内容?



立即在Ron将分支4_02合并到主设备,并给它一个标记4_02_prod,并将其推送到Prod,必须删除branch_4_02。因为相同的内容在标签4_02_prod的主分支中可用
。在这段时间之后保持分支机构不会有任何优势,但是如果有人错误地使用它,那么风险会很大。



要删除它,Ron会能够看到2个分支1)一个在本地系统使用命令git branch 2)另一个在主服务器上使用命令
git branch -r。两者都必须被删除,然后是git push,所以主服务器也是同步的。



你提到我需要删除两个地方的分支。本地和主服务器上的一个。但是,当我在本地删除分支并在主服务器上时,我仍然可以恢复到旧版本?



是的,如果你从本地git和主服务器删除分支,你仍然有所有版本的代码,因为在分支被删除之前,分支本身是合并为主,并被标记为4_02_prod。

这是git的美妙之处,只需使用一个命令 git checkout 开发者可以获得任何版本的代码,只要它被提交。

在当前的场景中,Ron只需要执行命令 git checkout tags / build_4_02_prod ,他的系统中会有旧代码。他可以通过执行命令 git checkout master



Q来获得最新的
代码。首先删除构建,然后从我的主人推(我猜),所有这些都是在部署到网站/实时代码之前完成的?



是分支删除后,我再次推送,所以服务器同步。为了更安全一侧,我做2推1)合并完成后,主2)分支删除完成后。如果分支在某个地方被删除而不是其他地方,它会在随后的推送过程中继续给出错误。


I have used Git for trying out but I'm pretty sure I don't use it correctly.
I would like to know how to use Git to become more professional.
I want to start from fresh so from a situation where no Git is installed.
Of course I have read the manuals and Googled a lot but I'm just not sure how to use it the correct way.

Right now we have a dedicated unix server for the website.
I use PhpStorm for webdevelopment on Windows and I have a college who make small 301-redirect changes in .htaccess.
And maybe in the future we will expand with another programmer.
I want to be able to track the changes made by him also, or is Git overkill for just adding some redirects to .htaccess?

What is the best way to use git in this situation?
Where should the master be, etc.
I'm very new to Git so please be specific with the explanation.
I also heard that it is possible that I have to review the commit from my college before it goes online, I would like to work that way.

Also do I need to update master with the new code or will it all be seperate branches, etc.

解决方案

Let me explain, in simple and minimum words.

  1. Git not necessarily used only for multi user environment, but it is same or more useful standalone for individual developer. When start we should first utilize all its function as a individual developer, than collaboratively manner.

  2. Let’s we created the git repository on local system ( using git init ) , and developer working on ( either master or branch , will take up later.. ), during development cycle he/she should identify very small but complete unit of work which is working, i.e. button is added and its doing some basic functionality, and it should be staged ( git add ) and committed ( git commit ) with proper detailed. I as a developer used to do 5 to 10 commits daily in local repository.

  3. Advantage of stage 2 is, we can go back in time anytime.. and in case of any issue within seconds ..make local repository as it was during commit no. 3 or commit no. 10 ( git checkout .. commit no. ) its extremely useful during debugging, and having clear picture for further planning . Even it will be very easy to catch any issue which raised after any specific commit (git diff will show diff between any 2 commits ). so we have very good control on our files through git..

  4. Now instead of working in master , we can prefer to work in the branch. For example before each new functionally , a branch can be created ( git branch branch_xyz, git checkout branch_xyz ) , and we can make certain rules (not by git) like ( code review completed, unit testing, component testing ) is not completed, code will not be merged to master.. reviewer, tester, or interested project team, can pull the specific branch in their system.. for their piece of activity. Either its master or branch, commit logic remain same and all mentioned in point 2 will remain same. Few commits which we want to identify as milestone can be tagged i.e. ( git tag -a build_3.4 -m "build description ) . After all verification, code can be merged to master ( git checkout master, git merge branch_xyz )

  5. All above mentioned was the minimum functionally can be used on local system itself, without any server. Now in multi user environment can choose to have repository on our own server, or we can use repository like github or bitbucket.. I use bitbucket for my code..Its not necessary to push only final changes to main repository ( don’t wait)... we can keep pushing our changes.. to main repository ( git push –all -u remotename ) as we were doing it locally, if locally it was part of branch, in main also it will be part of same branch, and if locally it was merged to master, than only it will be merged to master at server..

  6. Other team members who want to work.. they can use git clone or git pull command to get copy from main repository.. and post that they can follow same steps mentioned in point 2 onward..

Git in Multi User Environment

Q - Where is the best place to create the master (with git) is it on the dedicated server where the website is also hosted or on a separate installed server in-house?

  • You need not to call it master, because git server and git node, they all will have master ( main code ) and branches ( WIP Code ) , it is explained below

  • Main Git server need not to be a dedicated server, also need not be on same machine where web site is hosted. It can be anywhere as long as you can download and prepare build, which is being used for deployment. However git Main server/repository should not be used for development purpose, but not necessary.

Q - After creating the master I need to clone the master to my own computer and start developing. After that I need to commit the changes to the git-server?

  • Lets assume 3 member in your team 1) Ron - Developer 2) Gerard- Developer 3) Sytse - Tester , your current build is build_4_01 sitting on main git server in master branch, and as of today none of your team member have got code in their system.

  • to start with all these 3 guys have installed git on their local system, and clone it from main server. Now they have build_4_01 in local git repository under master branch

  • next Ron was asked to work on next functionality which will be named as build_4_02

  • Ron first will create a branch in his local git repository i.e. branch_build_4_02, and complete the code, under branch_build_4_02.

  • there were altogether 20 small functionalities under build build_4_02, and Ron completed it in 10 days.

  • after completion of each small functionality, Ron used to execute a commit on local repository

  • after each commit, or at the end of day, Ron will push code (git push ) to main server ( no worries, main server will not mess up with existing code. What Ron have pushed is inside the branch branch_build_4_02 , and main server keeping master and branch separate efficiently )

  • during these 10 days of his development time, many times Ron wanted Gerard's help to review the code, or help him technically

  • hence Gerard need all latest code from Ron. Every morning Gerard will execute git pull ( command ) , and it will sync up his local git repository with all latest changes Ron have done, Gerard can see them inside the branch_4_02

  • After completion of coding ( build_4_02) Sytse (Tester) was informed, and he also like Gerard executed a pull request in his local system , and got the latest code

  • Sytse deployed the code from git branch 4_02 on test server and completed the testing

  • during testing some defects were raised by him, but Ron have fixed them, and pushed code on git server, Sytse pulled them on local git for retest

  • after all was tested and test sign-off was received, a go ahead given for production deployment. Code was still sitting under branch 4_02

  • Ron now pointed his local git to master first time in 10 days ( git checkout master ) and merged branch to master ( git merge branch_4_02 ) , he also given a specific tag to last commit called build_4_02 so every one can identify by name

  • specific commit will look like 3003b9fe441dd6a2e3c1410880c3a86b496fcb27 but can be tagged with user friendly name i.e 4_01_code_completed, test_completed, 4_01_prod

  • Ron executed one more push ( git push .. ) so same changes will reflect on server too..

  • Gerard was responsible for Prod deployment, he executed a git pull on his local system, with latest code in Master branch, he perform prod deployment for specific commit ( Ron had done ) and named it tag build_4_02.

  • everyone in the team, executed a git pull request on their local git repository, to be in sync with prod.

  • Sytse sometime need to work on Production issue, and hence want to go back to build 401, he will just checkout that specific commit and will have build 4_01 code in his system

Q - After that I need to merge with the master on the git-server?

Merge will happen on local git after sign off will be received by Sytse, but will be pushed again to server as Ron did above after merge

Q - And how do I push the changes to the website from the git-server?

It will same as per your current process, only difference is person pushing the changes to website, will have git on his local PC, and before pushing the changes to website, he will ensure he get latest code, and have correct commit ( tag build_4_02) , same as Gerard did above

Q & A

Q - So to be sure the git main server will be just for receiving and sending builds. I could simple use a separate pc as git main server, with only git and centos installed on it?

Yes a separate server can be used though not mandatory, however instead of calling its job to receive and send builds, it will be more appropriate to say, it will be centralized code repository, where team can get any version of code.

Q - When Ron did the git merge branch_4_02 the changes are merged with the local git and the git main server? And thanx to these merge the git main server will be always up-to-date?

No, merge happen only at local system, from one branch to another branch, or from child branch to master branch or vice versa. To send it main server, git push command should be executed on local server, its gives more control to developer that commits will be sent to server only when developer will ask for that. Further with push command, developer will have to mention remote server name as well.

Q - Is the best way of committing that the Developer gives it the tag 4_02_prod and the one who deploys the changes pull the code and gives it a tag 4_02_build?

Ideally code finished and tagged by developer (Ron) as 4_02_prod, should go as it is to Prod without changes, in this case no tag will require from deployer. But in some case deployer ( Gerard ) making changes on top of Ron changes, he must tag it with good title i.e. 4_02_deployed. Advantage is during production issue, a simple diff ( git diff ) , will give a clear picture what was changed between 2 commits/tag to narrow down the issue, and further both commit/tag can be pulled by tester for further investigation.

Q- And when everything is done are there branches you should delete or do you keep everything?

Immediate after Ron merge branch 4_02 to master, and give it a tag 4_02_prod, and push it to Prod, branch_4_02 must be dropped. Because same content will be available in master branch with tag 4_02_prod. Keeping the branch alive after this time, will have no advantage, but big risk if someone used it wrongly, it will be kind of mess.

To delete it, Ron will be able to see 2 branches 1) one at local system using command git branch 2) another at main server using command git branch -r. Both must be deleted and followed by git push, so main server is also in sync.

Q -You mentioned that I need to delete the branch on two places. The one locally and the one on the main server. But when I delete the branch locally and on the main server can I still revert to old builds?

Yes it true, if you delete the branch from local git and from main server, you still have all version of code, because before branch was deleted, that branch itself was merged to master and was tagged as 4_02_prod.

This is the beauty of the git, by using just a single command git checkout developer can get any version of code as long as it was commited.

In current scenario Ron only need to execute a command git checkout tags/build_4_02_prod, he will have old code in his system. He can have latest code by just executing a command git checkout master

Q - And you say first delete the builds and then do a push (from your master I guess), that all is done before deployment to the website/live code?

Yes after branch deletion I push again, so server is in sync. To be at safer side I do 2 push 1) after merge is completed to master 2) after branch deletion is completed. If somehow branch is deleted at one place but not other , it will keep giving error during subsequent push..

这篇关于如何正确工作机智Git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆