Good Git Guide
A simple guide so you can work on a team.
“You can never understand everything. But, you should push yourself to understand the system.” — Ryan Dahl (Creator of Node.js)
As a programmer, we often required to work as a team for a software project. If we work by ourselves, we can easily manage our work flow, but as a team, we need a tool to help our collaboration. One of the best tools out there is Git. What is Git? Git is a version control system that is free and open source distributed, and we can use git to handle our source code from small to very large projects with efficiency. Git can be used to track changes in our source code, so our team developers could work together on non-linear development.
Imagine Git as a tree that have a root and a lot of branches. The branches is where we work as a individual developer, and the root is where our work from many branches are gathered. Each branch on the tree is independent, so the changes we made couldn’t affect another branch. The management of this tree is called git flow. We need to understand git flow so that our collaboration works well.
In this article, I will explain some basic Git commands and git flow introduction that I and my team use.
Git Commands
Before we start, if you haven’t downloaded Git on your computer/laptop, check out this website. If you want to check whether Git is installed or not, open your Command Prompt and type this command
git --version
Initialization
To create a new git directory, use this command
git init
To fetch an online project repository (Github or Gitlab), use this command
# Used to clone the main/master branch
git clone <repository_url># Used to clone the branch
git clone <branch_name> <repository_url>
git clone
is doing git init
in the process, so you don’t have to do git init
anymore.
Connect new git directory to online project repository
To connect a new git directory that created by git init
to your project repository (Github or Gitlab), use this command
git remote add <repository_remote_name> <repository_url>
Repository remote name is used as a identifier. If you use git clone
command, the identifier will be “origin” as default.
Get files from online project repository
To synchronize your local git to a certain online project repository, use this command
# Used to get all the files in a certain branch to your local
git pull <repository_remote_name> <branch_name>
Add files
To check all changes you made in the current directory, use this command
git status
To add files to your change, use this command
# Used to add all files in the currect directory
git add .# Used to add specific file(s) in the current directory
git add <file_name1> <file_name2> ... <file_nameN>
Commit the changes
After using git add
command, you should commit (save the progress) to your git directory using this command
git commit -m "<commit_messages>"
Save your changes to online project repository
After using git add
and git commit
command, you can save your changes on online project repository, such as Gitlab or Github using this command
git push <repository_remote_name> <branch_name>
Using branch
You should do your work on your own branch. To check all the branches on your git directory, use this command
git branch
To create a new branch, use this command
# Used to create a new branch
git branch <new_branch_name># Used to create a new branch and move to new branch
git checkout -b <new_branch_name>
To move to an existing branch, use this command
git checkout <branch_name>
To delete a branch, use this command
git branch -d <branch_name>
Check History From Commit
If you want to see you or your friend’s commit history on current branch, use this command
git log
Undo Commit
You can undo your commit if you something wrong. There are two methods, git reset
and git revert
. The difference between them is git reset
will reset the commit backward depends on the commit you choose, while git revert
will create a new commit that undoes the changes from previous commit. git reset
will delete the commit history, while git revert
will add new commit history.
# if you want to undo the last commit, change commit_hash to HEAD# revert to choosen commit
git revert <commit_hash># reset to choosen commit
git revert <commit_hash>
Git Flow in Software Engineer Project Course
As I said earlier, git flow is used to manage our workflow and track the changes on our git repository. Git flow divides all features on our project into smaller features, and put the small feature into different branches. We often call this smaller features as PBI (Product Backlog Item). For example, PBI-1 is working on authentication, PBI-2 is working on Profile Page and its related components.
Branch management
In group project, usually we divide git branch into five types:
- master/main
Main branch that used for storing production-ready source code. All codes that stored here are conflict-free, and ready for user. - staging/development
Main branch for developing. All codes made by you and your team are combined here, and will be used for sprint review (sprint review is a process on scrum methodology, which I will write the article for it later). If all the development team, scrum master, and product owner agree, staging branch will be merge to master/main branch. - feature (PBI-[1…n])
Branch to store a Product Backlog Item (PBI) that developed by development team. There are a lot of PBI branch, and the name of the branches depends on the name of Product Backlog Item. All feature branches will be merged on staging/development branch. - hotfix
Branch for bug fixing for master/main branch. - coldfix
Branch for bug fixing for staging/development branch.
On my team project for Software Engineering Project Course, we used master branch as production branch and all codes on master branch will be used for deployment. We also used staging branch to test the application before publishing it. After we agreed on the application when we did the sprint review, we merged staging branch to master branch.
When I or my teammates worked on a certain feature, we stored the changes on PBI branch independently, so my changes wouldn’t affect my teammates’ code. After my feature is done, I sent merge request to staging/development branch, and will be merged after my teammates give reviews and approvals.
For commit messages, we used some rules based on TDD (Test Driven Development, which I will the article for it later) such as:
- [RED] <description> : Commit testcase (expecting failed pipeline)
- [GREEN] <description> : Commit code implementation (expecting success pipeline)
- [REFACTOR] <description> : Commit code that has been enhanced by clean, faster, and effective code.
- [CHORES] <description> : Commit non-feature stuff and tasks.
That’s all I can write for you in this article, I hope this article helps you get used to Git and work together with your team using git flow. I will publish more article like this about my team development later such as TDD implementation, Scrum, etc. Thanks for reading! :D
References :
- Git reference by Software Engineering Projects, Computer Science Faculty at University of Indonesia
- Git flow guide by Software Engineering Projects, Computer Science Faculty at University of Indonesia