DEV Community

Stephen Chiang
Stephen Chiang

Posted on • Edited on

🌮 Git 'r done when you're done.

Once again I'm waiting for my app to run its tests and build so I thought I'd write another quickie.

Previously I showed how I wrote a git function for emergencies and I need to GTFO.

Today, I want to show you a more common process we all might do if we are working by ourselves or on small-teams.

When I'm done with a feature branch this is usually the steps I go through:

(Assuming I'm at the top level of my directory and I've done git push origin head -u in the past)

> {feature-branch} git add .
> {feature-branch} git commit -m "Awesome commit msg"
> {feature-branch} git push
> {feature-branch} git checkout develop
> {develop} git merge -
> {develop} git push
Enter fullscreen mode Exit fullscreen mode

If I want to clean up the merged branch locally and remotely, I would also have to do this:

> {develop} git branch -d feature-branch
> {develop} git push origin :feature-branch
Enter fullscreen mode Exit fullscreen mode

That's a lot of steps.

Here's how I make it easier with git done and git donep, I actually have an alias setup for git as g so I'll be using that from now on:

I'm going to go into my .gitconfig and create two new aliases like so:

[alias]
  done = ""
  donep = ""
Enter fullscreen mode Exit fullscreen mode

Using the same syntax as my g tfo function I'll create done first which does the following:

  1. Merge the branch
  2. Deletes the local branch
  3. Deletes the remote branch

That function looks like this:

done = "!f() { git merge $1 && git branch -d $1 && git push origin :$1; }; f"
Enter fullscreen mode Exit fullscreen mode

You will notice the $1. That is a reference to a variable that will be passed in when calling the function which happens to be the branch name. Now, I have tried using - and @{-1} but deleting the remote branch part of the function won't accept those as references to the last branch used. So unless a reader here has an idea, I am relegated to passing in the feature branch name specifically, which in a way makes it more reusable too, but a bit of a pain if the branch name is complex or hard to remember. But at least, you still only have to type it once!

🤷‍♂️So how do we use it?

> {develop} g done feature-branch
Enter fullscreen mode Exit fullscreen mode

🤯 Boom. That's it.

But with this one, we haven't pushed the changes to develop remote. That's where donep comes in.

donep = "!f() { git merge $1 && git branch -d $1 && git push origin :$1 && git push; }; f"
Enter fullscreen mode Exit fullscreen mode
> {develop} g donep feature-branch
Enter fullscreen mode Exit fullscreen mode

note: this function also assumes you have done git push origin head -u and set the upstream. if you haven't and don't want to, you can modify the function to:

donep = "!f() { git merge $1 && git branch -d $1 && git push origin :$1 && git push origin $2; }; f"
Enter fullscreen mode Exit fullscreen mode

and run it like:

> {develop} g donep feature-branch develop
Enter fullscreen mode Exit fullscreen mode

🐱‍👤 Git r' done!

If you find this valuable, please leave a comment and follow me on Dev.to @chiangs and Twitter @chiangse, 🍻 cheers!

Top comments (0)