Git/GitHub Basics #2 - Branches

Last Edited: 2/10/2025

This blog post introduces the concept of a branch in Git.

DevOps

In the last article, we covered how we can stage changes and make commits. There are many cases where we want to move between commits and undo changes. In this article, we will introduce ways of undoing commits and another way of managing different commits using branches.

Undo Commits

There are basically three ways of undoing commits: checkout, revert, and reset. The checkout is performed when we just visit and view the commit in read-only mode, which can be achieved by git checkout <id>. To return to the most recent version, we can use git checkout <branch-name> (we will discuss branches later). Since this method makes no changes to the commit history, it is the safest way of checking the commit history.

The revert performs a new commit that undoes the changes made after the specified commit, thereby returning to the previous state while preserving the subsequent changes in the commit history. The revert can be performed with git revert <id> -m "<message>", and you should see a new commit that reverts the change via git log. The last method, reset, deletes the commits after the specified commit using git reset <id>. While the code changes will remain in your text editor (and can be staged and committed as a new commit), it is a dangerous way of permanently deleting changes. Hence, we need to be careful when using the reset command.

Branches

So far, we have only been working with the main (or master) branch, but we can create multiple branches to avoid making critical changes to the main codebase while working on new features and collaborating with other developers. The diagram below shows how we can maintain a stable working codebase in the main branch while developing a feature.

Git Branches

Before confirming that the new feature is implemented correctly, we can create a new feature branch and work within that branch. Once the new feature is successfully implemented—and perhaps tested—we can merge the feature branch into the main branch. To create a new branch, we can use git branch <branch-name>. After creating a branch, we can confirm its creation by checking the list of branches with git branch -a, and switch to it using git checkout <branch-name>. Alternatively, we can create a new branch and move to it with git checkout -b <branch-name>.

As a demonstration, let’s create a new branch called c-txt to add a c.txt file. After creating and switching to c-txt with git checkout -b c-txt, we can add c.txt file and perform staging with git add . and make a commit with git commit. This should successfully create a new commit in the c-txt branch. We can confirm that the change is only applied within the c-txt branch by viewing the main branch with git checkout main.

Merging Branches

After confirming that c.txt does not cause any errors or unwanted behavior, we can merge the c-txt branch into the main branch. To do so, we can switch to the main branch and call git merge c-txt. Checking the current status, we should confirm that c.txt is now added successfully to the main branch. There should be no problem merging a new branch that only adds new files or texts to existing files. However, a merge conflict occurs when we are merging a branch that edited existing text in main differently.

For example, let's create a new branch called a-txt and add "Hello World!" to the first line of a.txt. Then, suppose someone with poor understanding of Git adds "Hi!" to the first line of a.txt directly on the main branch. When trying to merge a-txt into the main branch in such scenario, Git is unsure how to resolve the conflict at the first line of a.txt, causing the merge to fail.

When a merge conflict occurs, we need to resolve it and make a new commit. For example, we can resolve the conflict by keeping "Hi!" and moving "Hello World!" to the next line. After making these changes, we can stage them with git add <filepath>, and then create a commit (without a message since Git will automatically add Merge branch <branch-name> as a comment). Then, we can confirm that the conflict is resolved and branches are merged successfully by viewing git log.

Conclusion

In this article, we introduced three ways of undoing commits, the concept of branches, and how to use branches for properly managing projects. Now that we understand the core features of Git, we can discuss GitHub in the next article.

Resources