Git for Beginners: Learn The Trunk Workflow

A short yet practical introduction to Git, common actions and the basic trunk workflow.

Git is the world’s popular version control system. It enables you to track changes in a directory and creates checkpoints in your code. Git also allows you to go back in time to any of these checkpoints at a later date.

Popular Services like Google Docs and Wikipedia have a rudimentary form of version control ingrained into them. They create checkpoints as you make changes to a document or an article, and enable you to go back to an older version at any point in time. Version control systems allow much more.

Early version control systems were centralized, where a central repository had the full code base and developers would potentially get access to only parts of the code. Git is a distributed version control system — which essentially means that every developer contributing to a project has the complete copy of the code base.

While Git is effectively used by large teams working on the same project, this post focuses on the benefits of using Git for beginners. It covers the installation of Git, setting up Git for the first time, core Git concepts and the centralized workflow.

Why Git?

When starting out with Git, it may be for the simple reason of an advanced backup system. At some point in time, I am sure you may have saved files like my_file.txt , my_file_v2.txt and so on. Git removes the need for your manual intervention in maintaining versions of your work.

For a beginner, the number of new terms associated with Git may be overwhelming, so this post covers basic Git concepts and the simplest Git workflow to follow — the trunk workflow. Working with this workflow allows beginners to focus on and get comfortable with core Git concepts before tackling advanced branching options. Let’s get started!

Tip: If working with components (React, Vue, NG or Node modules) use Bit (GitHub) to share your components across Git repositories- to build modular software faster, without having to write the same code twice. Check it out:

Installation

On Windows or Mac OS, you can install Git using the Windows and Mac installers for Git.

On a Debian based Linux OS, you can install Git using the package manager apt .

apt install git-all

The detailed instructions to install Git are listed on the official website.

Git Configuration

Once you have installed Git, you may wish to first set up basic settings that will be associated with your continued use of Git. First, create an empty directory and initialize Git. In Mac or Linux, the git command is available for use in the terminal. In Windows, you can start a Git Bash in any folder from the right click menu.

mkdir my_project

cd my_project

git init

The init command initializes an empty Git repository in a directory. Once initialized, you should run the following commands.

git config --global user.name 'Shaumik Daityari'

git config --global user.email 'sdaityari@gmail.com'

The --global option saves the name and email globally and associates them with changes that you make in any Git repositories in your system. Additionally, you can run the following command to automatically color code outputs of Git commands.

git config --global color.ui 'auto'

Git Process

The typical process to save the state of files in a directory and create a checkpoint is as follows:

Add a file: In this step, you explicitly tell Git to track the files in a repository.

git add my_file

Stage changes: This step tells Git to stage specific changes in tracked files to save. This step is useful when you have made changes to multiple tracked files, but would like them to appear in different checkpoints. The command to stage changes to a file that you have asked Git to track remains the same.

git add my_file

Commit staged changes: In this step, you create a checkpoint, which is termed as a commit in Git. Once a commit is successful, you would notice that a commit hash is associated with the commit. This hash uniquely identifies a commit in Git.

git commit -m "Commit message"

Push commits: Optionally, once you have committed changes, you can push them

git commit remote_name branch_name

Although branching is a powerful feature in Git, let us focus on the default branch, master for now. If you would like to explore branching in Git, you can read this detailed tutorial on Git Branching concepts.

Trunk Workflow: Features

The functionalities of branching in Git opens up a lot of possibilities for you. Therefore, it is necessary to follow certain guidelines while working with Git. A workflow in Git is a set of guidelines to follow. This tutorial would describe the simplest workflow in Git — the trunk workflow. In the trunk workflow, development happens only on the primary master branch.

Branching often is a bit overwhelming for beginners — therefore, the trunk workflow is a good starting point.

Before the rise of distributed version control systems, centralized version control systems were popular. In a centralized system, you would have a central copy of a repository where every developer would contribute to.

In fact, the default branch of a popular centralized version control system, subversion, was named trunk . Therefore, this simple workflow in Git is termed as the centralized or trunk workflow.

As development happens only on a single branch at a central location, all team members must have access to that location. Trunk workflow works best in small teams, where every member is trusted to make changes to the code base. Trunk workflow allows you to make frequent changes to your repository without a significant review process.

There are certain disadvantages of the trunk workflow too! When a team matures and new members are introduced, changes should not be merged without a review process.

You need to add a review process before a merge, which is possible only when you adopt a more complex workflow. Additionally, giving everyone access to the master branch of the central repository can have disastrous consequences.

To Err is Human: Undo Errors in Git

To write a tutorial aimed at beginners in Git without having a section on undoing changes is incomplete. Given that anyone new to Git would probably end up adding a file by mistake, or committing a change only to realize the commit message was wrong, this section deals with Git commands that can undo these changes!

If you have asked Git to track a file that you did not intend to, you can simply ask Git not to track it by running the following command.

git rm --cached my_file

Note that the --cached option is important, as using a --force option may remove the file from the directory altogether.

What if, in addition to tracking a file, you also staged changes for a commit? In that case, this command would not work. You will have to use the following command.

git reset HEAD my_file

HEAD points to the latest commit in your repository. The command tells git to change the tracked status of the file to the latest commit, which essentially un-stages the changes in the file.

Additionally, if you have committed changes that you did not intend to, you can use the following command to un-commit changes.

git reset --option HEAD~N

The command will undo changes in the last N commits of the active branch of your repository. There are three options while undoing commits:

--soft : Undo a commit to a state just before the commit command. This means that changes to files that were made in the last N commits are staged, but not committed. Using this option does not change the content of any file in the repository. This may be relevant if you would like to make an additional change before a commit.

: Undo a commit to a state just before the commit command. This means that changes to files that were made in the last N commits are staged, but not committed. Using this option does not change the content of any file in the repository. This may be relevant if you would like to make an additional change before a commit. --mixed : This option un-stages any changes that were in the last N commits, in addition to undoing the changes. The content of files in your repository remain the same in this case as well.

: This option un-stages any changes that were in the last N commits, in addition to undoing the changes. The content of files in your repository remain the same in this case as well. --hard : This is the option that not only undoes the commit but changes to files in the last N commits too. You should be careful while using this option.

Conclusion

In this post, we discussed the nuances for someone who is beginning to use Git. First, we talked about various features of version control and Git. Next, we covered how to install and set up Git, followed by an explanation of different concepts associated with Git. Further, we discussed the trunk workflow in Git. Finally, we covered how to undo various actions in Git.

Do you use Git in your day to day activities? Do let us know in the comments below, and feel free to ask anything! Thanks for reading 😃