torsdag den 26. november 2015

Git Rebase Commands

The goal of this post is to make it easier for me to reference the individual steps of rebasing a Git repository.

I usually push the branch to origin as soon as I have made the first commit for backup purposes. Then I do an interactive rebase when the feature is complete to clean up the branch.

First update develop:


git checkout develop
git pull -u
git checkout feature/branch


Perform the interactive rebase:

git rebase -i develop


Push the feature branch to origin using force. Don't do this if somebody else is working on the same branch.

git push --force


If you don't want to do the rebase interactively, then just rebase:
git rebase develop


git rebase develop

If your current branch contain commits already merged into develop. Or is the continuation of a previous branch merged with develop after the current branch has been created. Then you must apply a little more magic.
Get the hash of the commit immediately before the commits you want to keep. The run the following command with #hash replaced by the hash of the commit.

git rebase --onto master #hash

The changes must be force-pushed.

See more here: https://www.atlassian.com/git/tutorials/merging-vs-rebasing/workflow-walkthrough.

mandag den 16. november 2015

How to list all databases with a given prefix in PostgeSQL

I recently had to delete a large number of databases in a PostgreSql database.

I found the code below very useful. It will list a sequence of SQL DROP DATABASE statements, one for each of the databases with a name starting with "database_name_starting_with_this".

The list of statements can be copied to psql a executed by pasting.

I want to warn you this can be dangerous, so go through the list of statements one by one, before you execute them.


SELECT 'DROP DATABASE "' || datname || '";' FROM pg_database
WHERE datname LIKE 'database_name_starting_with_this%';