Useful GIT commands and alias/shortcuts

This is something you might also be familiar with. We at Comsol Unternehmenslösungen AG are passionately discovering the new possibilities AND responsibilities Microsoft offers us in D365 Business Central with AL, GIT and VS Code. Especially GIT is a challenge as it is a technical hurdle for some of the colleagues. Little by little we find the functions we need in our daily business. And here I would like to share some of them.

In this post I will list my most used GIT commands and show how to create two nice little GIT shortcuts…

Info: I will expand this post over time!!!

The Commands

#############################
## Remote repository handling
#############################

# Add a remote repo named "origin" to your local repo
git remote add origin "https://..."

# Pull branch "master" from your remote repo "origin"
git pull origin master

# Push a commit to your remote repo
git push origin master

##################################
## Staging and committing of files
##################################

# Add all modified files to staging
git add -A

# Add single files to staging
git add filename.al

# Undo adding the file to staging
git reset filename.al

# Create a commit for your staged files 
git commit -m "commit message"

# Revert the last commit
git reset --soft HEAD~1

############
## Branching
############

# Create a branch of your master
git checkout mynewbranch

# Merge new branch "mynewbranch" back into master
git checkout master
git merge mynewbranch

#####################
# Repository handling
#####################

# Check the status of repo. Are there any changes?
git status

# Reset local branch to remote status. All local changes are lost
git fetch --all
git reset --hard origin/master

# Update and reset remote branch list in local repo
git remote update origin --prune

Creating a GIT shortcut

Now we will create the (imho) extremely useful GIT shortcut “git dev XYZ” and “git delete XYZ” which will automatically create/delete a local branch of our master branch and automatically naming it “dev/username/xyz”.

First, we must edit or gitconfig file. To do so, execute following line in your VS Code to edit your gitconfig:
git config –global –edit (The user-based config could be found at your user directory)

# This is Git's per-user configuration file.
[user]
	name = Jens
	email = [email protected]
	initials = jfr 
[alias]
    dev = "!f() { git checkout -b dev/`git config --global user.initials`/$1; }; f"
    delete = "!f() { git branch -d dev/`git config --global user.initials`/$1; }; f"   

To use the snippet, we simply hack it into our console in VS Code:

new git shortcut

What’s so special about that?

Especially if you guys use Azure DevOps this little snipped brings you some nice order into your remote repository, because all branches are stored in this order and keep the branch overview clean. Corresponding directories for all your colleagues are created:

branch overview in azure devops

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

Leave a Reply

Your email address will not be published. Required fields are marked *