Integrating Changes in Version Control Systems: Git Merge vs Git Rebase

Dolamu Oludare
Towards Data Engineering
6 min readAug 21, 2023

integrating workflow & changes from branch to branch.

Image by Author.

One essential tool for building innovative solutions in the technology space has been Git and GitHub. Git is a version control system that keeps track of changes while working on a project. GitHub is a cloud-based repository where copies of projects are stored, managed and tracked via Git.

One scenario where the concept of version control finds relevance is when there is collaboration on a major project. For instance, James and John are building a website for a Hotel. There are several features that should be present on the website and both of there is a tendency the work on the features is distributed amongst them.

Image by Author.

Firstly, there is a primary branch for the project, which is conventionally called the main or master branch. Then John has his own branch which he names the John_features branch and also James has his own branch which he named James_feature branch. So one issue they might face while working on the project is how they integrate changes from one branch to the other.

in Git, there are two basic ways of integrating changes from branch to branch: git merge and git rebase . In this article, we will learn a bit about branching in Git and also see how the git merge and git rebase works and the difference between both integration techniques.

Git Merge

To show how git merge works, we would carry out the following tasks.

  1. Create a folder on your local machine called merge and enter the folder with the following command.
mkdir merge && cd merge

2. initialize a git repository on your local machine with the following line of command.

git init

a hidden folder called .git should be created in the merge folder. To confirm if the folder was created, you can run the following line of command.

ls -la
Image by Author.

The folder has been created in my merge folder.

3. We are currently on the master branch. Now, create a file called m1.txt on the master branch with the following command.

touch m1.txt

after creating the file on the master branch, we need to add and commit it to the git repository with the following command in the snapshot below:

Image by Author.

We can also check our commit log to see if our commit was successful, you can use the git log command to do this.

Image by Author.

4. Now we would create a new branch called feature and switch to the branch using the following command:

git checkout -b feature

The command creates the feature branch and also moves into the branch. You can use the git branch command to see the available branches on the project and it also shows that we are currently on the feature branch with the asterisk (*) and the green color.

5. Now that we are on the feature branch, create a file f1.txt.

Image by Author.

We now have the f1.txt file we just created and the m1.txt file that we created on the master branch.

We would also need to add and commit the changes to the git repository and then check our commit log.

Image by Author.

The changes have been well captured in the commit log and we can see that the commit history is well arranged in chronological order. The HEAD pointer is currently on the feature branch because we are on the feature branch.

5. Switch to the master branch and create a file m2.txt .

Image by Author

Now, you can observe that our current change of adding the m2.txt file on the master branchhas been added to the commit log and the f1.txt file does not reflect on this commit log because we are currently on the master branch and the f1.txt file was created on the feature branch.

6. Now, we would merge the changes on the feature branch to the master branch. Firstly we would need to switch back to the master branch and run the following command:

git merge feature
Image by Author.

The changes from the feature branch has successfully been merged to our master branch.

When we check our commit history, you would notice that the f1.txt commit message has been added to our commit history even though we are currently on the master branch and a commit message was generated for the merge activity.

Image by Author.

This is what our branching looks like visually.

Observing clearly, no destructive operation was made in the merge operation, existing branches were not changed in any way, a new merge commit was created on the master branch, and the merge operation does not tamper with the history on the commit log at all.

Git Rebase

One of the dark sides of the git merge operation is when we have a lot of merge commits. The commit log can become so cluttered and muddled up that it confuses anyone that goes through the commit log. One way to solve this problem is to use the git rebase command.

To practicalize the git rebase operation, we would carry out the following tasks.

  1. create a directory called rebase and enter into the rebase directory with the following command:
mkdir rebase && cd rebase

2. initialize a git repository on your local machine with the following line of command.

git init

3. Create a file named m1.txt and add and commit the file to the git repository.

Image by Author.

4. Create a new branch called feature/rebase , and create a file called f1.txt on the branch.

Image by Author.

Check the commit log and you would observe that the f1.txt commit has been added to the commit log.

Image by Author.

5. Switch to the master branch and create a new file named m2.txt .

Check the commit log.

Image by Author.

6. Now, we would merge the changes on the feature/rebase branch to the master branch using git rebase.

Image by Author.

Observing the commit log, there is no merge commit and the feature/rebase branch has been rebased to the tip of the master branch. We now have a linear commit log.

Image by Author.

Git rebase changes the history of the commit tree.

  • Moves the entire feature branch to begin on the tip of the master branch.
  • Rewrites the project history in a much cleaner linear form.

Conclusion

In this article, we have successfully explored the concept of branching, merging, and the difference between merge and rebase operation in Git. Some of the things we have learned include:

  • To create a new branch:
git branch <name_of_branch>
  • To switch to a branch:
git checkout <name_of_branch> OR git switch <name_of_branch>
  • To merge changes from a branch:
git merge <name_of_branch_where_changes_were_made>
  • To rebase:
git rebase <name_of_branch_where_changes_were_made>

Thank you for reading!

--

--