Wednesday, December 12, 2018

Adding shortcut for github commands

Git is no doubt the most popular code versioning tool, out there, in the market. And people often ask about GIT to understand your perspective as a developer.

Today we are going to see some small shortcuts that we can configure in our bash_profile and get the advantage of git shortcuts.

These shortcuts are pretty much self-explanatory and easy to understand.

Open, .bash_profile ( vi ~/.bash_profile) and add the following code at the end of your bash profile, save the file and close.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#########################################################################################################################
                       #######    GITHUB SHORTCUTS     #######
#########################################################################################################################
alias gs='git status'                                                                           # Git Git status
alias glog='git log --pretty=format:"%h - %an, %ar : %s"'                                       # Git 1 liner git status
alias grid='git fetch origin develop:develop && git rebase --interactive develop'               # Git squash changes
alias gfp='git push --force'                                                                    # Git force push
alias gco='git checkout'                                                                        # Git checkout
alias gma='git merge --abort'                                                                   # Abort Git merge
alias gc='git commit -m '                                                                       # Git commit
alias gp='git push'                                                                             # Git push


###########################################################################################################################
              #######    SHOW BRANCH NAME IN COMMAND PROMPT     #######
###########################################################################################################################
parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

Restart the terminal, and you can start using the shortcuts and also if you browse the directory that is a git directory, the terminal will show the name of the branch you are currently in, like this:


Also, there is one more shortcut that seems to be quite useful, to know the parent of a branch, for that you have to add the following code in .gitconfig (vi ~/.gitconfig)  at the end of your bash profile, save the file and close.


1
2
[alias]
        parent = "!git show-branch | grep '*' | grep -v \"$(git rev-parse --abbrev-ref HEAD)\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//' #"

Restart terminal and in any directory with git content, you can call git parent to get the parent information of any branch.


Happy Coding,
:)

No comments:

Post a Comment