Skip to content

Instantly share code, notes, and snippets.

@mkcor
Last active April 9, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkcor/5733a25575bf0ca61d4947d9837bb43f to your computer and use it in GitHub Desktop.
Save mkcor/5733a25575bf0ca61d4947d9837bb43f to your computer and use it in GitHub Desktop.
A random collection of tips and lessons learnt.

How I upped my game with Git

I started running

$ git status

all the time and reading the output carefully.

But I could save some time after I added the following to my ~/.bashrc:

# Prepend current git branch in terminal
function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1 | awk '{print $1}') != "nothing" ]] && 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:\w\[\033[0;33m\]$(parse_git_branch)\[\033[0;m\]$ '

I found out (was told) that the equivalent of $ hg pull was actually $ git fetch. Yes, I learnt Mercurial first.

I taught the Software Carpentry Version Control with Git lesson, and never did $ git commit -am again.

I let go of my command-line-only dogma and started using $ git gui to add hunks to the staging area. Now they are gone from the $ git diff output and you can find them with $ git diff --staged.

I fell in love with $ git log -G<regex>: Look for differences whose patch text contains added/removed lines that match <regex>. Then I would typically run git show <commit_id> to see the details.

I adopted $ git stash for test-driven development.

I ran across the well-named revert command but, admittedly, it might feel like you are adding noise to your revision history. So I went to the dark side...

I rebased interactively, e.g.,

$ git rebase -i HEAD~3

Likewise, you may want to

$ git rebase master

when you have been working on that feature branch for so long (ship it!!) and other stuff has been merged into master in the meantime.

Caution! Rebasing involves history rewriting (for the cleaner) so make sure you know what you are doing if you work with collaborators, if you have already pushed your work to a remote, etc.

@mkcor
Copy link
Author

mkcor commented May 20, 2016

Some thoughts and thanks go to @gdevenyi, @scjody, @charettes ! 🐤

@jni
Copy link

jni commented Mar 21, 2018

@mkcor these are very nice! =) The top one is 👌

@btel
Copy link

btel commented Mar 22, 2018

Very nice!! Thank you! The git gui command does not work on my system (Ubuntu). Which GUI tool does it lunch?

@Debilski
Copy link

@btel did you install git-gui? Personally, I prefer gitg on Linux.

@jni @mkcor Not sure if I state the obvious here but many git installations come with a file /usr/lib/git-core/git-sh-prompt (Debian) /usr/local/Cellar/git/2.16.2/etc/bash_completion.d/git-prompt.sh (macOS homebrew). If you source that file in the .bashrc, it gives you similar functions, especially one called __git_ps1 that you can use as

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

or

PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'

customisation can be done by setting the environment variables GIT_PS1_SHOWCOLORHINTS or GIT_PS1_SHOWDIRTYSTATE (and a few more).

(I just copied verbatim form the doc. I am using a powerline spin-off myself so I don’t have a personal customisation myself for this one. ;) )

@mkcor
Copy link
Author

mkcor commented Apr 9, 2018

@jni @btel @Debilski Thanks for the comments!

@btel Yes, the Ubuntu package that will let you run git gui is git-gui.

@Debilski No, you are not stating the obvious, I wasn't aware of the /usr/lib/git-core/git-sh-prompt file. I'm glad to find out there's a more standard way to customize your prompt! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment