Set up a development workflow for your application by Git
When we develop our application, sometimes we need to work in a team to accelerate the completion time of work. Working together is awesome but it is actually not as easy as that, let’s say we share our source code to every person on a team, then think for the case when we need to merge all different source code from every developer after they done to implement a feature. At that time, we need at least one skillful-developer that able to merge all source codes without issues or we need to have a meeting for all developers, together combine all source codes into single-source codes. For a small project, it is applicable, but not for a large-scale project and still, it’s not a good practice even for a small project. Also, sometimes we need to go back to a specific state of source codes in order to track our codes or remove some features. Thanks to Git, for doing so we are able to tackle all those cases.
A Brief Introduction of Git
With Git, you able to control your codes by commits and branches. A commit, it’s like a state, what the latest thing you have done in that commit/state and also a commit comes alongside with a message, so it must be more understandable for any developer who wants to know what a developer has done in that commit. A branch is a branch, for example, you don’t want to mess up with your main codebase, then you are able to create a new branch without no effect to your main branch that is your main codebase, and in the end you able to merge your new branch to your main branch. I strongly suggest you check this awesome interactive tutorial of Git for better understanding, there are a lot of techniques there you will use in real-world problemS for sure.
Some basic commands of Git
When you want to initiate git settings to your folder or a repository.
Or if you have a link of the repository that used git somewhere on the internet you can use this command.
git clone [LINK]
Set a remote URL for a repository that you are working on.
git remote add [NAME_OF_REMOTE] [URL]
Pull a repository after you set a remote URL in a specific branch.
git pull [NAME_OF_REMOTE] [NAME_OF_BRANCH]
Check the status of files in the folder, whether it needs to commit or in the ready-commit state or view the current branch.
Make a commit
git add [NAME_OF_FILE | PATTERN]
git commit -m "[MESSAGE]"
Change the message of the latest commit and you will see a text editor VIM to edit the message.
git commit --amend
List all of the commits from the current commit
Specify the number of logs
git log -[N]
Push you all new commits to a remote repository
git push [NAME_OF_REMOTE] [NAME_OF_BRANCH]
Create a new branch and go to that branch after you have created it
git checkout -b [NAME_OF_BRANCH]
Only create a new branch
git branch [NAME_OF_BRANCH]
Delete an existing branch
git branch -D [NAME_OF_BRANCH]
List of all branches in local
git branch -v
Change the current branch
git checkout [NAME_OF_BRANCH]
Merge from a specific branch to the current branch.
git merge [NAME_OF_SPECIFIC_BRANCH]
Copy some commits from many different branches and put it in the correct order to the current branch. You can pick 1 or more commit, it’s up to you.
git cherry-pick [COMMIT_HASH_1] [COMMIT_HASH_2] [COMMIT_HASH_3]
Copy all commits from current commit to the same parent commit to another branch or commit hash
git rebase [NAME_OF_BRANCH | COMMIT_HASH]
Copy an existing commit and make it as a new commit after a current commit.
git revert [BRANCH_NAME | COMMIT_HASH]
Remove an existing commit permanently
git reset [BRANCH_NAME | COMMIT_HASH | HEAD~[N]]
You can configure your development workflow as you want, for instance, you want to create only 2 types of branch such as a main branch and a development branch. But in that case, sometimes there are a couple of things you need to be considered, such as before it’s going to production you need to test or check the quality of feature your team is working for together after all branch has merged from a development branch to master branch. In the next section, I introduce you Git Flow that has one more branch as a middle branch between a main branch and a development branch.
This flow is commonly used by many companies, the idea is simple you have 3 types of branch to work on your features, are namely production, staging, and development. Production is made for a final release after it has been reviewed in staging, and the development branch is where you are working to your feature. Rather than use 2 branches, it will more ensure your codes to have good quality checking and validate the requirements. For better understanding, look at this example of work with git glow. master is a production branch, develop is a staging branch, and feature is a development branch.
Also, there are more 2 branches to use to fix your codes for a specific branch, these are a cold-fix for a staging branch and a hot-fix for a production branch. Hot-fix branch is a crucial thing to know, that it helps you to fix what happens in the master branch directly without fixing it in a development branch or staging branch that takes more time and the same as cold-fix.
Git is awesome, it helps your team to speed up for finishing tasks as possible and save your time a lot whenever you need to combine all the work or track your old commits. In a real job, you are almost impossible not to meet git for their application version control, so it won’t be useless to learn git, believe me. Again, I want you to check this interactive tutorial if you want to get a better understanding because this is just the introduction of git.