Undoing the Last Git Commit

By Andres Jaimes

- 1 minutes read - 154 words

A command so important, it deserves its own page:

git reset --soft HEAD~1

The previous command undoes the last commit to git. I think this is one of those commands that I have typed many times during the last year. It’s very important to know which branch we currently are on, but sometimes excitement (or stress) may make us forget checking it.

Related to this, I have a custom prompt that shows a random smiley and the current git branch to the prompt. If using bash, you can add the following lines to ~/.bash_profile. Restart you terminal or reload changes by using source .bash_profile.

HAPPY=(🙂 🙃 🤓 😎)

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

emoticon() {
  echo ${HAPPY[$RANDOM % ${#HAPPY[@]}]}
}

export PS1="\$(emoticon) \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

alias ls='ls -F'

This prompt looks like this:

🙂 some-directory (master) $

For non-git directory it hides the branch part.