git

Git bash: Clear screen and show diff

I just added another little convenience command for Git Bash. This is for clearing the screen and showing the diff (changes) in your branch. I added support for a parameter so you can use it either to show all differences, show the changes for a specific file, or append some other parameter to it (see the examples below).

Here`s the code itself – append it to your .bash_profile file, then restart your git bash:

cdiff(){
    clear
    if [ "$1" ]; then
        git diff "${1}"
    else
        git diff
    fi
}

Examples of usage:

cdiff # Will clear screen and show all changes not added

cdiff --cached # Will clear screen and show changes in added files

cdiff my-file.xyz # Wil clear and show changes for the specified file

cdiff --name-only # Will clear, then list all changed files (not added)

A use case for bash functions with git on Windows

When you open a Git Bash shell, it will typically open under your home directory, as indicated by a yellow tilda (~) at the end of the command line.

Side-note tip if you’re new to unix type shells:
~ refers back to your home directory – e.g. C:\users\YourUserName\ under Windows, so entering cd ~/SomeDirectory from any location will always change your current dir back to c:\users\SomeDirectory.

For my part, I’ve got most of my projects and source code under a directory called “source“, and all my git-projects below that again in a directory called repos. This means that each time I open the git shell to do any work, the first thing I need to do is change directory using: cd source/repos/TargetRepo where TargetRepo would be the name of a given project.

This is not a big deal, but it would be better if I could simplify the process of moving to whichever project I wanted to. I could do just that thanks to this simple solution found on Stack Overflow (new window). I’m expanding slightly on it here so I know I’ll find it if I need it again, and because I’m convinced it can be useful to others too.

Continue reading

GIT alias: List all branches

Using GIT? Quick tip of the day: Add the line below to the  [alias] section of your .gitconfig file (you should find it in your home directory). This will let you quickly list all the branches available for a repo.

brl = branch --list --all

Save the file, and try the following command in a GIT window:

git brl

You should see a list of all available branches for the repo, both remote and local, with an asterisk marking the branch you currently have checked out:

A Git alias for logs, Version 2.0

A while back, I wrote a post about how to improve the log command in git by adding a customized alias for it. I’ve been fairly happy with that alias, but I kept missing one thing: A date, or some similar indicator of the age of each commit. Today, I finally set aside time to improve it, and here`s the result:

lg = log --format=\"%h: %Cgreen%an %ar %Creset(%ci) %n         %s\" -10

Continue reading