0

Sometimes I will checkout a test branch to see if a rebase / whatever works. But this leaves me in a situation where I have some branch feat-234, and some branch test, and I want to replace feat-234 with test.

Example workflow:

# on branch feat-234
git checkout -b test
git rebase main 
# apply fixes required to make rebase work

At this point I want to use test instead of feat-234, as what I wanted to do has worked.

edit 1

I tried to do the following whilst on branch test:

  • git branch -D feat-234, delete feat-234
  • git branch -m feat-234`

I don't think this worked though as I think I've lost all the remote information that was on feat-234.

baxx
  • 3,956
  • 6
  • 37
  • 75
  • You could also `git push origin test:feat-234` to push your local `test` branch over the remote `feat-234` branch – Cory Kramer Dec 06 '22 at 16:13
  • Just `git reset --hard` the feature branch onto the test branch. Make sure the test branch really has the history you want first. – Useless Dec 06 '22 at 16:16
  • @Useless to check, what would the command for that be ? I'm not sure how to reset --hard one branch onto another :S – baxx Dec 06 '22 at 16:21
  • 1
    There’s nothing wrong with your described workflow, but familiarity and confidence with `git reflog` and `git reset` would likely lead to the conclusion that you don’t need these transient branches at all. Once you know that even if things go “wrong” you can recover, there’s no reason to complicate “just do the thing” with that knowledge safety net there to catch you :). – AD7six Dec 06 '22 at 21:35
  • @AD7six I suspected it was a bit of a "smell" :S I should learn what you've mentioned – baxx Dec 07 '22 at 00:23

2 Answers2

2

After you've verified that test is exactly what you want the feature branch to become:

git checkout feat-234
git reset --hard test

and you're done. You'll probably need to push with --force as well, since you're not simply adding commits to the branch.

Useless
  • 64,155
  • 6
  • 88
  • 132
1

I'd like to propose a minor tweak to your workflow, which may be slightly cleaner than your proposed process:

  1. When you wish to test something like a merge or rebase, don't bother using a branch at all until you decide you want to keep it:

git switch --detach # Note you can add a starting commit or branch name if desired

This is equivalent to moving off to another test branch like you did, but you simply haven't named the branch yet, and you possibly never will if you don't like the result. In this way you also don't have to delete any test branches when you're done. Now do whatever you're going to do for your test, in your case perhaps git rebase main.

  1. If you like the result and wish to make your current branch feat-234, simply:

git switch -C feat-234

This creates a new branch called feat-234 like -c would do, but the capital -C replaces the branch if it already exists, which in this case it does. Remote tracking information will be retained as well.

TTT
  • 22,611
  • 8
  • 63
  • 69
  • Thanks, re "_Remote tracking information will be retained as well"_, would this be retained using the approach above (here https://stackoverflow.com/a/74705892) – baxx Jan 04 '23 at 14:43
  • 1
    @baxx yes- resetting a branch pointer doesn't change the tracking information so it is retained there as well. The outcome of my answer is functionally the same as the other answer, but I added it because it's (slightly) cleaner for "messing around" or "testing something", particularly in the case where you may not need a branch. I do this fairly often when testing a merge between two protected branches to see if there are conflicts before creating a PR. If there are conflicts I know I need an in-between branch for the PR, so I simply resolve them and create a branch from my detached commit. – TTT Jan 04 '23 at 15:35