An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
Most useful aliases for git
Hello everyone! In this article, I'll share with you some useful aliases for git that I use every day.
Working with branches
For each feature I work on, I create a new branch, this alias saves me a lot of time:
gco
This is an alias for
git checkout
Usage
gco branch-name // switch to branch an existing branchgco -b branch-name // create a new branch and switch to itgco . // discard all changes in the working directory
Commits
Every day I create a lot of commits, each time type git commit
is boring, so I use these aliases:
gcmsg
This is an alias for
git commit -m
Usage
gcmsg 'feat: commit text' // create new commit
gca
This is an alias for
git commit --amend
gcfix
This is an alias for
git commit --fixup
Usage
gcfix commit_hash // create a fixup commit for commit with provided hash
Rebasing
When you work on a large project, rebasing is part of your everyday routine, so this alias will save you some time of typing
gpr
This is an alias for
git pull --rebase origin dev // remote and branch may be different in your case
Also often I need to rename, remove some commits or squash fixups in my local branch, for this I use this alias:
gria
git rebase -i --autosquash
Usage
gria HEAD~4 // interactively change last 4 commits
Pushing
After I've created some commits I need to push them, for that I use next:
ggp
git push origin $(current_branch) // instead of origin you can use your own remote
What is $(current_branch)
? It's an function that returns name of current branch, It looks like that:
function current_branch() { ref=$(git symbolic-ref HEAD 2> /dev/null) || return echo ${ref#refs/heads/}}
How to keep aliases
If you just type in terminal command
alias gs="git status"
it will work, but when you restart the terminal it will not.
For fix that I keep all my aliases in file ~/.bash_aliases
and import them in my ~/.bash_profile
like that:
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliasesfi
My ~/.bash_aliases
looks like that:
function current_branch() { ref=$(git symbolic-ref HEAD 2> /dev/null) || return echo ${ref#refs/heads/}}alias ~="cd ~";alias ..="cd .."alias g="git"alias gs="git status"alias gc="git commit"alias gcmsg="git commit -m"alias gcfix="git commit --fixup"alias gria="git rebase -i --autosquash"alias gca="git commit --amend"alias gco="git checkout"alias gb="git branch"alias gpr="git pull --rebase origin dev"alias gp="git pull"alias ggp="git push origin $(current_branch)"
Thank you for reading
I hope you've enjoyed this article! Please click on the "Follow" button to see my future articles.
I'll glad to see any feedback!
Original Link: https://dev.to/paulcodes/most-useful-aliases-for-git-35e7
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To