Why Git for AL Development?
Thank you for reading this post, don't forget to subscribe!
Git is the backbone of modern version control, enabling you to manage your AL code (.al files, app.json, etc.) with precision. Using Git in Visual Studio Code ensures you can:
- Track changes to your AL objects (tables, pages, codeunits).
- Collaborate with teammates on shared Business Central projects.
- Roll back mistakes or experiment safely with branches.
- Deploy reliable extensions by maintaining a clean codebase.
Let’s explore the must-know Git commands to supercharge your AL development process.
Basic Git Commands
- Initialize a Repository
- Command: git init
- What it does: Creates a new Git repository in your project folder, setting up the .git directory to track your AL files.
- Use case: Start version control for a new Business Central extension project.
- Clone a Repository
- Command: git clone <repository-url>
- What it does: Downloads a remote repository (e.g., from GitHub or Azure DevOps) to your local machine.
- Use case: Get the latest AL project from your team’s shared repository.
- Check Status
- Command: git status
- What it does: Displays modified, staged, or untracked files in your working directory.
- Use case: Verify which AL files (e.g., page.al or app.json) have changes.
- Stage Changes
- Command: git add <file> or git add .
- What it does: Adds specific files or all changes to the staging area for the next commit.
- Use case: Prepare modified AL files for committing.
- Commit Changes
- Command: git commit -m “<message>”
- What it does: Saves staged changes to the local repository with a descriptive message.
- Use case: Record updates like “Added customer page in AL” to your project history.
- Quick Stage and Commit
- Command: git commit -am “<message>”
- What it does: Stages and commits all tracked file changes in one step.
- Use case: Quickly commit updates to existing AL files.
Branching and Merging
- List Branches
- Command: git branch
- What it does: Shows all branches, with the current branch marked by an asterisk.
- Use case: Check available branches (e.g., main, dev) in your AL project.
- Create a Branch
- Command: git branch <branch-name>
- What it does: Creates a new branch for isolated development.
- Use case: Start a branch like feature/customer-page for a new AL feature.
- Switch Branches
- Command: git checkout <branch-name>
- What it does: Switches your working directory to the specified branch.
- Use case: Move to a feature branch to work on AL code.
- Create and Switch Branch
- Command: git checkout -b <branch-name>
- What it does: Creates and switches to a new branch in one command.
- Use case: Begin work on a new AL extension feature.
- Merge Branches
- Command: git merge <branch-name>
- What it does: Combines changes from the specified branch into the current branch.
- Use case: Merge a completed AL feature into the main branch.
- Delete a Branch
- Command: git branch -d <branch-name>
- What it does: Removes a branch after its changes are merged.
- Use case: Clean up old feature branches.
Working with Remote Repositories
- Add a Remote
- Command: git remote add <name> <url>
- What it does: Links your local repository to a remote one.
- Use case: Connect your AL project to a GitHub or Azure DevOps repository.
- Push Changes
- Command: git push <remote> <branch>
- What it does: Uploads your local commits to the remote repository.
- Use case: Share your AL app changes with your team.
- Pull Updates
- Command: git pull <remote> <branch>
- What it does: Fetches and merges remote changes into your local branch.
- Use case: Sync your local AL project with team updates.
- Fetch Changes
- Command: git fetch <remote>
- What it does: Downloads remote changes without merging.
- Use case: Check for updates without modifying your AL files.
- View Remotes
- Command: git remote -v
- What it does: Lists all remote repositories and their URLs.
- Use case: Confirm your AL project’s remote connection.
Inspecting History
- View Commit History
- Command: git log or git log –oneline
- What it does: Shows the commit history, optionally in a compact format.
- Use case: Review changes made to your AL app over time.
- Compare Changes
- Command: git diff or git diff <file>
- What it does: Displays differences between your working directory and the last commit.
- Use case: Check modifications in an AL file before committing.
- Inspect a Commit
- Command: git show <commit>
- What it does: Shows details of a specific commit.
- Use case: Examine changes in a particular AL commit.
Undoing Changes
- Unstage Files
- Command: git reset <file>
- What it does: Removes a file from the staging area but keeps changes.
- Use case: Unstage an AL file you don’t want to commit yet.
- Revert to a Commit
- Command: git reset –hard <commit>
- What it does: Discards all changes after the specified commit.
- Use case: Revert your AL project to a previous state (use cautiously).
- Undo a Commit
- Command: git revert <commit>
- What it does: Creates a new commit that reverses the specified commit’s changes.
- Use case: Safely undo a problematic AL change.
- Discard File Changes
- Command: git restore <file>
- What it does: Reverts a file to its last committed state.
- Use case: Discard unwanted changes in an AL file.
Stashing Changes
- Stash Changes
- Command: git stash
- What it does: Temporarily saves uncommitted changes and cleans the working directory.
- Use case: Pause AL development to switch branches or pull updates.
- List Stashes
- Command: git stash list
- What it does: Displays all saved stashes.
- Use case: Check stashed AL changes.
- Apply Stash
- Command: git stash apply or git stash apply stash@{n}
- What it does: Reapplies stashed changes.
- Use case: Resume work on stashed AL code.
- Apply and Remove Stash
- Command: git stash pop
- What it does: Applies and removes the most recent stash.
- Use case: Restore and clear stashed AL changes.
- Delete Stash
- Command: git stash drop
- What it does: Removes the most recent stash.
- Use case: Discard unneeded stashed changes.
Tagging Releases
- Create a Tag
- Command: git tag <tag-name>
- What it does: Marks a specific commit as a milestone (e.g., a release).
- Use case: Tag a stable version of your AL extension (e.g., v1.0.0).
- Create Annotated Tag
- Command: git tag -a <tag-name> -m “<message>”
- What it does: Creates a tag with metadata.
- Use case: Add release notes for an AL app version.
- Push a Tag
- Command: git push <remote> <tag-name>
- What it does: Uploads a tag to the remote repository.
- Use case: Share an AL release tag with your team.
- Push All Tags
- Command: git push <remote> –tags
- What it does: Syncs all tags to the remote repository.
- Use case: Share all AL version tags.
Collaboration and Advanced Commands
- Rebase Branches
- Command: git rebase <branch>
- What it does: Reapplies your commits onto another branch for a cleaner history.
- Use case: Integrate AL changes from main into your feature branch.
- Cherry-Pick Commits
- Command: git cherry-pick <commit>
- What it does: Applies a specific commit to your current branch.
- Use case: Bring a single AL bug fix to another branch.
- Track Changes
- Command: git blame <file>
- What it does: Shows who modified each line in a file and when.
- Use case: Identify who changed an AL file (e.g., page.al).
Configuration Commands
- Set Username
- Command: git config –global user.name “<name>”
- What it does: Sets your name for commit authorship.
- Use case: Ensure your AL commits are attributed to you.
- Set Email
- Command: git config –global user.email “<email>”
- What it does: Sets your email for commits.
- Use case: Link your AL commits to your email.
- View Configuration
- Command: git config –list
- What it does: Displays all Git settings.
- Use case: Verify your Git setup for AL development.
Miscellaneous Commands
- Clean Untracked Files
- Command: git clean -fd
- What it does: Removes untracked files and directories.
- Use case: Clean up unused files in your AL project folder.
- View Reference Log
- Command: git reflog
- What it does: Shows a log of all reference changes (e.g., commits, resets).
- Use case: Recover lost AL commits or branches.
Thank you for reading this post, don’t forget to subscribe!