Git branches – Source Code Management with Git and GitOps

A Git branch is a copy of the code base (from where the branch is created) that you can independently modify and work on without affecting the main code base. You will want to create branches while working on new features to ensure that you are not affecting the main branch, which contains reviewed code. Most technology companies normally have several environments where you have code deployed in various stages. For example, you might have a development environment where you test your features, a staging environment where you integrate all features and test the complete application, and a production environment where the application that your end users access resides. So, there would be a possibility that you might have additional environment-specific branches where code deployed on those branches reside. In the following sections of this chapter, we will talk about GitOps, which works on this fundamental principle. For now, let’s look at how we can create and manage Git branches.

Creating and managing Git branches

To create a Git branch, you must be on the branch from where you want to branch your code. As in our example repo, we were working on the master branch. Let’s stay there and create a feature branch out of that.

To create the branch, run the following command:

$ git branch feature/feature1

As we can see, the feature branch has been created. To check what branch we are on now, we can use the git branch command. Let’s do that now:

$ git branch

And as we see by the * sign over the master branch, we are still on the master branch. The good thing is that it also shows the feature/feature1 branch in the list. Let’s switch to the feature branch now by using the following command:

$ git checkout feature/feature1

Switched to branch ‘feature/feature1’

Now, we are on the feature/feature1 branch. Let’s make some changes to the feature/ feature1 branch and commit it to the local repo:

$ echo “This is feature 1” >> file1

$ git add file1

$ git commit -m “Feature 1”

[feature/feature1 3fa47e8] Feature 1

1 file changed, 1 insertion(+)

As we can see, the code is now committed to the feature/feature1 branch. To check the version history, let’s run the following command:

$ git log

commit 3fa47e8595328eca0bc7d2ae45b3de8d9fd7487c (HEAD -> feature/feature1)

Author: Gaurav Agarwal <[email protected]>

Date:     Fri Apr 21 11:13:20 2023 +0530

Feature 1

commit 17a02424d2b2f945b479ab8ba028f3b535f03575 (origin/master, master)

Author: Gaurav Agarwal <[email protected]>

Date:     Wed Apr 19 15:35:56 2023 +0530

Added fourth line

As we can see, the Feature 1 commit is shown in the Git logs. Now, let’s switch to the master branch and run the same command again:

$ git checkout master

Switched to branch ‘master’

Your branch is up to date with ‘origin/master’.

$ git log

commit 17a02424d2b2f945b479ab8ba028f3b535f03575 (HEAD -> master, origin/master)

Author: Gaurav Agarwal <[email protected]>

Date:     Wed Apr 19 15:35:56 2023 +0530

Added fourth line

commit f5b7620e522c31821a8659b8857e6fe04c2f2355

Author: Gaurav Agarwal <<your-github-username>@gmail.com>

Date:     Wed Apr 19 15:29:18 2023 +0530

My third commit

As we can see here, the Feature 1 commit changes are absent. This shows that both branches are now isolated (and have now diverged). Now, the changes are locally present and are not in the remote repository yet. To push the changes to the remote repository, we will switch to the feature/ feature1 branch again. Let’s do that with the following command:

$ git checkout feature/feature1

Switched to branch ‘feature/feature1’

Now that we’ve switched to the feature branch, let’s push the branch to the remote repository using the following command:

$ git push -u origin feature/feature1

Enumerating objects: 5, done.

Counting objects: 100% (5/5), done.

Delta compression using up to 12 threads

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.

Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for ‘feature/feature1’ on GitHub by visiting:
remote: https://github.com//first-git-repo/pull/new/feature/
feature1
remote:
To github.com:/first-git-repo.git

[new branch] feature/feature1 -> feature/feature1

Branch ‘feature/feature1’ set up to track remote branch ‘feature/feature1’ from ‘origin’.

With that, we’ve successfully pushed the new branch to the remote repository. Assuming the feature is ready, we want the changes to go into the master branch. For that, we would have to raise a pull request. We’ll look at pull requests in the next section.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these