- Published on
How to be A Pro in Code Version Control
- Authors
- Name
- Advis Tasyah Mulia
- @licacila_
Dont create new folder for your version management
In the field of information technology, version control is a crucial skill that every software engineer must possess. A proactive software engineer should be able to roll back their code to a stable previous version in case of errors without storing multiple versions that could consume memory. Version control allows monitoring of every new line of code created through activity logs. The commonly used version control system is Git.
What is Git ?
Git is a free and open-source software application that uses a merkle tree as a data structure to store the history of events/commits that occur in a data folder. Git can organize and serve as a revision control system for new code features. Folders using Git are marked by the presence of a .git folder. There are cloud services available for storing Git code folders, such as gitlab, github, and bitbucket.
Why Use Git ?
- Can be used publicly
- Easy-to-use API documentation
- Version control simplifies group work
- Cloud services for storing Git repositories are available
Getting Started with Git
Git can be used by installing the git command line from the official website at the following link: Git Official Website. After installing the command line, Git can be initiated by running the command:
- git init
A folder that uses Git as its version control can be referred to as a repository. Once initiated, developers can start writing the code for their project.
With every change, developers can create a log describing the new code they've written. This log is called a commit. To make a commit, developers need to specify which new files they want to commit by using the following command:
- git add
${new created file}
To create a snapshot of all files, use the command:
- git add .
Then, the next step (commit) is made using the command:
- git commit -m "description of the new feature created"
And you have made a commit that can be revised in case of errors later on.
Integration with Cloud Service
In a Git-enabled folder, developers first need to create a repository on the chosen cloud service, such as GitHub for example.
After creating an empty repository on the cloud, developers can link the existing Git folder by establishing a remote connection using the command:
- git remote add origin
${url_empty_repository}
How To Use Git as A Pro
In its usage, there are several Git commands and terms that a proficient software engineer must understand in the context of Git flow and merge requests created by other developers. What are Git flow and merge requests?
Git Flow
Git has a main branch, commonly referred to as master/main, which serves as the central code repository. When collaborating on code, a best practice is to create new branches for each individual and each feature to be implemented. For example, creating a branch named advis/landing_page.
Having different individuals code run in a serial manner is not efficient, as it leads to inefficiencies in the work process. Instead, code development should occur in parallel. Therefore, each branch created can update its code with the main branch by pulling the changes using the command:
- git pull origin
${branch_name}
This command can be used for branches other than the main branch, in situations where a branch needs code from another branch.
Merge Request
After a developer creates a new feature in the branch they have created, they can request to merge the new code into the main branch using a merge request.
A merge request can be initiated after the code has been committed. Git will check the newly created branch for possible conflicts that may occur in the new code. Conflicts in a merge request can arise due to:
- Code not being updated against the main branch
or
- Two different commits existing in the same file
As a professional software engineer, conflicts in Git should be resolved promptly to avoid any blockages in delivering the best possible outcome to users.
The image above is an example of a merge request from my PPL group, Paytungan. The merge request is directed to the staging branch, which is our development branch. Before a merge request is accepted, each developer can review the code to be merged, a practice commonly known as code review. Additionally, the MR allows us to see the results of the pipeline jobs that were run. In the following section, any failures in the code and the test results in the MR can be examined. Once the code review is completed and approved, the MR can be executed, and the new code will be saved in the intended branch.
Vscode to Resolve Conflict
Conflict resolution occurs due to discrepancies between the new commit and the parallel remote commit. In VSCode, there are terms that differentiate user commits from remote conflict commits :
- Current Change (Changes committed by the user locally)
- Incoming Change (Remote changes)
There are 3 resolution options offered: accept current change, accept incoming change, and accept both. Accepting both will keep both sets of code, and no code will be deleted between them. This is an important feature and, in my opinion, a helpful feature in VSCode as it allows us to compare the conflicting codes.
Advanced Control
Git is a sophisticated tool; Git commands are not only used for low-level functions like git push and git pull. There are certain conditions where additional steps are required to prevent code from damaging the branch code due to asynchronous code push activities.
Git Rebase
In Paytungan, we need an input page. However, during the development of the input page, a new commit was made on the main branch to add a button component. What are the correct steps to make the commits look neater and move the branch to become the HEAD of the main branch?
Use: git rebase
The use of git rebase can make commit history clearer by displaying the commits that have been made, so hopefully, commit messages like 'merged from branch dev' won't appear.
git merge combines the entire last state of both branches, while rebase moves the commit history.
Additional command line:
git rebase <branch_name>
git rebase --abort # undo the rebase
### Additional Syntax
git rebase --continue
git rebase --skip
By executing rebase, there is a possibility of conflicts occurring. If conflicts occur, developers are required to resolve conflicts first. Once resolved, rebase can be continued using the additional 'continue' command. The 'skip' command can be used if developers want to skip commits that cause conflicts.
Git Stash
Imagine a scenario where we have made a lot of progress, but there was a revert on the main branch due to mistakes and errors. However, we want to keep the changes we have made
When this happens, developers can use git stash to save the progress to a stack that holds local modifications and revert the work being done to the HEAD position.
Git Fetch
Git fetch is a command used to update the local git with the remote git stored in the cloud. For example, when making changes locally, our friend pushes a new branch called 'new_branch,' and we want to see their work locally. With the current state, the 'git checkout new_branch' command will result in an error because the branch is still on the cloud. This is where the 'git fetch' command comes into play. After executing 'git fetch,' we can switch to the new branch or pull new commits available on the main branch.
In Conclution
Version control makes it easier to view code revisions and facilitates collaborative coding efforts. Git is a widely used version control system. There are many commands in Git operations, ranging from 'git add' to 'git push.' Conflicts can occur if the code being written has conflicting lines with different commits. A great engineer must understand these concepts thoroughly and be able to resolve any conflicts that may arise.