Why working on Git repositories?
Working on a project means taking the risk - daily - to loose the work already done, especially when many members are working on the same files. But also, working on multiple versions when a client request an additionnal functionnality, or a functionnality change, can quickly become tricky.
Working on Git repositories allows to:
- Keep history of the changes
- Keep clean environment structures: "production" (master branch), "development" (some other branch), "v2" (some other branch)
- Allow teams to work together without affecting the work of others
- And so much more!
Prequisities
In order to setup a project on a Git repository, here is what you need to do before-hand:
- Need to install Git Client on your system: https://git-scm.com/download/win, configured to be used from the Command line and 3rd party software, with OpenSSH and the OpenSSL library (all default recommended options)
- Need to install Visual Studio Code => https://code.visualstudio.com/
- Even if by default, Visual Studio has a Source Control tool, need to install the following extension to have the full functionnalities of a Git repository:
- Git History
- Make sure your account is created on Gitlab.com, with your
@gopadmaemail => https://about.gitlab.com/
Connect a Gitlab Project to your Visual Studio Code
Once all is installed, you get to connect your computer with your Gitlab account:
- The project on Gitlab can be configured to "Private" or "Public", it doesn't matter
- The Gitlab user has to have permissions on the project (in "Members", it has to be set as a "Developer")
- Now, even though the Gitlab user has the right permissions, the computer which is going to access the repository has to be recognized. In order to do that, it is necessary to upload the system's SSH private key into your Gitlab's account details:
- Step 1: create your private key on your local machine
- Case 1: you don't have a key generated yet:
- Open Git CMD
-
ssh-keygen -t ed25519: generate a new SSH key with the preferred algorithm in Gitlab ED25519 - The terminal will ask for a custom name: it is not compulsory. It is required if many keys have to be created (when working on different Git installations/projects)
- And a passphrase: this is not compulsory. It hads security to the key. It has to be set when an untrusted party has to access the repository.
- Case 2: you already have a key generated
- Check in your local folder:
C:\Users\your-user/.ssh/id_ed25519.pub(the name can vary:id_rsa.pubfor example)
- Check in your local folder:
- Case 1: you don't have a key generated yet:
- Step 2: add your key to the project in GItlab
- On http://gitlab.com, go into your account Settings: select your avatar in the upper right corner, and click Settings
- Click SSH Keys.
- Paste the public key that you copied into the Key text box.
- Make sure your key includes a descriptive name in the Title text box, such as Work Laptop or Home Workstation.
- Step 1: create your private key on your local machine
The system is ready to get Synchronised with the Repository
Now you are ready to start working on the project!
- You can now create your project folder, where you need it to be. For example in
C:\xampp\htdocs. - In Visual Studio Code, go to the "Explorer" tab, and open the project folder which is on your system
- The go in the menu "Terminal" > "New Terminal"
-
git init: initialize the repository -
git clone git at gitlab.com:user/project.git: get synchronized to the project's repository -
git remote add origin git at gitlab.com:user/project.git: to make sure the current project root folder is the origin and destination for all the bellow commands.
-
You are all set!
Daily work on the project
Create an alternative branch
When working on a project, it is important to understand the concept of branches.
A branch is a node. It is used to separate version and environments:
- the
masterbranch is the default one. It is the Live version reference. - any other branch has the meaning you want it to have. Creating a
developmentbranch would make sure the team work on a version which is not yet finished!
Please make you work on the right branch! In Visual Studio Code, you can switch from a branch to another by clicking on the bottom-left branch name (by default master).
Pull requests
A Pull request in actualy a "get" of the files from the distant repository, and the branch configured in the request.
-
git pull: get the files from the currentbranch
Staging modifications
You don't want to send each and every file independantly. You want to work in your local version, and then push the working files in bulk. Here is how to do that:
-
git add .: to add the files to the next commit
Commits
A commit is actually a set of modified/finished files, sent to the server. It has to be commented, so anyone can see in the team, what was changed in that particular version of each file.
-
git commit -m "Comment about the change": send the staging files forcommit
Push requests
One the commit is configured, we send/push it to the reference repository and branch.
-
git push: push the current commit to Gitlab
Compare versions
One of the main advantages of Git is to have different persons from a same team working on the same files.
Let's imagine the following situation:
- Developer A works on File X, makes some modifications in it, and Push it to the Repository
- Developer B is also working on that file at the same time. If he Pushes his version, Git will save 2 versions of the file, and give the possibility to compare the changes between each, so that each developer can decide how to best merge the 2 files into 1, having the work of both developers!
This is possible on Visual Studio Code using the extension Git History, with a right click on the file in the list.
Roll-back
If a customer asks to come back to a previous version of a file, that he finally found better; or if a new version is actually not working anymore; then Git gives the possibility to go back in time and to a better version!
This is possible on Visual Studio Code using the extension Git History, with a right click on the file in the list.
Versions, delivery, and Merge requests
When a development version gets finished and is validated by the client, this version can become the new live version.
In that case, the working branch will be merged to the master branch with a merge request.
This has to be done by the Project Manager, directly on Gitlab.
Useful commands
-
git status: to know the status of the files, pending commits, etc
Most Commented