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.