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.
精彩评论