Add and remove files in Git

Completed

After initializing your local Git repository, you can start working on your AL extension. Every file you create needs to be added to your repository. To do that, you need to execute some commands. Let's look at different commands to add to or remove from your repository and commit your changes in the Git directory.

Add files

To add an untracked or modified file (or multiple files) to Git, you can use the add command. This will add your modified files from the working directory to the staging area. With the add command, you specify the filename of the file to add. An asterisk can be used to indicate all files.

git add <filename>

git add *

Within Visual Studio Code, you can add file by clicking the + sign next to a filename in the source control window (Ctrl+Shift+G). This action will put your file in the staging area. To add all the changed files, you can click the + sign in the changes section.

Screenshot of the + feature to add a file in the staging area.

The result of adding files to the staging area is visible in the staged changes section within Visual Studio Code. In the next screenshot, the app.json, BodyType.al, and .gitignore file are staged. They also are assigned the status code A, indicating that they are added.

Screenshot of the staged changes section in Visual Studio Code.

Remove a staged file

If you added a file to the stage area, but it shouldn't be included, you can unstage that file. To unstage a file, you need to use the reset command.

git reset HEAD <filename>

This will put the file back into the working directory and remove it from the staging area. If you do this on recently added files, your file will become untracked again.

Within Visual Studio Code, you can use the - sign to unstage or reset.

Screenshot of the - feature to unstage or reset a staged file.

Commit your staged files to the Git directory

Once your files are in the staging area, you can use the commit command to store them in the Git directory. Each commit will take all the files from the staging area, add them to the Git directory and include your username and email within the commit.

git commit

A commit always needs a commit message. If you use the previous command, the default editor will open and allow you to enter a commit message. You can also use the -m option.

git commit -m "Your message comes here"

Within Visual Studio Code, you have a GUI to enter your commit message. There are two locations where you can enter that message. You can enter it in the SCM pane just above the staged changes section.

Screenshot of the add a commit message area in the SCM pane.

If you do not provide a message when you click the commit button, Visual Studio Code will also ask you to enter a message.

Screenshot of prompt to provide a message.

The actual commit is done by using the check icon or the commit commands from the Git commands list. You can open the commands list by clicking the three dots.

The Commit Staged changes is only committing files in the staging area. The check icon is a shortcut for the Commit Staged command. Normally every commit creates a new commit entry in Git. Every commit generates a unique commit string that uniquely identifies that commit.

For example:

521747298a3790fde1710f3aa2d03b55020575aa

If you select the Amend option, your changes will be added to the last commit, instead of creating a new commit. This is useful if you forgot some files.

git commit --amend

Screenshot of the Commit changes in Visual Studio Code.

If no files are added to the staging area, but you click the commit button, then Visual Studio Code will show a message indicating that there are no files in the staging area, but it will also ask you if you would like to commit the files that are in your working directory instead. So, it will bypass the staging area. You can also do this by using the -a option on the commit command.

git commit -a -m "Your message comes here"

Once a commit is executed you click the branch name (in this example, master) and it will show you an option to create a new branch, but it will also show you the commit ID your branch is using.

Screenshot of the commit ID in the branch name.

It is recommended to often add your changed files to the staging area, and if they are ready and stable, you can commit them into your Git directory. Don't wait until the end of the day, instead commit your modifications as often as possible.

Remove a file from the repository

If you delete a file from your working directory using Windows Explorer or within Visual Studio Code, your file will still be part of the Git repository. Use the rm command to indicate the Git repository that you want to delete the file from your project. This action will be stored in your staging area, together with files you modified or added. When you perform a commit, your file removal will be completed in the Git repository.

git rm <filename>

A delete will automatically be detected in the working directory and made visible in Visual Studio Code. This file will get a status code of D, indicating that this file is deleted from disk. Stage this change to execute the rm command and commit to completely remove it from the Git Directory.

Screenshot of a deleted file marked with D in the working directory.

Maybe you do not want to delete your file from disk, but you do want to remove it from Git. You can use the rm --cached to do that.

git rm --cached <filename>

This action will remove your file from the staging area and execute a remove in the Git repository, but your file will still be on your disk. The file will become untracked again.

Undo your changes

If you made some modifications to a file and you would like to get the previous version back, you can use the checkout command. This will get the committed version from the Git directory and put this version in your working directory.

git checkout <filename>