7 git commands you should know

by May 20, 2023AL Language

Home 9 Development 9 AL Language 9 7 git commands you should know

For many years, developers in C/AL did not need to know anything about Git or other versioning tools. That has changed with Business Central, or more specifically with AL Language. Today, we will look at the most important (and basic) git commands any developer should know.

These commands are usually quicker and more professional than working with the GIT UI in VS Code.

I’m not trying to explain how the git works, what can be done or how to master the git. This article is mainly for new developers in AL Language or developers who are experienced in C/AL development but do not know how to work with git commands in AL Language. For this reason, I tried to explain some parts in a straightforward way or even provide not completely accurate information, as these details/knowledge are not necessary for people who are not used to git.

> # Init new repository. Be sure you are in the right folder...
> cd /MyFolder/TheRealFolderIWantToUse/
> git init
>
> # Add all files and create the first commit with message "Init Commit"
> git add *
> git commit -m "Init commit"
> 
> # Link the local repository with the remote one in DevOps (similar code will be for GitHub)
> # You do not need to remember these two lines, they are always suggested by DevOps when you create a new repo
> git remote add origin https://[email protected]/ORGANIZATION/PROJECT/_git/REPOSITORY
> git push -u origin --all
>
> # Add all .al files and create another commit
> git add *.al
> git commit -m "Another commit"
>
> # Add new changes to the previous commit (must be only local, not yet pushed to remote repository!)
> git add *.al
> git commit --amend
> # VS Code will open window with the overview of the commit, you can adjust the commit message or just close the window
> 
> # Check remote repository for any changes made by other developers
> git pull
>
> # Push local changes to remote repository
> git push
> 
> # Create a new branch
> git checkout -b "NewBranch"
>
> # Go to already existing branch
> git checkout main
>
> # Merge changes in main branch to NewBranch 
> git checkout "NewBranch"
> git merge main

And now, let’s explore each of these commands.

git init

The basic command to initialize a new repository. There is nothing special about this command; just be sure that you are in the folder you want to initialize (usually the parent folder of App/Test app folders).

git add

Another basic command that specifies which files/changes should be staged. You can use wildcards (*) to specify more than one file (for example, *.al specifies all .al files)

> git add .
> git add *
> git add *.al
> git add FileName.Table.al

git commit

Commit is the command you will be using most of the time. This command commits all the staged changes (added to the staged list using the add command). It’s always a good idea to provide an informative message (using the -m parameter).

I you need to update the last commit message or add new files/changes to the last commit, you can use the –amend parameter. This command will open a file with information about the last commit where you can update the commit message, and it will also automatically add all staged changes to this commit.

> git commit -m "Init commit"
> git commit -m "Another commit"
> git commit --amend

git pull

This command checks the remote repository for any new changes, and if there are changes, they will be downloaded and merged automatically to your local repository. It’s a bit more complicated, but for the purpose of this article, this should be well enough. There is a similar command, git fetch, but again, as this article is for beginners, you should be fine with the git pull only.

git push

This is the opposite command to git pull – it pushes your locally committed changes to the remote repository.

git checkout

Branches – the core of the versioning. I’ll publish another article in a few weeks about how to handle branch management and how to work with multiple branches. For now, just two basic commands

Git checkout changes your current branch to the branch you provide as a parameter. There is also a parameter (-b) that specifies that the branch does not exist yet and should be created. Without this parameter, if the branch does not exist, the command will end with an error.

> git checkout -b "NewBranch"
> git checkout main
> git checkout "NewBranch"

git merge

Git merge is the basic command you need to know when you are working with branches. It merges all changes from one repository to your current repository; for example, if other developers merged their changes to the main branch, you pull the changes to the main branch, checkout to your branch and using the git merge command, you can merge the changes in main to your branch.

If there are any conflicts that can’t be resolved automatically, VS Code will show you the UI editor, and it will be up to you to decide how the changes should be merged.

Recent Articles from the category

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here