Often, as a programmer, we need to collaborate with other programmers especially on a software project. To make sure that the collaboration runs well, we need a collaboration tool that manages the software codes that we are working on. Here comes Git. Think of collaboration tools like google office, office 365, but it’s far more sophisticated. A tool that can for example cherry-picking which change(s) can be applied or version tracing & control, all in one tool. That’s basically what Git really is. Progress or change in git is represented as a node of a tree structure that’s called Git Tree and one software is represented as this git tree. Each branch on the git tree is independent of other branches so one change on one branch couldn’t affect the others. The branch management and ordering of this git tree structure are called git flow. To make our collaboration easier, we need to know the proper git flow.
In this article, I’ll explain some common git commands & git flow particularly in the software engineering project course that I took so we can gitting gud on collaboration. (Intermezzo: git gud & casul are terms that were coined by Dark Souls player that mean get good and newbie respectively)
Common Commands Tutorial
Before we talk about the common commands, make sure your computer/laptop is already installed git on your system. Some Linux distro especially Ubuntu have git preinstalled so you don’t bother to install it. But if you haven’t installed it, you can check out this source.
To initialize git directory on your project directory, you can use this command
Once you executed that command, there will be .git directory that signifies that this directory is a git directory so we can work on this directory by using git. You can use this command if you start a new git project or reinitialize a git project. This particularly can be skipped if you working on an ongoing git project. If you want to work on that kind of project from a certain online repository, you can fetch that directory by using commands
#This command will clone the main branch of a repository
git clone <repository_url>#This command will clone particular branch of a repository
git clone <branch_name> <repository>
p.s: If you clone a git directory, you can’t do git init since it has already .git on it unless you want to reinitialize it.
Before you commit your change, make sure you tell git who you are. To do so we can use these commands.
#This will setup the username
git config user.name <your_username>#This will setup the email
git config user.email <your_email>
This config usually adjusted with the repository where you wanna commit the change. If you commit the change to GitLab, then use your GitLab username & your GitLab email.
Connect your local git with repository git
After you did git init on your project folder, you can link that project directory to a repository by using
git remote add <repo_identifier_on_local> <repository_url>
That identifier by default is called origin although that identifier can be whatever we want. That “origin” serves as an identifier on the local branch for a particular online repository.
Add files to your change
To specify files that you want to add to your change, you can use these commands
#Add all files in the current working directory
git add .#Add single file
git add <file_name>#Add multiple files
git add <file1> <file2> <file3> <filen>
Commit a change
To mark a change that you have made, you can save that progress by using
git commit -m "<commit_message>"
Each commit has its own hash for identification purposes. Can I change the commit message? Sure you can by using this command.
git commit --ammend -m “<new_commit_message>"
Save Your Progress Without Marking It
I want to jump into another branch, but I want to save this progress without mark it since I haven’t completed it yet. Can I do this with git? Yes. You can do that by using these git commands
#Before jumping into another branch execute this command
#this will continue the last progress and drop it from the progress stack
git stash pop #this will continue the last progress without dropping it from the progress stack
git stash apply#this will throw away the last progress
git stash drop
Synchronize The Local Git
To synchronize your local git, you can use
git pull origin <branch_name>
This command will synchronize all changes in a particular branch of the remote “origin” and apply those changes into the local directory. I already executed that command and I got conflicting changes. What should I do? You can resolve that conflict by fixing it on the source code and then add & commit it. If you just want to keep track of a branch on a remote repository only, you can use
git fetch <remote-identifier> <branch>
Store Your Changes to Online Repository
After you did all the add & commit processes, you can store your change on the online repository that you want by executing
git push origin <branch_name>#Usually I use this command to make local branch linked with the remote branch
git push -u origin <branch_name>
Create & Delete Branch
To create a new branch, you can use these commands
#Make new empty branch
git branch <new_branch>
#Make new branch that is derived from current working branch
git checkout -b <new_branch>
To delete a branch you can use these commands
git branch -d <branch_name>#Force delete a branch
git branch -D <branch_name>
Move To Another Branch
To move to another branch you can execute this command
git checkout <branch_name>
Check Commit History
If you want to check the commit history of the current working branch, you can execute this command
There are two methods to undo commit in git and that’s git reset and git revert. The difference is git revert will commit the target commit as a new commit meanwhile git reset will forget the commits that commited after the target commit and set a target commit as a current commit.
Here are several commands to execute them
#Revert to a certain commit
git revert <commit_hash>#Reset to a certain commit
git reset <commit_hash>
If you want to undo the previous commit, you can change commit_hash with HEAD.
Combine Two Branches
Say you wanna apply all changes on a source branch to a target branch. There are there approaches to do so, merge, merge with squash, and rebase. The difference between those three lies in the commit. Git merge will apply all commits on a source branch to a target branch. While git merge squash will also put all the changes into one commit on the target branch. Git rebase will move all the commits on top of the head commit of the target so it doesn’t add any additional commit. If you prefer the traceability aspect, use git merge, if you prefer linearity of the flow & cleaner commit, use git rebase. Personal opinion, you work on a team, it’s better to use git merge rather than git rebase. This visualization can help you understand those differences.
To use those commands you can use these commands
git checkout <target_branch>
git merge <source_branch>#Merge with squash
git checkout <target_branch>
git merge --squash <source_branch>#Rebase
git checkout <source_branch>
git rebase <target_branch>
git checkout <target_branch>
git merge <source_branch>
Git Flow in Software Engineering Project
Usually, in a project, the git branch consists of five types:
- master/main: location of the production-ready code
- development/staging: location of the development code
- feature: location of the implementation of a certain feature
- coldfix: location of the bugfix for the staging branch
- hotfix: location of the bugfix for the master branch
In the software engineering project course, we use master as the main branch, staging as the development branch, PBI-n as the feature branch, coldfix as the coldfix branch, hotfix as the hotfix branch. When we work on a particular feature, we push the changes on PBI branch. This PBI branch can be split into multiple branches to avoid conflicts. Once the feature is completed, we merge PBI to the staging branch. We can only merge to staging once we have at least two approvals from two persons on the team. Once the code in the staging branch is production-ready or well-tested we can do merge request to master. The development branch is related to the development environment while the master branch related to the production environment (final product).
As the commit rule, we use these commit tags:
- [RED] <description>: Commit that contains testcase stuffs
- [GREEN] <description>: Commit that contains implementation
- [REFACTOR] <description>: Commit that contains refinement of the code that doesn’t break the logic
- [CHORES] <description>: Contains non-functional stuffs & grunt tasks
That’s all from me. I hope this article will be helpful especially for programmers considering git is an essential tool that every programmer must know in my opinion. You can check out this link to learn more about Git. See ya on the next article& let’s git gud on Git!
git flow reference for PPL 2021
git guide for PPL 2021