Tag Archives: bash

Interactive git rebase with non-interactive editing

When working with git and especially GitHub, I often have commits on my local branch that were already submitted as a pull request. Sometimes I continue working and later notice that I have commits on the branch that have nothing to do with the next thing I am already working on. Therefore I want to remove them from the current branch.

$ git log --oneline @{upstream}..
e159d1e Commit C
70140e3 Commit B
16ed14a Commit A

Let’s say I want to get rid of Commit B. The normal way would be to use git rebase -i and then tell git to remove the commit by deleting the corresponding line or replacing pick with drop, then save the file, exit the editor and let git do the rest. However, if I already know exactly what I want to do, this seems cumbersome. Therefore I came up with the following alias:

$ git config --global alias.drop '!f() { for c in "$@"; do sha1=$(git rev-parse --short $1); [ -n "$sha1" ] && git -c rebase.abbreviateCommands=false -c sequence.editor="printf \"%s\n\" \"g/^pick $sha1 /s//drop $sha1 /\" w \"g/^#/d\" \"g/^$/d\" \"%p\" q | ed -s" rebase -i --autostash; done }; f'

I admit that maybe at some point I should have changed my plan and I should have made this into a shell script instead… But this works and requires no extra files than just ~/.gitconfig.

What does it do? This alias is not a normal alias, but as indicated by the ! at the start it is supposed to be executed in a shell. As git will append all additional arguments from the command line to the end of this shell command, I turned it into a shell function to be able to handle the arguments one after another in a loop. First, the given argument is validated as a reference to a commit and the matching sha1 is retrieved. This means it can also understand HEAD~3 and similar references. If it is not valid, git will already have printed an error message. If it is valid, this runs git rebase with some special options.

The configuration option rebase.abbreviateCommands ensures that git will show us the long command name pick and not just p in the editor. But wait, there is also sequence.editor, which will be used instead of your normal $EDITOR. This is a custom command that will get a temporary filename as an argument. As the file needs to be edited in place, I chose ed for this task. Other options like sed or awk might work as well, but as far as I know there is no standardized interface for in-place editing and I want this to work with all flavors of Linux or *BSD.

ed takes a filename as argument and then reads line-wise commands from standard input that are applied to the file. I wrapped this with a printf to avoid using lots of \n in one long string or using many echo commands instead. The first command is g/re/... which will search for a line matching the given regex and execute the commands following the second slash. As a side note, the name of the tool grep comes exactly from this ed command: g/re/p, where the p stands for print. The command executed here is s/re/replacement/ which will replace the first occurrence of the regex with the given replacement. The empty string denotes the previous match. This combination of two commands is necessary to mask errors from ed. If a s// does not replace anything, ed would return an error and exit with a non-zero status code. This behavior is masked by combining it with g//, which will only try to replace on lines that already matched.

The command w will write the file to disk. Then I also wanted to get some output of the results to quickly see what’s going on. The next two g// commands will delete comments and empty lines before %p will print the whole file to standard output. Finally, the last command q tells ed to quit.

git will then take the modified file just as if it was edited in your $EDITOR and continue with the rebase. The --autosquash option is useful to automatically stash any uncommitted changes before running the rebase and then restoring them afterwards.

Now let’s finally get to removing Commit B from the example above:

$ git drop 70140e3
pick 16ed14a Commit A
drop 70140e3 Commit B
pick e159d1e Commit C
Successfully rebased and updated refs/heads/master.

Here it goes! This was an interactive rebase, but with non-interactive editing of the sequence file using ed. Setting up a similar alias for git reword to quickly edit the commit message is left as an exercise for the reader.

tmux update-environment

tmux is one of the tools I use everyday. But one thing always annoyed me: even though I am using X11 forwarding and ssh-agent forwarding when re-attaching to a session, the DISPLAY and SSH_AUTH_SOCK environment variables are often wrong. Environment variables are initialized only once when the window was created. tmux is able to update some environment variables for new windows and panes based on the update-environment setting, however, existing shell windows cannot be updated.

Continue reading

bash: reuse last argument from previous command

Reuse the last argument of the previous command with !$:

$ echo abc def
abc def
$ echo !$

A common use case would be mkdir and cd:

$ mkdir foo
$ cd !$

You can also insert the last argument of the previous command and continue typing with <ESC>.:

$ echo abc def
abc def
$ echo <ESC>. ghi
def ghi

Oh, the little things… 🙂

bash: for-loop with glob patterns

It is common to use a for-loop with glob patterns:

for i in *.txt; do
    mv $i $i.old

But if the glob pattern does not match anything it will be preserved unchanged in the command. This results in command execution of mv *.txt *.txt.old which fails because no file named *.txt (literally!) exists.

As this is not the desired behavior, here is a way how to do this as expected without forking using the nullglob bash shell option.

oldnullglob=$(shopt -p nullglob)
shopt -s nullglob

for i in *.txt; do
    mv $i $i.old

eval "$oldnullglob" 2>/dev/null
unset oldnullglob

This will silently prevent the execution of the mv command. If you use failglob instead of nullglob bash will interrupt the evaluation of any command if the glob pattern did not match anything.

Disclaimer: Be careful with this option, as this will not be the expected behavior in all cases. Most (in)famously it breaks bash-completion if you set it in your interactive bash session. I suggest to use it temporary only.

Hiding MOTD on bash startup

Usually bash displays /etc/motd at the time of last login on opening a new shell. I do not find that information very useful, as I am opening new shells a lot, so the time does not really mean anything for me. Also, the MOTD rarely changes.

There is a feature in bash to hide these messages by creating a file ~/.hushlogin. This will make bash jump right to the first prompt without any output before.

But just in case the MOTD changes and includes important messages, I enhanced this setup a little bit. Instead of just touching the .hushlogin file, I am storing the old MOTD in it. At startup the old and current MOTD is compared against each other and will be displayed only if it differs. To avoid accidentally missing the MOTD, bash will also ask for my confirmation that I have read it.

Here is the snippet from my ~/.bashrc:

# Show motd only if necessary
cmp -s $HOME/.hushlogin /etc/motd
if [ $? != 0 ]; then
    echo -e "\n==> !!! IMPORTANT: /etc/motd changed !!! <==\n"
    cat /etc/motd
    echo -e "\n==> !!! IMPORTANT: /etc/motd changed !!! <==\n"
    read -e -n 1 -p "Show again? (Y/n) " ans
    if [ "$ans" == "n" ]; then
        cat /etc/motd > $HOME/.hushlogin

Please note: If you are going to use this, make sure it will not get executed on non-interactive shells. Otherwise it can break tools like scp, sftp or rsync. To ensure this will not be used on non-interactive shells, I am using this conditional at the top of my ~/.bashrc:

if [[ $- != *i* ]] ; then
    # Shell is non-interactive.  Be done now!