Git is a Version Control System (VCS). A version control system records and saves changes as you modify files, allowing you to restore a previous version of your work at any time.
GIT Repositories:
A git repository is a centralized folder for storing code. There are two types of git repositories.
Local Repository: The local repository is where the git permanently stores the committed changes.
Remote Repository: The remote repository is a centralized location usually hosted on a server like github. It is essential to collaborate with team to push and pull code and have the project saved in a host server.
Three Stages of GIT:
In the git workflow, files can be in one of the three stages,
Untracked/Modified: It is the current working files, the file exists but they are not part of the version control yet.
Staged: It is the stage where the files are added to the version control but not yet committed.
Committed: Once staging of all files are done, the files are committed, and it is the final stage with all the details like time of commit, author committed etc.
Basic commands to know:
The git init command initializes a new git repository in the current directory.
The git add . command is used to add all files in the project to the staging area.
The git add <filename> will add the file mentioned to the staging area.
The git status command shows the status of the current working directory status.
The git commit command is to commit the staged files with the commit descriptive message.
The git push command is to send the committed changes to a remote repository. Origin is the default name of the remote repository and master/main is the branch the code is pushed to. To share changes with others, you have to push them to a remote repository using the git push command. This will update the remote repository and synchronize it with your local repository.
The git log command is used to show the commit logs.
The git pull command is to re-synchronize the local repository with the updated remote repository.
The git branch command is to see the available branches.
The git branch <branchname> command is to create a new branch with the specified branch name.
The git checkout <branchname> command is to switch to specified branch.
GIT Workflow - Steps to use git in a team:
1. Get the Repository URL
2. Clone the repository/project in your local
3. From the master branch, cut/create a new branch -- git branch <branchname>
4. Switch to the created branch -- git checkout <branchname>
5. The branch should be reflected in your eclipse. Add scripts, functions, files and make some changes in your created branch
6. Add the files using git add <file>
7. Commit the changes using git commit -m "commit reason"
8. Push the branch to the remote using git push origin <branchname>
9. Raise a PULL Request from GIT hub (remote)
10. The reviewer will check the pull request and approve or reject the request.
11. The reviewer does the code review, adherence to code standards etc and gives comments.
12. Then the collaborator works on the reviewer comments and push the code once again
13. The reviewer approves it and merge it to main branch
14. If any merge conflicts, resolve it carefully by properly communicating with the team
15. New changes should be reflected in main branch and delete the created subbranch if not needed anymore
16. Whenever working, regularly take the latest pull from main (remote) to local (master) using git pull origin main
Let us see a practical example where a team of 2 members are working on ShoppingCart Application. One is team leader(TL) and one is team member(TM).
The team leader creates a repository “ShopMartApp” in github. Say the TL has developed scripts for login and logout and has files Login.txt and Logout.txt
The team leader collaborates with the team member by adding the team member github account in the ‘Add Collaborator’
Team member receives notification in github and email from the github account. TM accepts the invitation.
The TM is working on the ‘Purchase’ module of the Shopping Cart Application. So TM starts with cloning the repository. The cloned repository is currently in main branch.
TM creates a branch with branch name “purchase’” and checkouts to “purchase” and works in the “purchase” branch.
The team member adds and works on his module Purchase.txt and makes modifications on existing Logout.txt
TM uses git add command to add the two files and commits it.
The team member then pushes it to remote “purchase” branch.
Now, in the github a branch with name ‘purchase’ is created with added files.
TM clicks on the ‘Compare and pull request’ button in the Github. TM makes sure it’s ‘purchase’ branch that is selected.
The pull request will be analyzed by the TL and decided if it is ok to merge with the main branch. It will be available in the Pull requests in Github.
The TL verifies the code and decides some changes needs to be done and provides review comments.
The TL in review mentions to make changes in Request Changes, So, TL asks the TM to change the code accordingly and submits it for review.
The TM receives notification to make request changes.
So TM makes changes as advised and adds those files, commits it and pushes it to remote ‘purchase’ branch.
The TL is now satisfied with the changes made by TM and approves the Merge pull request.
If the project is completely done, and if the team is sure that is project is working successfully after merge, the branch can be deleted.
Meanwhile, it should be always remembered that the TL may make changes in the main branch anytime. So, TM should periodically do a git pull to make sure that everything is up to date.
A graphical representation of the whole process up to this point can be viewed by clicking on the Insights tab and then on the Network option in github. You will be able to see how the branching and merging were performed.
Conclusion:
Understanding Git’s basic commands and git workflow can significantly streamline your project development. In this blog, I have tried to explain the basic git workflow for a sample project. With these basic Git commands in your toolkit, you're well-equipped to collaborate seamlessly with your QA team, track changes, and ensure software quality.
Comments