开发者

Git Revert and then commit but want to keep revision before revert

开发者 https://www.devze.com 2023-04-01 23:28 出处:网络
My commit is already push to Github, let say A > B > C > D > E -- HEAD I want to revert back to B , so I use

My commit is already push to Github, let say

A > B > C > D > E -- HEAD

I want to revert back to B , so I use

git reset --hard <B:H开发者_如何学Pythonash>

Now, it will look like

A > B -- HEAD

So, if I push it to the server with git push --force, I lost C,D,E in repository

How can I revert the file and commit to be the last revision like A > B > C > D > E > F

F = Reverted: B

I cannot use git commit because it said there are no file to commit.


Try this:

git checkout B
git reset --soft E
git commit -m 'Reverted to B'

The checkout command will change the working tree and index to match commit B. Reset with --soft will change the commit the current branch points to, without affecting the working tree or index. In other words, after the reset command, the index will be in exactly the same state as the B commit.


You could also use git revert B..E, which will do the same thing, except it will create one new commit for each commit reverted. If you want the revert to happen in one commit, just add the -n option (git revert -n B..E) and Git will apply the reversion to the working directory and index. commit the result to finish the reversion.

0

精彩评论

暂无评论...
验证码 换一张
取 消