MODX Git Fu

I use Git a lot. It has saved my bacon more times than I can count. I thought I’d write a little about how I have Git set up in MODX and about an easy way to create Git aliases to make life with Git a little easier.

There are a number of Git GUI options, and as I mentioned in the last article, Git features built into PhpStorm (and other editors). If you’re using any of those, this article may not be of much use to you. If, on the other hand, you’re a “command-line” type and don’t already have a bunch of Git aliases set up, read on.

MODX logo

Git for MODX Projects

I have dozens of different MODX projects in Git. Most of them are extras, but some are small custom projects for clients. I have them all inside the assets/mycomponents/ directory of a localhost MODX install. The MODX install is kept up-to-date (more or less), and I have separate MODX installs for other versions. If I need to test an extra in another version of MODX, I can just build it, copy the transport.zip file to the other version’s core/packages directory and install it in Package Manager with “Search Locally for Packages.”

Each extra has its own separate Git repository and (for the public ones) its own repo on GitHub. This is remarkably easy to do once you’ve installed Git and Git Bash. Here are the steps:

  • In Git Bash, CD to the root directory of the project (e.g., assets/mycomponents/newspublisher.
  • Type: Git Init
  • Type: Git Add .
  • Type: Git commit -a -m “Initial Commit”
  • Go to GitHub (create an account there if necessary) and follow the steps for creating a new repository.
  • GitHub will give you the commands for setting up a remote to your new repo. Issue them in the Git Bash window on your local machine (while still in the project’s directory).
  • In Git Bash, type: Git Push Origin Master

(Notice that there’s a period after the “Git Add” command, which means add everything here.)

Once you’ve completed the steps above, you’ll have both a local and a remote Git repository for your project. At this point, I usually create a “dev” branch and push it to GitHub with these commands:

[code language=”html”]
git checkout -b dev
git push origin dev
[/code]

What About the Aliases?

Here’s why I need aliases for Git. When I launch Git Bash, I’m not “in” any of my many Git Repos. In order to work on NewsPublisher with Git, for example, I would have to type this:

[code language=”html”]
cd /c/xampp/htdocs/addons/assets/mycomponents/newspublisher
[/code]

That’s a lot of typing, especially if I’m working on several different projects in one work session. Using aliases means that I can just type
np at the Git Bash prompt and be in the directory for the NewsPublisher project. It works because I’ve set up np as an alias for the long line above.

How?

As you may know, you can create an alias in Git by typing “git config” followed by some stuff I can never remember. Here’s a couple of examples:

[code language=”html”]
git config –global alias.co checkout
git config –global alias.ci commit
[/code]

In the .gitconfig file, the commands above would produce this:

[code language=”html”]
[alias]
alias co=’checkout ‘
alias ci=’commit ‘
[/code]

I almost never use git config for aliases. It will only set Git command aliases so you always have to type “git” in front of them to get Git’s attention. I prefer to use the Bash Profile file to create my aliases. As far as I know, all command-line shells for Git have a profile file. On my localhost/XAMPP/Windows setup, the file is C:/users/Bob Ray/.bash_profile. You’ll have to locate (or create) yours. Git Bash reads this file when it launches (and you’ll have exit and relaunch the shell before any changes will take effect). In the .bash_profile file, the lines above would look like this:

[code language=”html”]
[alias]
alias co="git checkout"
alias ci="git commit"
[/code]

Notice the added git in front of each command. Now, when I type co at the command line, it’s interpreted as git checkout.

Here are some common abbreviations for Git:

[code language=”html”]
alias gs=’git status ‘
alias ga=’git add ‘
alias gb=’git branch ‘
alias gc=’git commit ‘
alias gd=’git diff ‘
alias go=’git checkout ‘

alias got=’git ‘
alias get=’git ‘
alias gut=’git ‘
[/code]

The last three are to correct some common typing errors I make when entering Git commands in a hurry.

What About Those cd Commands?

The cd (change directory) commands were more difficult to work out on my Windows platform than I expected. It took some trial and error and I went through a number of permutations before I thought of having both quotes and leaving out the colon after the drive letter. Here are some actual lines from my .bash_profile file:

[code language=”html”]
alias newspublisher="cd /c/xampp/htdocs/addons/assets/mycomponents/newspublisher"
alias np="cd /c/xampp/htdocs/addons/assets/mycomponents/newspublisher"
alias quickemail="cd /c/xampp/htdocs/addons/assets/mycomponents/quickemail"
alias qe="cd /c/xampp/htdocs/addons/assets/mycomponents/quickemail"
alias cacheclear="cd /c/xampp/htdocs/addons/assets/mycomponents/cacheclear"
alias cc="cd /c/xampp/htdocs/addons/assets/mycomponents/cacheclear"
alias spform="cd /c/xampp/htdocs/addons/assets/mycomponents/spform"
alias spf="cd /c/xampp/htdocs/addons/assets/mycomponents/spform"
[/code]

Notice that there are two entries for every project. One is the short alias, the other is the lowercase name of the project, in case I can’t remember the short alias. In the previous article, I wrote about having the Git Bash window in my code editor (PhpStorm). This wouldn’t be nearly as pleasant without the aliases above to take me instantly to the project I’m working on.

PhpStorm Tip: A little-known feature of PhpStorm allows you to copy the full path of the file you’re working on to the clipboard with Edit -> Copy Path or Ctrl-Shift-C. It’s handy for creating aliases like the ones above and for a lot of other situations as well.

Wrapping Up

You can do a lot with Git Aliases. Here’s one that produces an attractive history of a project:

[code language=”html”]
alias hist=’git log –pretty=format:"%h %ad | %s%d [%an]" –graph –date=short’
[/code]

You can find lots of interesting aliases if you just Google Git Alias. There are some interesting ideas here as well as a nifty section on using auto-completion in Git Bash.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

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