3

I have a git project where several commits were made and I want to revert most of them. However, within this time, there were several bug fixes and other commits that I'd like to keep so I want to do the following:

  1. Revert repository to the commit it was on prior to the first commit that I want to revert
  2. Go through commits and reapply only some of them, and some of them only partially

Is there some sequence of git commands that will enable me to achieve this? I could figure out how to revert to certain commit, but I'm clueless if selective re-commiting is even possible.

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • doing a `git rebase --interactive ` lets you rewrite your history. you can remove, reorder, and change what happened in each commit. it'll drop you into VIM by default, just fyi – worc Jul 06 '18 at 16:20
  • take a look at this answer and the others for a similar question: https://stackoverflow.com/a/180085/769780, if that helps i think we can mark this as a duplicate? – worc Jul 06 '18 at 16:26
  • 1
    Note that all the commits have already been pushed and I want to change more than a just the commit message. I don't think that answer covers for that. – Karlovsky120 Jul 06 '18 at 16:27
  • if the commits have already been pushed, you can still overwrite remote history if that remote allows it with a `git push -f `. though you have to be careful that your changes haven't been "published," or taken in by other remotes or users. then you'll really be creating a mess. – worc Jul 06 '18 at 16:29
  • yeah the answers on that question aren't great. what you want to do is pretty clearly a `git rebase -i` to me, and you'll want to know how to use the pick, reword, squash, and fixup keywords when in the interactive menu. and also, it's important to note you can move commit lines around in that view. i'm not seeing a clear example of that on that question's page, if i can't find anything clear, i'll write something up – worc Jul 06 '18 at 16:31
  • I would be perfectly satisfied without modifying history, but simply applying several commits that will get it to the state I want. I could do some kind of undo commit that would take it back to point in time that I want and then, one by one, apply commits that I want to keep, I just don't know how to reapply commits from history... – Karlovsky120 Jul 06 '18 at 16:32
  • Thank you. I don't even need a complete answer, pointers to the commands I can research myself saves me a lot of time and I have no problem figuring the thing out myself, as long as I know I'm going in the right direction – Karlovsky120 Jul 06 '18 at 16:34

1 Answers1

3

You don't have to revert. You can use git rebase in interactive mode to edit your history. For example, on my current topic branch, if I run git rebase -i master I get this view in VIM:

pick 23299aa6 improve unit test messages
pick cf1c3c0c move sagas tests to where they'll get run
pick f7ff7fa7 use docker-compose up to rebuild the ui image more quickly
pick 544902ec split definitions file into helpers and given, when, then files
pick 55e94e7b remove canary test
pick 8a4b0862 move expectTitleToBe function to helper file

# Rebase 72b74f26..8a4b0862 onto 72b74f26 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

From this view I can remove lines I don't want to commit, if I didn't actually want to remove the canary test I could delete or comment out that line or use the drop command. I can stop the rebase at a commit and change what will be committed with edit (you could commit partially here by removing changes) and so on for the other commands listed:

edit 23299aa6 improve unit test messages
pick cf1c3c0c move sagas tests to where they'll get run
pick f7ff7fa7 use docker-compose up to rebuild the ui image more quickly
pick 544902ec split definitions file into helpers and given, when, then files
drop 55e94e7b remove canary test
pick 8a4b0862 move expectTitleToBe function to helper file

# Rebase 72b74f26..8a4b0862 onto 72b74f26 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

For your situation and rebasing against yourself and not another branch, you can look back in the git log to find the commit hash of the commit you want to revert to. This is usually going to be one commit before the work that you started: git rebase -i <where to "revert">.

worc
  • 3,654
  • 3
  • 31
  • 35
  • I'm a bit scared to go into this without a backup. Is it possible create a new branch, have it "copy" last N commits into it and then work there? Once I'm satisfied with the result, I can merge it and possibly remove part of the history if it bothers me so much. Also, I get "noop" line when i run `git rebase -i master`. All the commits that I want to revert were already published, and were not published by me. Sorry if I'm omitting important bits, I don't have much experience with git and know only the basics... – Karlovsky120 Jul 06 '18 at 16:48
  • yeah, if you're currently on this branch and do `git checkout -b ` you'll create a new branch that is essentially a copy – worc Jul 06 '18 at 16:50