Git change branch, but don't change files in workspace
This is similar to my another question ( Switch to another branch without changing the workspace files ) but solution that worked there, doesn't work now.
I needed to remove some changes, which were long time ago pushed to remote master. So I don't want to remove commits from master, but I want to change the files just like these changes were reverted. So I did this:
git branch limits
git checkout limits
git rebase --interactive <commit before the ones I wanted to remove>
So now in limits
I have the code like I'd like it in master. How can I "move" it to master? With the code from limits
I'd like to change to master
branch, but without changing any file in workspace, so I can then commit the changes as a new change to master
.
Rather than use interactive rebase, you can just revert each commit that you don't want. When you use git revert <object-name-of-commit>
, git will introduce a new commit that backs-out the change introduced by the one you name. So, suppose that the commits you want to remove are abc123
and def456
, you can just do:
git checkout master
git revert abc123
git revert def456
However, if it was a lot of work to find those commits, and you're happy with the tip of limits
, you can just create a new commit on master
that contains the state of the tree from there. Firstly, make sure that git status
is clean, since you're going to use git reset --hard
, and that will wipe out uncommited changes:
git checkout master
git reset --hard limits
git reset --soft HEAD@{1}
git commit -m "Reverting unwanted commits"
That recipe is a variation of one in this question:
The answer to your stated question is:
$ git checkout master # switch to master branch
$ git reset --hard limits # hard-reset it to the limits commit
$ git reset --soft master@{1} # move the reference back to where it was, but
# don't modify the working tree or index
This will leave your working tree and index exactly as it would be if you had limits
checked out, but you will be on the master
branch at its original location.
However, the proper way to do something like this would be to git revert
each change that you are trying to undo.
This asks for revert
:-)
I would go back to master and git-revert
each of the commits you want to unapply. This creates a "unapplying" commit for each - so no history changes, but you get the effect you wanted.
上一篇: 使一个分支等于另一个分支
下一篇: Git更改分支,但不要更改工作区中的文件