Ricky
Ricky

Reputation: 2907

How to rollback one particular commit in Git?

Here's my situation:

  1. About 10 commits ago, I accidentally did a bad commit. I was switching between UNIX/Windows, and long story short, I did a commit where EVERY file was changed, due to the line endings.

  2. I had done about 10 commits after that.

  3. I now realize that in order to PUSH to the remote repository, I need to undo the commit from step (1) (there's a setting in .gitattributes which I've modified for this to normalize).

How do I go back to that point in time and not commit every file? And then I need to re-commit the other 9 changes after that?

Upvotes: 0

Views: 171

Answers (1)

user456814
user456814

Reputation:

If you haven't pushed your 10 commits yet, or your remote repo is private and not shared with other people, then you can try using an interactive rebase:

$ git rebase -i head~10 # Go back 10 commits in time.

In the TODO list of commits, choose edit for the one at the top of the list:

edit d6627aa Commit message 1
pick 0efdaca Commit message 2
pick 9ec752f Commit message 3
pick c84bf57 Commit message 4
pick d4bcd50 Commit message 5
pick c0110c9 Commit message 6
pick 6606d13 Commit message 7
pick 22933be Commit message 8
pick cab2453 Commit message 9
pick 05add41 Commit message 10

Then amend your last commit to change the line endings to be however you want:

# Modify your .gitattributes, then amend last commit
$ git commit --amend
$ git rebase --continue

To learn more about rebasing, see the Rewriting History section of the free online Pro Git book, as well as the official Linux Kernel Git documentation for rebase.

Upvotes: 4

Related Questions