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 .
2
  • 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, and in particular, this answer where Regret Type 2 is exactly what you're asking. Commented Jul 14, 2023 at 20:28
  • Woops, yes, I must have meant git branch -c all the time. Thanks for the amazing answer with regret types: stackoverflow.com/a/59675191/7483211 Commented Jul 15, 2023 at 1:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.