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 fres
Let me explain, in simple and minimum words.
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.
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.
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..
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 )
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..
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..
Normally master branch
is default branch and always need to update it with all codes that are stable. Working Flow looks like -
One/more developer create and switch to a new branch from master. Then do changes and test the codes. After completing a task (& also the codes are stable), create a pull request
and merge
the codes with master branch.
Now, master has some new changes and all other developers take (pull) the latest codes of master into their different branches they are working on.
When need to release a version of the product, give a tag (e.g. v1.0.0
) on a commit in master branch. Then create a new branch (e.g. release-v1.0.0
) from that tag and release the product.