Git Tricks
Couple of weeks ago I saw this tweet from Mike:
I had similar problem couple times when dealing with Git. So, I created the following git command:
git undo
It is far easier to remember ?
Git is powerful, but its build in commands can be confusing. Pretty much everything is possible but sometimes is so complicated it might as well be impossible. Because of this, I have a lot of helpers for Git.
In Git config files you can create aliases. Part of my .gitrc are the following aliases:
[alias]
lol = log --graph --decorate --pretty='format:%C(yellow)%h%Cgreen%d%Creset %s %C(white) (%an, %ar)%Creset' --abbrev-commit
undo = reset --soft HEAD^
amend = commit --amend
git lol
is just a pretty git logsgit undo
removes the current commits but leave the changesgit amend
would put your current changes into the previous commit
The other way you can extend Git is to create custom git commands by creating scripts starting with git-command-name
and put them in your load path.
Then you can call git command
.
I have a couple of those.
I create Pull Requests all the time because I work with feature branches. Because of this, I have git pull-request
which pushes the current branch to GitHub and opens the new pull request screen.
Here is the source of the script:
#!/bin/bash
current_branch=`git rev-parse --abbrev-ref HEAD`
remote_url=`git remote get-url origin | awk '{gsub(/[email protected]:/,"")}1' | awk '{gsub(/\.git/,"")}1'`
git push origin $current_branch --set-upstream
open https://github.com/$remote_url/compare/$current_branch?expand=1
While working on features, the master is changing. Keeping your feature branch up to date with the master is essential. So I have git rebase-on-master
Here is the source of the script:
#!/bin/bash
current_branch=`git rev-parse --abbrev-ref HEAD`
git checkout master
git pull origin master --rebase
git rebase master $current_branch
After I'm done with a branch, I have to remove it. So I have git delete-merged
. It removes all merged branches compared to your current branch. It is useful when you pull a master after you have merged a feature branch
Here is the source of the script:
#!/bin/bash
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
Those are some of the most significant productivity boosters, I have to deal with Git. I have couple more tricks you can find in my config files.