How do I hg revert - all using git?

When using Mercurial, you can run the following command: The hg revert --all --rev <revision>

contents of the working directory are set to the specified one <revision>

, but you remain where you are in the tree (same parent, branch, etc.).

How do I do this in git?
It should behave as git reset --hard <commit>

without moving the branch pointer.

TL; DR git config alias.revert-all 'read-tree -um HEAD' git revert-all <commit>

Comparison / test of all found variants can be found here: http://git.io/vk9it

+3


source to share


4 answers


git read-tree -um @ $thatcommit

      

will do it. This is "moving the index and worktree from HEAD, and @commit to $ thiscommit as for checking (but not touching HEAD)".



When what you are doing doesn't fit any of the comfortable commands, the main commands have your back :-)

If you need to blow away uncommitted changes, git reset --hard

perhaps with some choice of options git clean

to clean up completely unrepeatable files first, git really hates stomping on uncommitted work without an explicit order.

+1


source


This requires several steps. First, if you have any files or any unwanted local changes, use

git reset --hard

      

to get rid of them (of course, if you want to keep them, do them first). You can skip this step if there are no staged files and no local changes.

Second, check the version you want to revert to:

git checkout <rev>

      

Your working directory now contains the correct files, but indicates a bad commit. To revert to the original version, follow these steps:



git reset --mixed <original_rev>

      

The original revision is usually the head of the branch you are on, so you can just use the branch name for the original version. --mixed

is optional as it is the default for git --reset

; it will move the files from the index where you git checkout

put them and move the commit to <original_rev>

.


An easier way is to check the version files directly and then use git reset --mixed

to forget the index. I.e.

git checkout <rev> .
git reset --mixed

      

Note the dot ( .

) above. It indicates the current directory (and recursively all the content below it).

0


source


For completeness, another (slower) alternative: git diff --cached --full-index --binary <commit> | git apply --reverse --cached

Updates only the index to make it work in the working directory: git checkout-index -fa && git clean -dxf

0


source


The contents of the working directory are set to the specified, but you stay where you are in the tree (same parent, branch, etc.).

In git, yours HEAD

can point to one commit. You cannot have multiple HEADS at the same time (unless you use git new-workdir

then you have n copies of the repository)

The only thing that might be similar to this (but again not quite what you want) is to work with branches (same as in hg).

Once you have multiple branches, you can have a different HEAD for each branch.

You don't need to use reset, you can just branch

exit at any time:

git checkout -b <branch_name> <SHA-1>

and you will have a new branch pointing to the parent you want.


In git, when you execute git revert

, you stay on the current branch, but your parent is the commit before the current commit (history moves to that point in the tree).

-1


source







All Articles