0

In order to create a clean commit history and get rid of some files I don't want for a PR, I would like to set my working directory to be at the state of my branch, say B, but make git remain on branch A which is identical with main, so I can now stage whatever I want to be part of the commit.

In other words: I want to be on main, but have the working directory be like the working directory of another branch A.

I know I can git rebase and edit, but that involves a lot of steps. I simply want to change the files in the working directory and nothing else.

I've found a hacky way but is there something simpler than:

git branch -c A B
git reset --soft main
git restore --staged .
Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
  • 1
    I'm pretty sure both your question and your answer have some typos in it. (What is `git branch -b` and `git create -b` ?) And, it sounds like you'd benefit from [reading this question](https://stackoverflow.com/q/3528245/184546), and in particular, [this answer](https://stackoverflow.com/a/59675191/184546) where Regret Type 2 is exactly what you're asking. – TTT Jul 14 '23 at 20:28
  • Woops, yes, I must have meant `git branch -c` all the time. Thanks for the amazing answer with regret types: https://stackoverflow.com/a/59675191/7483211 – Cornelius Roemer Jul 15 '23 at 01:15

1 Answers1

0

Using git checkout COMMIT . instead of git reseset appears to be slightly more intuitive to me:

git branch -c A main
git checkout B .
git restore --staged .

(from https://stackoverflow.com/a/49572040/7483211)

But using git reset --mixed is actually the most succinct I think:

git branch -c A B
git reset --mixed main

as --soft puts changes in staging, while --mixed makes changes unstaged.

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55