Skip to content

Reorganizing branches is a pain. #1

@CardenB

Description

@CardenB

Here is a list of aliases I had to create in order to make branch rearranging doable:

  # Prints the name of the currently checked out branch.
  curbranch = "!f() { \
      git rev-parse --abbrev-ref HEAD | grep -v ^HEAD$ || git rev-parse HEAD; \
    }; f"
  # Prints the name of the upstream branch.
  parent = "!f() { \
    git name-rev --name-only $(git rev-parse `git curbranch`@{upstream}); \
  }; f"
  # Prints the name of the branch upstream to the specified branch.
  parent-of = "!f() { \
    git name-rev --name-only $(git rev-parse $1@{upstream}); \
  }; f"
  # Sets the upstream of the currently checked out branch to the specified
  # branch.
  sup = branch --set-upstream-to
  # Sets the upstream of the specified branch to the currently checked out
  # branch. This is the inverse of `git sup`.
  # NOTE: This fails if the branch matches the parent branch.
  adopt = "!f() { \
      CUR_BRANCH=`git curbranch`; \
      SELECTED_BRANCH=$1; \
      SELECTED_PARENT=`git parent-of $1`; \
      git branch $1 --set-upstream-to $CUR_BRANCH; \
      git rebase -i --onto $CUR_BRANCH $SELECTED_PARENT $SELECTED_BRANCH; \
      git branch $SELECTED_BRANCH --set-upstream-to $CUR_BRANCH; \
    }; f"
  # Does an interactive rebase only for the commits between the parent branch
  # and the currently checked out branch. Then sets the upstream of the
  # currently checked out branch to the specified branch. After this, the
  # currently checked out branch will the new parent's commits plus the commits
  # unique to this branch. It will no longer have the old parent's commits
  # (unless the old commits are also in the new parent's branch).
  transfer-to = "!f() { \
    CUR_BRANCH=`git curbranch`; \
    ORIGINAL_PARENT=`git parent`; \
    NEW_PARENT=$1; \
    git rebase -i --onto $NEW_PARENT $ORIGINAL_PARENT $CUR_BRANCH; \
    git branch $CUR_BRANCH --set-upstream-to $NEW_PARENT; \
  }; f"
  # Creates a parallel copy of the branch instead of one tracking the currently
  # checked out branch.
  copy = "!f() { \
    NEW_BRANCH_NAME=$1; \
    PARENT_OF_PARENT=$(git parent-of $(git parent)); \
    git checkout -b $NEW_BRANCH_NAME; \
    git sup $PARENT_OF_PARENT; \
  }; f"

These seem to do a decent job, but need first class support in gitflow, especially to handle failure cases (i.e. transfer-to fails, so the operation needs to be aborted and the old upstream should be restored).

Open to suggestions for the gitflow interface for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions