gitpython modules GitCommandError and Repo import error

You have installed gitpython using pip install gitypython in Mac.

And now you try importing the modules (from git.exc import GitCommandError or from git import Repo) that work with git repository, but then you run into the below error

>>> from git.exc import GitCommandError
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/lib/python2.7/site-packages/git/__init__.py”, line 33, in
_init_externals()
File “/usr/local/lib/python2.7/site-packages/git/__init__.py”, line 27, in _init_externals
raise ImportError(“‘gitdb’ could not be found in your PYTHONPATH”)
ImportError: ‘gitdb’ could not be found in your PYTHONPATH

If you have installed python using ‘brew install python’ or ‘brew install python@2’, then it install pip for you, as well as when you did gitpython, it would have added the gitdb. It’s seems that the folder /usr/local/sbin wasn’t in my PATH ( link ), so after I added it to my .bash_profile (PATH=$PATH:/usr/local/sbin) and ran ‘source bash_profile’, I was able to import the above modules.

Hope this helps.

ebates promo.png

Git issues after Mac Mojave OS upgrade

ebates promo.png

If you are running into Git issues / Git not being found, in Terminal or IntelliJIDEA, or any other IDEA….. see below for the solution.

Run the below commands to get the git version fixed, after Mac Mojave OS upgrade.

#1: sudo xcode-select –switch /Applications/Xcode.app
#2: sudo xcode-select –switch /Library/Developer/CommandLineTools

If you run into the error: xcode-select: error: invalid developer directory ‘/Library/Developer/CommandLineTools’
while running command #2, you would have to run the command #3, and finish the install prompt (with xcode, or just the commnadlinetools), and then run command #2 again

#3: sudo xcode-select –install

Also, note that you’ll have to run these command with ‘sudo’ previleges.

Git Config and Git Alias

Where to find git config file in Mac:

In Mac, you can usually find the Git config file in your home directory, in hidden file .gitconfig

eg., /Users/.gitconfig

How to Add alias(aliases) to Git config file:

You can add Aliases for GIT commands (a single command or a series of commands) for your convenience (easy to remember or fewer keyboard strokes)

Couple ways to add alias to the config file:

    1. Add using git config command: eg., here is to add alias to find the Last commit to the branch you are on.
      git config --global alias.last 'log -1 HEAD'
    2. Add the entry manually in the .gitconfig file itself. The same example can be written in the config file. Just add the below lines to the end of the config file if there is no [alias] tag in the file already, or if there is a [alias] tab already, add the alias as key value pair.


[alias]
last = log -1 HEAD

If you want the list of files that are committed, add the flag ‘–stat’.


[alias]
last = log --stat -1 HEAD

 

And the command you use to execute the alias is much like how you use other git commands.

git last

Also if you want to add an alias which takes a parameter, here is how to add git alias with a parameter.

Lets say we want to create a branch, and want to use a shorter command (‘cb’) instead of checkout.

you give an alias ‘cb’ with the actual command, wrapped in a function that takes a parameter.

[alias]
cb = "!f() { git checkout -b $1;}; f"

And you can execute ‘git cb {branch_name_you_want_to_create}‘ to create the git branch.

Feel free to provide comments/suggestions.