Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Revert Your Mistaken Git Commits
Michael D. Callaghan
Michael D. Callaghan

Posted on • Edited on • Originally published atwalkingriver.com

     

Revert Your Mistaken Git Commits

GitHub logo Recently one of my development teams had a merge problem. For version control, they use a modified form ofGitFlow, and it was time to merge from thedevelop branch to therelease branch. If things are done correctly, this should always be a clean, simple merge. It is especially true in this case, because it was their first release for a new project. The team opened a pull request fromdevelop torelease and then reported to me that there GitHub was reporting merge conflicts. Huh? How is that possible? Therelease branch should be empty. Except that it wasn't.

As it turned out, a well-meaning developer on the team knew that the code eventually needed to go intorelease, so that's he committed all of his changes. Locking down branches is a topic for another day, I suppose. I had to deal with the problem in front of me first.

I ran the following command on therelease branch.

git log--oneline
Enter fullscreen modeExit fullscreen mode

Its output showed something like this:

1   852291a   blah blah blah2   f575c87   blah blah3   83d855d   blah blah4   9fa11df   blah blah blah blah5   111b003   blah blah6   2b3a530   blah blah blah7   a4c5f54   blah blah blah blah blah blah8   b2a62fa   blah blah blah blah blah blah blah blah blah9   5fb67b9   blah blah blah10  4d1a5fc   blah blah blah11  ed40aec   Initial commit
Enter fullscreen modeExit fullscreen mode

No, we really don't use “blah blah” as commit messages. As I said, these commits were made directly to therelease branch, and not through a merge or pull request fromdevelop, which is the prescribed method. I don't hold any grudges against the well-intended developer. Both git and this workflow are new to that team, and I mostly blame myself for not at least setting up a 30-minute discussion ahead of time. They weren't new to source control, just this particular process.

Regardless of my feelings or the good intentions of the team, I was faced with a problem I had to solve. A new pull request fromdevelop torelease was waiting. The test team was waiting for a release build, and that wasn't happening. I decided that the best and safest thing to do was to undo each of those commits.

So after a bit of Google- and Bing-foo, I landed on some questions and answers at StackOverflow.com that enabled me to find just the right sequence of git commands to fix therelease branch. I'm recording the experience here so that I know where to look the next time this happens, and in the hopes it may help someone else.

The first thing I had to do was figure out how to revert a series of commits. There were only 11, so I could have done them one at a time, but what if there had been 111 instead? I wanted to understand the process regardless of the number of commits. After reviewing the various commands that are available, I settled on the following syntax:

git revert--no-edit <oldest-commit-hash>..<newest-commit-hash>
Enter fullscreen modeExit fullscreen mode

This actually took a few tries, because most of the documentation said things like “first bad commit” and “last bad commit.” Is “first” the oldest, or is it #1 in the log? Turns out, it's the oldest, which is the highest number in the commit log.

So, to undo all of those commits in the above list, I used the following command:

git revert--no-edit ed40aec..852291a
Enter fullscreen modeExit fullscreen mode

The--no-edit flag kept git from prompting me to edit the commit message for each of the 11 reverse commits it made. Instead, it used the original commit message, preceded by the word “Revert.” Fine by me. After that command completed, each of my commits had a mirrored revert, in reverse order. Checking the directory showed that the only thing present was the initial README.md file. Perfect! So the only thing left to do was push the branch back up to the server:

git push origin release
Enter fullscreen modeExit fullscreen mode

Back on the server, I checked the open pull request fromdevelop torelease, and there were no merge conflicts. I completed the pull request, and the build was able to continue.

Now the testers can get on with their work, and we can all live happily ever after — until the next problem, of course.


Cross-posted fromWalking River Blog

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
stephencweiss profile image
Stephen Charles Weiss
Engineer | Lover of dogs, books, and learning | Dos XX can be most interesting man in the world, I'm happy being the luckiest. | I write about what I learn @ code-comments.com
  • Location
    Chicago, IL
  • Work
    Software Engineer at Olo
  • Joined

Returning to the topic of "first bad commit" / oldest / highest number -- highest number actually doesn't clarify it for me.

If we use the git log that you referenced as a basis for the example, and we wanted to revert the last five commits, would it be:

git revert --no-edit 111b003..852291a

following the pattern:

git revert --no-edit <oldest-commit-hash>..<newest-commit-hash>

1   852291a   blah blah blah2   f575c87   blah blah3   83d855d   blah blah4   9fa11df   blah blah blah blah5   111b003   blah blah6   2b3a530   blah blah blah7   a4c5f54   blah blah blah blah blah blah8   b2a62fa   blah blah blah blah blah blah blah blah blah9   5fb67b9   blah blah blah10  4d1a5fc   blah blah blah11  ed40aec   Initial commit

Thanks!

CollapseExpand
 
jay97 profile image
Jamal Al
Free individual, lost in the world of javascript
  • Location
    San Antonio TX
  • Education
    Associate in Computer Programming
  • Joined

Wow this is funny.

CollapseExpand
 
walkingriver profile image
Michael D. Callaghan
I help build cool web apps | Author of http://DontSayThatAtWork.com, https://AngularAdvocate.com, and more at Amazon https://amazon.com/author/mcallaghan | #LDS
  • Location
    Orlando, Florida
  • Work
    "Lead" Software Engineer at Disney Parks Experiences and Products Technology
  • Joined

Thanks, I think? :)

CollapseExpand
 
stephencweiss profile image
Stephen Charles Weiss
Engineer | Lover of dogs, books, and learning | Dos XX can be most interesting man in the world, I'm happy being the luckiest. | I write about what I learn @ code-comments.com
  • Location
    Chicago, IL
  • Work
    Software Engineer at Olo
  • Joined

FYI- the SO link is dead here and on your blog -- not sure if there was a specific conversation you were pointing to.

CollapseExpand
 
walkingriver profile image
Michael D. Callaghan
I help build cool web apps | Author of http://DontSayThatAtWork.com, https://AngularAdvocate.com, and more at Amazon https://amazon.com/author/mcallaghan | #LDS
  • Location
    Orlando, Florida
  • Work
    "Lead" Software Engineer at Disney Parks Experiences and Products Technology
  • Joined

I am not sure how it ended up being a link. I think I was simply saying I searched on SO, and was not intending to point to any specific conversation. Thanks for pointing it out.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I help build cool web apps | Author of http://DontSayThatAtWork.com, https://AngularAdvocate.com, and more at Amazon https://amazon.com/author/mcallaghan | #LDS
  • Location
    Orlando, Florida
  • Work
    "Lead" Software Engineer at Disney Parks Experiences and Products Technology
  • Joined

More fromMichael D. Callaghan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp