最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - Reseting single file and fixup to another commit - Stack Overflow

programmeradmin0浏览0评论

The situation is the following :

commit_A : contains fileA and fileB
commit_B : contains fileC and fileD

I want to move the fileA from the commit_A to the commit_B.

What I am doing most of the time is a interactive rebase, I mark commit_A as e (for edit), I then reset the commit, do a commit with fileA and redo the commit with only file B, then finish the rebase and finally I fixup the new commit with only fileA to commit_B. My question is, is there any better way to do ? Because it doesn't seem optimal, Thanks

The situation is the following :

commit_A : contains fileA and fileB
commit_B : contains fileC and fileD

I want to move the fileA from the commit_A to the commit_B.

What I am doing most of the time is a interactive rebase, I mark commit_A as e (for edit), I then reset the commit, do a commit with fileA and redo the commit with only file B, then finish the rebase and finally I fixup the new commit with only fileA to commit_B. My question is, is there any better way to do ? Because it doesn't seem optimal, Thanks

Share asked Nov 19, 2024 at 18:55 Lovis XIILovis XII 576 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

If you are curious about what git reset does, then git-reset-demystified shines in revealing some git concepts and terminology.

发布评论

评论列表(0)

  1. 暂无评论