My Git tips for a better life in your console

The first tool to discover is:

Hub by GitHub

What is hub? You can get at hub.github.com and see all the features.

One of the most used feature (at least for me) is the checkout from a pull request:

git checkout https://github.com/github/hub/pull/134

In this way is very easy to do tests and also get updates from a pr, that is like a branch.

Git-Fork alias

Basically I download a lot of repo from my profile but maybe I don’t have a copy locally, so I have also to add another remote that is the original.

So I can automate it?

function git-fork() {
    url=$1
    url=${url%/}
    url=$(echo $url | sed 's/.git//g' | sed 's/git\@//g' | sed 's/github.com\://g')
    echo "$url download in progress"
    git clone git@github.com:$url.git &> /dev/null
    user=$(echo "$url" | awk -F/ '{print $1}')
    repo=$(echo "$url" | awk -F/ '{print $NF}')
    cd $repo
    remote=$(curl -s "https://api.github.com/repos/$user/$repo" | jq -r '.parent.clone_url' | tail -c +20)
    if [ "$remote" != "" ]; then
        echo "$remote download in progress"
        git remote add upstream "git@github.com:$remote" &> /dev/null
        git fetch --all &> /dev/null
    fi
}

Ref: https://github.com/Mte90/dotfiles/blob/master/.bashrc#L147

This bash alias do that for you automatically 😀

# For Git
alias git-commit-rename='git commit --amend'
alias git-remove-last-commit='git reset --soft HEAD~1'
#  To remember the SSH password for 36000 minutes
alias git-pass='ssh-add -t 36000'
alias gpm="git push origin master"
alias git-restage="git update-index --again"
alias git-rename-branch="git rename-branch"
alias git-remove-deleted-branch-remotely="git remote prune origin"
#  Add and remove new/deleted files from git index automatically
alias git-remove-file-not-exist-anymore-history="git ls-files -d -m -o -z --exclude-standard | xargs -0 git update-index --add --remove"
function git-merge-last-commits() { git reset --soft HEAD~$1 && git commit; }
function commit() { commit=$(kdialog --title 'Commit message' --inputbox 'Insert the commit' '') && git commit -m "$commit" && echo "$commit"; }
function git-stat-months() { git diff --shortstat "@{$1 month ago}"; }

The command that I use everyday is git-pass that automatically remember the login for me during the work.

The rest of the commands are quite clear to understand but very useful.

In conclusion for this section is my gitconfig https://github.com/Mte90/dotfiles/blob/master/.gitconfig

Improve the Bash prompt

Customize the bash prompt to improve the UI and understand better what is the branch and the path.

I am using this https://github.com/petobens/trueline and is very important, because saved my life quite often.

Other my articles

Leave a Reply

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

My Git tips for a better life in your console

time to read: 2 min
0