Git reuse branch or delete and create again

I recently got the task to add a certain feature to a project I’m working on. As this feature depends on old code that was cruel to use, I decided to split the task in 2 steps:

  • refactor old code to be more usable
  • create feature using refactored code

I created a branch feat/foo, and after the refactoring was done, I merged it into our master so we could use the changes directly. Now I’m left with the following commit history:

  • git move uncommited changes from master to new branch
  • Switching between multiple ssh keys in Git on Windows
  • Git how to rebase a (personal) branch that I have already pushed?
  • “Hiding” things in GIT
  • Pull changes Github to Github
  • git pre-commit hook to format and re-add files at the same time
  • A ---> B ---> C --> E ---> F <master  B: created branch feat/foo
           |            ^                 D: refactoring finished
           D -----------|                 C: changes in master in between
           ^                              E: merge commit
         feat/foo                         F: master is now here

    feat/foo still points to D, and my master advanced to be at commit F. What would I do now to continue my work on this task in branch feat/foo? I see two possibilities:

    • either delete feat/foo and checkout -b it again, so I have a new branch that has the same name as my old branch,
    • or somehow “reuse” feat/foo, which I don’t know how to do

    The first solution somehow feels not quite right to me, it seems “wrong” to delete the branch just to create it again. But I don’t know how I could reuse it.

    What should I do? Delete and recreate the branch, or if the right answer would be reusing it, how?

  • Unexpected underscore in git log --graph output
  • Git commit from python
  • git remote url automatically resets
  • using Git and pushing to production mode (w/ diagram)
  • Should I add web libraries as git submodules for a project?
  • aborting git pre-commit hook when var_dump present
  • 2 Solutions collect form web for “Git reuse branch or delete and create again”

    In many workflows, once a feature branch has been merged back into master it is deleted. GitHub is probably the prime example of this. If you follow this school of thought, you would delete feat/foo and create a new feature branch for your next sprint.

    If you really want to keep using the branch, then you will either have to rebase feat/foo on master or merge master into feat/foo. I don’t see any advantage to rebasing, which could be messy, so let’s consider merging. You merged feat/foo into master at commit E. Therefore master already has all the features from feat/foo, but the reverse is not true, i.e. feat/foo is likely missing several features which have been introduced into master since commit D. To do the merge you would use this command:

    git checkout feat/foo
    git merge master

    You may have to resolve merge conflicts arising from new features in master which are not yet in the feat/foo branch.

    Now the feat/foo branch is up to date with master, and you can keep using it if you wish. Personally, I would just leave feat/foo where it is and create an entirely new feature branch. You can keep it around for a few sprints until you are sure that deleting it safe.

    Since you need to continue working on branch feat/foo, the first thing to do is to checkout it:

    git checkout feat/foo

    Because you didn’t complete the work on the new feature you keep working and committing on the branch until you finish the work and are ready to merge it back into master.

    From time to time it’s good to pull into the branch the latest changes from the master branch. You do that by running:

    git merge master

    while you are on the feat/foo branch.

    It doesn’t make much sense to remove the branch just to re-create it again. git checkout feat/foo followed by git merge master produces the same result.

    Git Baby is a git and github fan, let's start git clone.