git reset while keeping future commits
EDIT: This questions is not "how to revert to a previous commit".
Let's say my master
looks like this:
A B C D
Let's say I want to push commit B
to the origin/master
without resetting it and losing commit C
and D
.
This is what I get with a normal git reset --hard
, but like I said I lose any commits after B
so that's no good:
A B
Instead, I want the commit chain to look like this:
A B C D B
Is this possible? Put it other words, I want to make the local files match commit B
and then committing them, without resetting the chain. The new commit will technically be commit E
but it's files will be identical to commit B
.
Since this will be run by an automated script I don't want to use branches, potentially creating a crow's nest.
I think I might have found a solution to this. In searching for a solution, I found that this can also be considered "how to commit a detached head". Which is generally considered bad and git doesn't want you to do it, because you would be obliterating any other commits by other people.
However, since no one is pulling from this repo and making changes, there won't be any risk of overwriting anyone else's work. The only pulls are automatic from web servers.
So the steps are:
git reset --hard master
get rid of any uncommitted changes that would trip up the script.
git checkout B
this changes the files to the correct commit.
git reset --soft master
this changes the HEAD to master
, but leaves the files like they were in commit B
git checkout master
this solves the "detached HEAD" issue by getting us back to the tip of the tree, but since the previous command already put us back there, no files are changed .
At this point we can add and commit like normal, and git will make the commit patch in essence from commit master
to B
.
I'll have to do some more testing to make sure that this works as I expect it to.
链接地址: http://www.djcxy.com/p/18858.html上一篇: 我如何从GitHub提交一个提交
下一篇: git重置,同时保留未来的提交