Git to change a branch, but do not change files in the workspace

This is similar to my other question ( Switch to another branch without changing workspace files ), but the solution that worked there now does not work.

I needed to remove some changes that had long been transferred to the remote wizard. Therefore, I do not want to delete commits with master, but I want to change the files in the same way that these changes were canceled. So I did this:

  • While on the main, git branch limits
  • git checkout limits
  • git rebase --interactive <commit before the ones I wanted to remove>
  • in the interactive console. I deleted commits with the changes I wanted to revert.

So, now in limits I have code, as I would like in master. How can I โ€œmoveโ€ it to the master? With the code from limits I would like to switch to the master branch, but without changing any file in the workspace, so I can commit the changes as a new change to master .

+4
source share
4 answers

Instead of using online forwarding, you can simply discard every commit you don't want. When you use git revert <object-name-of-commit> , git will introduce a new commit, which will undo the change introduced by the name you named. So, suppose the commits you want to remove are abc123 and def456 , you can simply do:

 git checkout master git revert abc123 git revert def456 

However, if there was a lot of work to find these commits, and you are happy with the end of limits , you can simply create a new commit on master that contains the state of the tree from there. First, make sure git status clean, since you are going to use git reset --hard and this will destroy the undefined changes:

 git checkout master git reset --hard limits git reset --soft HEAD@ {1} git commit -m "Reverting unwanted commits" 

This recipe is a variation of one in this matter:

+5
source

The answer to your question:

 $ 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 if you checked limits , but you will be in the master branch in your original location.


However, the correct way to do something like this would be to git revert every change you are trying to undo.

+5
source

This requests revert

I will return to the wizard and git-revert each of the commits that you want to delete. This creates an โ€œunacceptable" commit for everyone, so the change history does not change, but you get the desired effect.

+1
source

Travel pointer

If what you are actually trying to do is move the branch pointer for the master after a complex reboot or merge in a separate branch, you can do this in various ways. For example, to forcibly move some intermediate branch so that it becomes your new main branch, you can do one of the following:

 # Use git porcelain to move the branch over top of one that already # exists. git branch -M limits master # Explicitly move the branch pointer to wherever the head for limits # is pointing. git reset --hard refs/heads/limits 

There are also ways in which you can execute various plumbing commands, but this does not mean that this is an exhaustive treatise. That should be enough for you to point in the right direction.

Undo commit

As other posts and comments have been correctly noted, if all you want to do is to cancel targeted commits from the wizard, you can use git -revert (1). Please note that this leaves both initial commits and reverses in your story; this is usually what you want.

Also note that git-revert can cause conflicts or unwanted changes that you will have to manually resolve, especially if the commits you return are small, atomic changes. Think of git-revert as the reverse patch (which it is mostly under the hood), and you can see where potential conflicts might arise.

Your mileage will certainly vary.

0
source

All Articles