简体   繁体   中英

How to do hg revert --all with git?

When using Mercurial it is possible to issue the following command: hg revert --all --rev <revision> The content of the working directory is set to the specified <revision> but you stay where you are in the tree (same parent, branch, etc).

How to do that in git?
It should behave like git reset --hard <commit> without moving the branch pointer.

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

A comparison/test of all variants i found can be found here: http://git.io/vk9it

git read-tree -um @ $thatcommit

will do it. That's "transition the index and worktree from the HEAD aka @ commit to $thatcommit, as for checkout (but without touching HEAD)".

When what you're doing isn't a good match for any of the convenience commands, the core commands have your back :-)

If you need to blow away uncommitted changes, git reset --hard possibly with some selection of git clean options to clear out completely untracked files first, git really hates stomping on uncommitted work without an explicit order.

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

git reset --hard

to get rid of them (of course, if you want to keep them, commit them first). This step can be skipped if there are no staged files and no local changes.

Second, checkout the revision that you want to go back to:

git checkout <rev>

Your working directory now contains the correct files, but points to the wrong commit. In order to go back to the original revision, do:

git reset --mixed <original_rev>

The original revision will typically be the head of the branch you're on, so you can just use the branch name for the original revision. The --mixed is optional, because it's the default for git --reset ; it'll move the files out of the index, where git checkout put them and move the commit to <original_rev> .


An easier way is to checkout the files of the revision directly and then use git reset --mixed to forget the index. Ie

git checkout <rev> .
git reset --mixed

Note the dot ( . ) above. It specifies the current directory (and recursively all content below it).

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

Only updates the index, to force it to the working directory to issue: git checkout-index -fa && git clean -dxf

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

In git your HEAD can point to a single commit. You cant have at the same time multiple HEADS (unless you use git new-workdir and then you have n copies of the repository)

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

Once you have several branches you can have different HEAD per branch.

You don't have to use reset you can simply branch out at any given point:

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

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


In git when you do a git revert you remain on the current branch but your parent is the commit prior to the current commit (the history is moving to the given point in the tree)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM