Git in terminal - Improve your git workflow using aliases and functions
This will be the first one in a series of posts about improving git
in the terminal. If you prefer to use the tools inside your editor or a visual tool such as SourceTree, go for it! use whatever is better for you, but you could learn some useful things using git from the terminal.
Note: It's expected from the reader to have a basic git
knowledge.
Index
These are the topics we'll discuss in each post:
- Improve your git workflow using aliases and functions
- Adding
hub
to the formula - Introduction to Fzf, custom scripts and similar tools
- Lazygit
Improve your git workflow using aliases and functions
When using git
in the terminal, our first contact begins by just typing git
in the terminal and hitting Enter. By doing this, we get a list of the possible commands to use, such as clone
, add
, among others.
First, we'll list some common commands used everyday and then we'll check how we can access faster to them.
status
: Shows the status of the current branch.add
: Add files and changes for the next commit.commit
: Creates a commit with the current changes.branch
: Used to list and manage branches.checkout
: Used to change branches.push
: Push the changes to a remote git server.pull
: Pull the changes from a remote git server.log
: Show info about commits.
Personally, these are the commands I use the most, but here is the thing: typing these each time is cumbersome. Let's see how we can use alias.
Alias structure
IMPORTANT: These commands can only be used on Unix based operating systems (such as Linux and Mac OS X).
An alias is an abbreviation for a specific command. You can check what aliases you have defined by typing:
alias
This is an example of the output:
Z='| fzf'
c=clear
cat=bat
d=docker
dc='docker container'
dco=docker-compose
di='docker image'
dn='docker network'
ds='docker service'
dv='docker volume'
You can also see one specific alias by adding the alias name
alias c
This will show the output:
c=clear
You can add an alias by modifying your .bash_profile
, .bashrc
or .zshrc
(depends of your command line shell) with this format:
alias <alias>="<command>"
Example:
alias g="git"
Note: Remember to source again your configuration by running source <path_to_config_file>
to see the changes.
After adding the alias, you can just make the call in the shell and get the same result.
Another cool trick that helps you if you forget any alias is to use grep
, like this:
alias | grep git
This will display all the lines that contain git
.
Aliases
Now, you can create your own git aliases to be more efficient. These are the git commands I personally use:
alias git='g'
alias gaa='g add --all'
alias gb='g branch'
alias gst='g status'
alias gco='g checkout'
alias gc='g commit'
alias gcam='g commit -a -m'
alias glog='g log'
Functions
Now, in the aliases we're missing the commands push
and pull
. Normally, to call this commands, you need to specify the branch, but we can use some functions to get the current branch, like this:
function git_current_branch() {
local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?
if [[ $ret != 0 ]]; then
[[ $ret == 128 ]] && return # no git repo.
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
fi
echo ${ref#refs/heads/}
}
function current_branch() {
git_current_branch
}
function ggl() {
if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
git pull origin "${*}"
else
[[ "$#" == 0 ]] && local b="$(git_current_branch)"
git pull origin "${b:=$1}"
fi
}
compdef _git ggl=git-checkout
function ggp() {
if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
git push origin "${*}"
else
[[ "$#" == 0 ]] && local b="$(git_current_branch)"
git push origin "${b:=$1}"
fi
}
compdef _git ggp=git-checkout
Don't worry to much about what is going on here. The important part is that by defining these functions, you can use ggl
and ggp
to pull
and push
the current branch.
All this is too cumbersome too...
Agree. Actually, all that I'm showing you here is integrated in Oh My Zsh (the code for ggl
and ggp
was taken from here). I just wanted to show that you can implement and expand the use of these techniques to improve your workflow. Don't just install, understand what happens under the hood!
Workflow example
Now, let's see a small example showing how to use this approach:
g clone some-random-repo.git # Use `g` instead of `git`
gst # git status to see the current branch
gco -b my-new-branch # -b to create the branch if doesn't exist
touch foo.md bar.md baz.md # Add some files
gst # git status again to see the files
gaa # git add -all
gc -m 'My commit message' # commit changes
ggp # push branch
gco - # small trick. Use "-" to go to the previous branch
As you can see, having control of these aliases can make your git experience in the terminal very comfortable.
Extra trick! Copy current branch
When you need to share the current branch, normally you need to call git branch
or git status
and copy manually. Here is an extra alias that can be very helpful.
alias gbr="git rev-parse --abbrev-ref HEAD | tr -d '\n' | pbcopy"
This command will copy the current branch in your clipboard, so you can go and share it easily.
Note: pbcopy
is only available on OSX, so probably you need to pipe the result to another command if you're using linux
.
Conclusion
I hope these tips help you to use better this great tool. In the next article we'll see how hub
can give us some extra tools for our daily job.