79205072

Date: 2024-11-19 21:21:28
Score: 1
Natty:
Report link

is there any better way to do ?

I believe there is - it is mixed reset.

Let's imagine your head is on commit_B. The commands to execute are the following:

  1. If commit_A is not the first commit in your repo, you need to reset
    your head to state preceding to it: git reset [--mixed] HEAD~~

    Now all the changes you made within the last two commits are in working tree, but not in stage area.

  2. Stage fileB: git add fileB and commit git commit -m 'fileB'

  3. Stage the rest: git add . and commit git commit -m 'A,C,D files'

  4. If your old commits are already on remote repo, you have to rewrite history there too:
    git push --force

Voila, it should work.
Usually git allows to do many things in different ways, so often it is just a matter of preference or how good you own the stuff (like in git checkout vs git switch).
The same is for rebase/merge, rebase/reset, et cetera.

For example, git rebase is often used for squashing commits, but it does not preserve dates (the result will have the date of the oldest commit), but git reset --soft doesn't have such problem.

Reasons:
  • Blacklisted phrase (1): is there any
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): is there any
  • Low reputation (0.5):
Posted by: Ryan