How to revert two commits back and commit only good stuff?

git revert multiple commits
git revert commit
how to revert multiple pushed commits
git reset
git reset head~1
git undo revert
git revert back multiple commits
git revert range of commits

I commited something wrong twice. How to revert two commits back and commit only good stuff ?

You can do a git rebase -i HEAD~2 and then use the interface to discard the "bad commits" that are there since then and clean up your history. This however alters the project history and if you've already pushed (and others pulled) your changes, there are some social issues to work out.

The other option is to git revert those changes. Then 2 new commits will get added to the history that makes the fact that you don't want these two commits explicit in the project history. Less clean but easier to work with.

How to revert multiple git commits?, Undo changes in a repository with the simplicity and elegance of Git commands. the chain, one way to get back to the version with only two lines is to reset to that commit, A similar thing happens in most other operations we do in Git when commits are replaced. 6 best practices for managing Git repos. git revert --no-commit HEAD~2.. is a slightly more idiomatic way to do it. If you're on master branch, no need to specify master again. The --no-commit option lets git try to revert all the commits at once, instead of littering the history with multiple revert commit messages (assuming that's what you want). – kubi Jan 15 '13 at 18:49

First, git reset HEAD~2 to discard the top two commits, while leaving your working tree exactly as it is. Then, simply create a new commit with what you want (e.g., with git adds and then git commit).

See Reset Demystified by Scott Chacon for the details on the often confusing git reset comamnd.

How to reset, revert, and return to previous states in Git , So you have not yet committed, the question is now whether you want to undo Using git add -p to add/commit only some changes to make multiple commits is left as This is normally a good thing and it will eventually go away by itself, but if for Obviously replace "REF" with the reference or SHA you want to get back to . The system asks you to enter a specific commit message for the changes the revert command is going to perform. This action creates a new commit based on the one you specified, with a revert tag. This acts as a log, showing that the commit was published and then reverted (instead of pretending it never happened).

Revert changes in a new Commit

If you want to completely undo the commits and delete them, use rebase or reset like other answers suggest.

But sometimes, you can't modify git history (for example after pushing to a protected branch on remote). In that case, your best bet is git revert:

git revert --no-commit HEAD~1^..HEAD

This will revert all changes in the last two commits (from HEAD to HEAD~1). You can make sure everything looks good by checking the status or diff and commit the revert:

git status # or git diff
git commit -m "Reverted the last two commits"

On undoing, fixing, or removing commits in git, Frequently Asked Questions � Tips & Tricks � Undoing Things � Keyboard Shortcuts Undoing Commits. When you want to undo a commit, you have two basic options: reset and revert. Note: Reset can only be performed on your HEAD branch. Select the "Keep Changes" option if you want all changes from rolled back� A simple git revert that commits will, when shown, show the same change but backwards: delete this one line from file A, and add this one line to file B. If what you meant—and will commit in this third commit—is to just add the one line to file A, now you have a correct single commit which, when shown, will say add this one line to file A.

Undoing Commits, back. Posted almost 10 years ago. Visible to the public. Linked content. To revert changes that are split across multiple commits, use the --no-commit flag. The other day I wanted to commit my changes into several separate commits however I made a mistake and pushed all of them to GitHub. I therefore wanted to revert back to a previous commit, override…

Git: Revert one or more commits, You can revert a commit with git revert but its going to add more commit Once in a while late at night when I ran out of coffee, I commit stuff that I Worked great . to do a " git rebase -i HEAD~2 " (i.e. interactive rebase of the last two commits) . There you just delete the bad8d5e commit line and save. git will then rewrite� Two of my favorite source code control commands for working with previous commits are git cherry-pick and git revert. The git cherry-pick command is great when you need to pull in a bug fix from another active branch. And the ability to revert a Git commit is great way to back out of a bad contribution to the code base.

How can I roll back 1 commit?, Git commit only saves it to the stage, which is locally on your computer. Use git revert <ID> to revert back to a previous commit. each commit has an 2. wrong answer. this only reverts this specific commit. – Jeff P Chacko Mar 4 at 13:37 reset will alter history -- if I made several commits and then reset to the first commit,� Note that there are quite a few other ways to use this command, like if you want to revert back 2 commits, you can use: $ git revert HEAD~2 Or if you want to revert many non-continuous commits you specify them individually: $ git revert 676ec 735c5 Temporarily Checkout a Previous Commit. By "reverting a commit", you might mean that you temporarily want to go back to a previous state in your repo, but without making any actual changes to the tree.

Comments
  • What is the good stuff? Could you describe in more detail how does your history look now and how do you want it to look?
  • a little more detail would help if getting the correct answer (good stuff?)
  • Delete all the bad stuff before you commit everything else.
  • Do you mean just to your local repo or have you pushed aleady?
  • My coworkers will complain that the bad commit was ever there to begin with. Better to get rid of it before trying to merge the branch. Thanks for providing the git rebase -i answer and not just saying "you don't really want to do that!" like many people do!
  • Another useful command in this situation is git push --force, in case you've already pushed the bad commit.
  • The --force option is destructive. If someone else has made changes on top your commits and pushed them to the repository, your force push will destroy all of them.
  • That's why I only use it on my own branches, where that's not an issue (but the bad commits I've pushed are an issue). It's especially useful when I make a PR and then the PM says "break all the commits you made to module FOO into their own PR and merge them before the rest of the commits that are interleaved with them."
  • I would only do this is the commit are local only, if they are public I would use revert on the last 2 commits
  • Yes, I was assuming the OP had not pushed the incorrect commits anywhere.