chop – efficiently dispense with current git branch
by Laust Rud Jacobsen on January 18, 2011
My git workflow includes the hack and ship commands for easy tracking of a shared master branch, and conveniently delivering commits. Feature branches are cheap and fast in git, and I am often spawning new branches to try stuff out or work on unrelated things.
Now, meet chop – for chopping down the current working branch after it has been shipped and is no longer needed. The script changes the current branch to master, and then deletes the branch you was previously on. If you give a branch-name as an argument that will be the new current branch.
#!/bin/sh -xset -o errexitCURRENT_BRANCH=$(git branch | grep '\*')git checkout ${1:-"master"} || exit 1git branch -d ${CURRENT_BRANCH:2}I use this small script is multiple times every day, and I really like the name of it. There is not a whole lot of functionlity, but as this is an often repeated action, it makes sense to automate it.
Enjoy!
7 comments
[...] This post was mentioned on Twitter by HN Firehose. HN Firehose said: Chop — efficiently dispense with the current git branch: http://bit.ly/eF2iza [...]
by Tweets that mention chop – efficiently dispense with current git branch « object.io -- Topsy.com on January 18, 2011 at 12:39. #
This is nifty, even if it only saves a few keystrokes.
But it might make sense to check that you’re not deleting the master branch? AFAICT, this can happen if you run chop.sh while on the master branch (with the feature branch as argument).
by Jacob Poulsgaard Tjørnholm on January 19, 2011 at 12:13. #
It is fortunately not a problem, as git refuses to delete the current branch:
git branch -d master
error: Cannot delete the branch 'master' which you are currently on.
This is at least true for version 1.7.3.5.
by Laust Rud on January 19, 2011 at 12:37. #
True, but it can happen if you provide an argument to the script:
[ barbar - ~/tmp/foo ] $ git init
Initialized empty Git repository in /Users/jtj/tmp/foo/.git/
[ barbar - ~/tmp/foo ] $ touch FOO
[ barbar - ~/tmp/foo ] $ git add FOO
[ barbar - ~/tmp/foo ] $ git ci -m “initial”
[master (root-commit) cc8379f] foo
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 FOO
[ barbar - ~/tmp/foo ] $ git b foo_branch
[ barbar - ~/tmp/foo ] $ chop.sh foo_branch
++ git branch
++ grep ‘\*’
+ CURRENT_BRANCH=’* master’
+ git checkout foo_branch
Switched to branch ‘foo_branch’
+ git branch -d master
Deleted branch master (was cc8379f).
by Jacob Poulsgaard Tjørnholm on January 19, 2011 at 17:17. #
Ah, you are absolutely right.
However, there’s nothing special about the
masterbranch, and you still have thefoo_branchpointing to the same (or newer) revision asmasterbranch poointed to, no harm is done, and no history is lost. Simply create a newmasterbranch.I have the current branch name visible in my prompt, so I have never hit this issue. But thank you for reproducing the corner-case, cleared it up for me :)
by Laust Rud on January 19, 2011 at 17:47. #
Right, no data would be lost. Just needed to nitpick a bit ;-)
Great to see some Git tips on your blog, keep up the good work! Now that you mention the bash prompt, maybe you could share some tips in this area some day?
by Jacob Poulsgaard Tjørnholm on January 19, 2011 at 20:34. #
I just might do that
by Laust Rud on January 19, 2011 at 22:15. #