Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git revert --no-commit without staging

Tags:

git

git-revert

Usually the command git revert automatically creates some commits with commit log messages stating which commits were reverted.

To avoid automatic commit there's the option -n (or --no-commit).

But after this command, the reverted files are in the staged area. I can unstage them by using the command git reset HEAD.

Is there a direct way to revert a commit without committing and staging?

In other words: is there a direct command to revert a commit applying the changes only to the working directory, without touching the index?

like image 453
lornova Avatar asked Nov 12 '15 14:11

lornova


People also ask

How do you revert a commit without commit?

If you wish to add more changes before committing reverted changes, simply add --no-commit or -n to the revert message. This will prevent git revert from automatically creating a new commit; instead, the revert will appear in the staging index without a commit message.

Can we commit without staging in git?

If you run git commit right now, though, the index / staging-area still has the original git checkout versions, not the modified work-tree versions. So you have to git add the changed files to copy them into the index. This is kind of a hassle, so Git has git add -u and also git commit -a .

How do I force a git commit revert?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .


1 Answers

There is no single command for it. As you already noted, you can combine git revert -n with git reset to get the reversion undone in the index.

Besides that method, you can use git apply -R to "reverse apply" a patch, and you can turn a commit into a patch with git show, so:

$ git show <rev> | git apply -R 

has the same effect, with one important (but subtle) difference. Let me quote from the git revert documentation and add my own emphasis:

-n, --no-commit
Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

In other words, git revert -n operates in, and on, the index, with a side effect of updating your work tree. The git apply command operates (by default anyway) on the work-tree only, and in fact can be used outside a git repository.

like image 78
torek Avatar answered Oct 06 '22 12:10

torek