git-ify your command line!
In a nutshell:
Run bash /dev/null | tail -n1) != “nothing to commit (working directory clean)” ]] && echo “*” }
function parse_git_branch { git branch —no-color 2> /dev/null | sed -e ‘/^[^*]/d’ -e “s/* \(.*\)/[\1$(parse_git_dirty)]/” } export PS1=’\u@\h \[\033[1;33m\]\w\[\033[0m\]$(parse_git_branch)$ ‘
I’m totally addicted to this wicked CLI upgrade, can’t work without it nowadays. Thanks to Henrik Nyh. I’ve seen lots of other variations on the web for this, but I like his’ the best.
Update: You can also use the following snippet to also get a little plus sign (/path/to/git/repo[master+]$) when the local repo is ahead of the remote repo, i.e. you have unpushed commits. Thanks to Paul!
function parse_git_dirty { if [[ $(git status 2> /dev/null | tail -n1) != “nothing to commit (working directory clean)” ]]; then
echo “*”
elif (( $(git status 2> /dev/null | grep ‘Your branch is ahead of’ | wc -l) != 0 )); then
echo “+”
fi
}
2. Common shortcuts (git aliases)
I use the following shortcuts (some were inspired by previous workings with SVN):
/path/to/git/repo[master]$ git st # == git status
/path/to/git/repo[master]$ git br # == git branch
/path/to/git/repo[master]$ git co someBranch # == git checkout someBranch
/path/to/git/repo[master]$ git ci -m”message” # == git commit -m”message”
/path/to/git/repo[master]$ git df # == git diff
To get these aliases up and running just run the following lines in your command line:
git config –-global alias.st status
git config —global alias.br branch
git config —global alias.co checkout
git config —global alias.ci checkin
git config —global alias.df diff
3. Avoiding fast-forward merges
This is just another alias, though I think it’s worth mentioning in detail. In git, when you create a new branch and start working on it, once you decide to merge back to the originating branch, if that originating branch hasn’t diverged yet (i.e. received more commits), git doesn’t really perform a merge. What it actually does is change the original branch’s pointer to point to the last commit on the new branch, resulting in both the original branch and the new branch pointing to the same last commit. The trouble with this feature is that if you want to trace back where you’re branch originated from, the history graph won’t show the intersecting commit where you branched out. So if for some reason you need to revert the work done on this branch you’ll have to manually sift through git log’s historical commit messages and try to remember where you started your branch - a serious headache? Instead, I regularly commit with:
/path/to/git/repo[master]$ git nffmerge someBranch
which is just an alias to git’s regular commit command but with fast-foward flagged off:
git config —add alias.nffmerge ‘merge —no-ff’
An excellent resource to understand this better is Vincent Driessen’s git branching model
4. History graphs (git log)
git log just doesn’t cut it for me. There’s too much clutter on the screen that you can’t really read. I suggest using this simple alias:
git config —global alias.logk “log —graph —pretty=oneline —abbrev-commit —decorate”
# Now use it: /path/to/git/repo[master]$ git logk
Then git logk will generate a nice flat looking log which is easier to read:

But the real bomb (wait for it…), is the colorful history graph that shows relative commit time and commiter names. Don’t just stand there, you have to try this one out:
git config —global alias.log-color “log —graph —full-history —all —color —pretty=format:’%x1b[31m%h%x09%x1b[32m %C(white)- %d%x1b[0m%x20%s %Cgreen(%cr) %C(bold blue)%Creset’ —date=relative”
# Now use it: /path/to/git/repo[master]$ git log-color
And here’s how it looks:

Thanks to Bart’s blog and some random dudes on stackoverflow.com.
5. Whole project diff (git diffall)
Sometimes I’d like to see a diffs of all changed files in my project in a tree view of the folder structure (like Winmerge, Tortoise and other Windows tools). The solution is to combine git-diffall with a GUI diff tool (meld in my case):

To do this, you need to install git-diffall and configure git to use it:
git clone https://github.com/thenigan/git-diffall.git ~/tools/git-diffall sudo ln -s ~/tools/git-diffall/git-diffall /usr/bin/git-diffall git config —global diff.tool “meld” git config —global diff.external “meld”
# Now use it: /path/to/git/repo[master*]$ git diffall
You can also you git-meld which is very similar and also a good tool.
Conclusion
Git is awesome compared to some older source control systems I’ve worked with (CVS, SVN, Clearcase to name a few). The fact that branching and merging is so easy and fast really transforms your work flow. With just a bit of command line hacking, you can make your life a lot easier.
If anyone out there has more git command line productivity tips, do share!
5 Notes/ Hide
-
refaelos likes this
-
refaelos reblogged this from rubyglazed
-
rubyglazed posted this
