Branch commands in Git

Completed

To use branching and merging in Git, you first need to learn about the commands that are built into Git to create a branch. The command is branch followed with a name for the new branch.

git branch <branchname>

When you execute the branch command, it (by default) uses the pointer of the current branch and create a new branch that points to the same commit as the current branch. The branch command doesn't automatically change the current branch to the new branch. Therefore, you need to use the checkout command.

git checkout <branchname>

Git uses another pointer, called the HEAD pointer, that points to the branch that is currently in use. Whenever you execute a checkout command, it changes the HEAD pointer to point to the selected branch.

You can also specify the -b option on the checkout command. This creates the branch and checkout in one command.

git checkout -b <branchname>

Let's have a look at a visual example. Three changes have been committed to the Git system on the main branch. The main branch is the currently selected branch because the HEAD pointer is referencing the main branch. With every commit, the main branch points to the newest commit.

Diagram of the Git branching with commits A, B, and C.

In the next step, a new branch feature-23 will be created, and the newly created branch will become the selected branch. The HEAD pointer moves from the main to the feature-23 branch. The checkout command with the option -b is used.

git checkout -b feature-23

Alternatively, you can use the branch command and then the checkout command.

git branch feature-23
git checkout feature-23

Diagram of a newly created branch with head pointer moved from main.

After modifying some files and executing the commit command, the feature-23 branch points to the newest commit, while the main branch still points to the previous commit.

The -a option is used to first stage the changes and immediately save the changes in the Git directory. The -m option is used to provide a message. In the example, the commit message uses a hashtag, therefore the commit is automatically linked to the work item with ID 1. We discuss this further in the module on Azure Boards later in this learning path.

git commit -a -m "Added new table #23"

Diagram of feature-23 branch pointing to newest commit with main pointing to previous commit.

In the next example, the main branch is selected again, a new branch feature-24 is created, and a new commit is added.

git checkout main
git checkout -b feature-24
git commit -a -m "Added card page #24"

Diagram of branch feature-24 with new commit added.

After feature 23 is developed, the decision is made to deploy that feature. Therefore, we need to merge the main branch with the feature-23 branch, because the main branch is used for builds and deployments.

This merge is an easy forward merge because we can just change the main pointer to point to the same commit as the feature-23 branch. To merge a branch, you need to select the branch you want to merge into. In this example, you want to merge the changes from feature-23 into the main branch.

git checkout main
git merge feature-23

Git forward merge diagram for feature-23.

Because the main and the feature-23 branch all point to the same commit, we can delete the feature-23 branch. This doesn't delete the commits and the files within the commit. It only removes the pointer feature-1. The main branch still points to commit D. To delete a branch, you need to use the -d option on the branch command.

git branch -d feature-23

Let's continue by adding a new commit to the feature-24 branch. So, first checkout the branch, make the modifications or add some new files to this branch and commit the changes.

git checkout feature-24
git commit -a -m "Added list page #24"

Diagram of new commit for feature-24 branch.

Feature 24 is developed and needs to merge into the main branch, so we can deploy the changes. This isn't a simple forward merge, because feature-24 branched off commit C. In the meantime, the main branch already has another commit merged into it. To resolve it, a three-way merge should be applied. This takes the last commit of the main branch, the last commit of the feature-24 branch and the commit that they have in common, which is commit C.

First you need to select the branch to merge into.

git checkout main
git merge feature-24

This automatically creates a new commit that merges the two branches. It's possible that you may encounter merge conflicts because there may be some objects that are both changed in the two branches. Resolve this merge conflict in your local repository.

Diagram of a three way merge applied.

In this case, you can safely remote the feature-24 branch.

To push a branch to a remote repository in Azure DevOps Server, you need to specify the --set-upstream option with the push command.

git push --set-upstream <remotename> <branchname>

This uses or creates a new branch on the remote repository. In Azure DevOps, you can select Branches in the Repos section and select the blue New branch button to create a new branch.

Screenshot of the Azure DevOps Branches page with New branch button.

Creating and changing branches in Visual Studio is simple. You don't need to use all the different commands. At the bottom left, you can see the active branch. Clicking that branch name brings up a menu in Visual Studio Code where you can create a new branch or select an existing branch.

You also see a list of the remote branches. By executing the fetch command, this list is updated if there's a remote branch that isn't listed. In the next example, there's only the main branch on the local repository and the main on the remote repository. If there would be another remote branch, you can just select that branch, and it automatically creates a local branch linked to that remote branch.

Screenshot of the Create new branch feature in Visual Studio Code.

To create a new branch, you can select the Create new branch option. It creates a new branch, based on the commit of the current branch.

Before you can change to another branch, you need to make sure all your changes are at least staged or committed. Otherwise, a change of branch gets the files in the state that they were on that branch commit. So, they overwrite the files in your working directory. If you don't want to lose your work, make sure to commit first!

In the next example a develop branch is created and as you can see, they both are using the commit with ID 5c2b7bb0.

Screenshot of the menu with develop branch created.

To add your branch to the remote repository, you have to select the Publish icon in Visual Studio Code and it arranges everything for you.

Screenshot of the button to Publish a branch.

To merge your changes, you first need to switch to the branch you want to merge into. So, if we would like to merge from the develop branch into the main branch, you need to switch to main.

After you selected the correct branch, you can use the Git: Merge Branch... command. Use View, Command Palette to search for the command. After that, you can select the branch you want to merge from.

Screenshot of the branch menue to select the branch to merge from.

On every merge, you have the risk of encountering merge conflicts. Resolve them in order to continue with the merge process and commit. In the example, we have a merge conflict because the main branch has a commit that included a new field Color in the Car table. In the develop branch, a new field Length was introduced in the same Car table. You can see that the ID that is used for the fields are different, so for AL, there's no problem using the two fields, but Git compares the two files on a line base. And the two fields are introduced on the same line within the Car.al file. Visual Studio Code displays a window where you can see the merge conflict, and you can resolve the problem. You can decide to keep your current main change; the incoming develop change, or accept both changes. Visual Studio Code opens it in a text file, so you can still make all the modifications you want. You can add extra code, remove code, change code, and so on. To select a change, you can select the text displayed in gray, which is just above the merge conflict.

Screenshot example of a merge conflict.

After you selected a change, your file will show up in the merge changes list. You still need to stage this file to the staged changes list before you can commit your changes.

Screenshot of the merge changes list.