Skip to content

Git tips

Thomas SCHWENDER edited this page Nov 4, 2020 · 12 revisions

Git tips

Adding a config variable directly in the git command line

To add a config variable directly in the git command line you can use option -c:

bashrc
git -c http.sslVerify=false clone https://domain.com/path/to/git

Customizing the Git prompt

It is possible to add useful information to your Git prompt, through the edition of %USERPROFILE%\.bashrc (in Windows).
Everything is explained in the excellent doc from Delicious Insights, Un prompt Git qui déchire.

Note
Be Careful!

Even if generally the sames, paths targeted by Windows %USERPROFILE% environment variable and Unix $HOME var (defined in your Git Bash) can be different. This is especially true when working in enterprise.
Yet, your .bashrc file must be stored in the $HOME path defined in Git Bash, NOT in the Windows %USERPROFILE% folder.

Here is the one I generally use:

# Git-friendly prompt
# See https://delicious-insights.com/fr/articles/prompt-git-qui-dechire/

# GIT_PS1_SHOWDIRTYSTATE : signaler les modifs à la copie locale (symbole *) et à l'index (le stage, symbole +).
export GIT_PS1_SHOWDIRTYSTATE=1

# GIT_PS1_SHOWSTASHSTATE : signaler la présence d'entrées dans le stash (symbole $)
export GIT_PS1_SHOWSTASHSTATE=1

# GIT_PS1_SHOWUNTRACKEDFILES : signaler la présence de fichiers ni versionnés ni ignorés (donc untracked) dans la copie locale (symbole %).
export GIT_PS1_SHOWUNTRACKEDFILES=1

# GIT_PS1_SHOWUPSTREAM : indiquer le rapport entre la branche locale et sa version trackée (en retard <, en avance >, synchro = ou ayant divergé <>).
# Les affichages peuvent être modifiés selon la valeur de la variable ; les symboles décrits ici sont pour la valeur auto, mais on en a d'autres, notamment verbose. Voyez votre script de prompt pour les détails.
export GIT_PS1_SHOWUPSTREAM=verbose

# GIT_PS1_DESCRIBE_STYLE peut prendre diverses valeurs pour représenter un detached HEAD ; par défaut (default), on a l'abbreviated SHA mais on a d'autres options, la plus utile étant probablement branch.
export GIT_PS1_DESCRIBE_STYLE=branch

Here is an example of rendering:

userToto@yourHost MINGW64 /d/repos-git/big-data-2017-overview (master *% u=)
$

Handling special characters in URL

When using a login and password in the URL for, by example, a git clone, be careful with special characters!
Some of them, as the @ are not supported and must be replaced by their equivalent in percent encoding.
Example: @ can be replaced by its equivalent %40, which gives:

# Not good!
git clone https://user:p@ssword@github.com/user/repo.git
# let's use %40 to replace the @
git clone https://user:p%40ssword@github.com/user/repo.git

The same applies for http.proxy and https.proxy of git config

Updating the PATH in Git Bash

To effectively update the PATH in Git Bash, you need to update it twice (strange, but is working workaround)
By example, if updating the PATH this way:

export PATH=/c/tools/JRuby-1.7.19/bin:$PATH

Then, even if the echo $PATH is correct, the new value doesn’t seem to be taken into account.
To force it, you have to update some other environment variables (is it the 2nd call to the export command that solves the issue? I don’t know…​)

Useful Git aliases

  • full git log, well formated, with branches displayed as a graph:

    git config --global alias.lg1 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"
    git config --global alias.lg2 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"

    Check this Stackoverflow post for more details.

Tip

To enable more powerful aliases, that accept parameters, you can use the following declaration (paste it in your .gitconfig, in alias section):

lgp = "!f() { \
                git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' $@; \
                }; f"

This example allows you to limit your beautiful custom git log command to a single branch: git lgp my_branch

Using a credential helper

When performing remote commands (clone, fetch, etc.) on a Git repo using HTTPS, you can use a credential helper to tell Git to remember the username / password of the remote system (like GitHub). For Windows, this can be done with the following command:

# to add the Windows credential helper in the Git global configuration
git config --global credential.helper wincred

Your credentials can then be managed through the Windows Credential Manager (accessible through the Control Panel).

Clone this wiki locally