New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Reverting a commit

From: Git Essential Training

Video: Reverting a commit

In the last movie, we saw how we could amend older commits by crafting a new commit that would undo the changes from the previous commit, and we talked about how we could do that manually by just making the changes to the files or by checking out a previous version of the file and then assembling that together so we can do a commit. But Git also gives us a helpful way when we really want to just undo the changes for a commit completely and totally, we can use the Revert command. So let's take a look with git log. Here is the commit that we're talking about, this first one, Rearrange the items to bring on an outdoor trip.

Reverting a commit

In the last movie, we saw how we could amend older commits by crafting a new commit that would undo the changes from the previous commit, and we talked about how we could do that manually by just making the changes to the files or by checking out a previous version of the file and then assembling that together so we can do a commit. But Git also gives us a helpful way when we really want to just undo the changes for a commit completely and totally, we can use the Revert command. So let's take a look with git log. Here is the commit that we're talking about, this first one, Rearrange the items to bring on an outdoor trip.

What the Revert command will do is it will take all of the changes that were there, and it will flip them around. It will do the exact opposite of those changes. So, anything that was added will be deleted, anything that was deleted will be added back in again, and anything that was modified will be changed back to its previous state. It's going to be a complete mirror image of this commit. So, in order to use it, it's real simple. We just have to grab part of the reference to it. Remember, we don't need the whole thing. I'm just going to copy it, and let's use git revert, and then I'm going to put in that S-H-A.

Now, this is going to do it all in one step for us. It's not going to assemble the commit and then wait for us, it's going to go ahead and make that commit. So, I'm going to hit Return, it's going to pop up with the chance for me to edit the commit message, and it's going to do that in TextMate for me. So it pops up, and it says, all right, if you want to edit this message before I commit it, go ahead and do it now. Now, I'm happy with the default that it gives me. Just lines 1, 2, and 3 are going to be part of commit, all that commented out stuff is going to get ignored, and not be part of my commit message.

So, I'm just going to say Save and Close, and then Git will go ahead and do its thing. It went ahead and made the complete commit. So, if I do git status, you can see my working directory is still clean, but git log, and you can see that it did make this new commit here which reverts that old commit. And sure enough if we go and open up resources and take a peek at it, you'll see that down here it's now put insect repellent, sunglasses, and sunscreen back at the bottom of the list. Now, you can pass in the -in option with revert, and then it won't actually do the commit, it will just stage it and then wait for you to actually do the commit yourself.

It will give you the chance to write your own message for it and also make any other modifications you might need to make. Now, git revert works really well when things are simple. We're just making a simple change from A to B, and then we're going to switch it back from B back to A. But what if the changes are really complicated? What if in the meantime other things have changed? What if files have moved perhaps, things have been renamed? It might get little harder to make an exact mirror image commit. Git is going to use a more complex set of rules for how to deal with those changes, and the set of rules it's going to use is what we use for merging.

Merging is a more advanced technique that we'll talk about a little later on, but if you need to revert something complex, you're essentially going to find yourself doing a merge between the current branch and the new set of changes that you're trying to merge into it.

Show transcript

This video is part of

Image for Git Essential Training
Git Essential Training

89 video lessons · 27684 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 2m 46s
    1. Introduction
      1m 7s
    2. How to use the exercise files
      1m 39s
  2. 20m 24s
    1. Understanding version control
      4m 48s
    2. The history of Git
      7m 58s
    3. About distributed version control
      5m 4s
    4. Who should use Git?
      2m 34s
  3. 26m 12s
    1. Installing Git on a Mac
      3m 44s
    2. Installing Git on Windows
      5m 37s
    3. Installing Git on Linux
      1m 30s
    4. Configuring Git
      7m 29s
    5. Exploring Git auto-completion
      5m 35s
    6. Using Git help
      2m 17s
  4. 15m 49s
    1. Initializing a repository
      1m 58s
    2. Understanding where Git files are stored
      2m 34s
    3. Performing your first commit
      2m 4s
    4. Writing commit messages
      5m 22s
    5. Viewing the commit log
      3m 51s
  5. 17m 44s
    1. Exploring the three-trees architecture
      3m 57s
    2. The Git workflow
      3m 15s
    3. Using hash values (SHA-1)
      4m 7s
    4. Working with the HEAD pointer
      6m 25s
  6. 25m 52s
    1. Adding files
      5m 59s
    2. Editing files
      3m 56s
    3. Viewing changes with diff
      3m 35s
    4. Viewing only staged changes
      2m 28s
    5. Deleting files
      5m 29s
    6. Moving and renaming files
      4m 25s
  7. 19m 18s
    1. Introducing the Explore California web site
      2m 2s
    2. Initializing Git
      3m 48s
    3. Editing the support phone number
      6m 20s
    4. Editing the backpack file name and links
      7m 8s
  8. 38m 45s
    1. Undoing working directory changes
      3m 49s
    2. Unstaging files
      2m 37s
    3. Amending commits
      4m 50s
    4. Retrieving old versions
      4m 7s
    5. Reverting a commit
      3m 12s
    6. Using reset to undo commits
      3m 44s
    7. Demonstrating a soft reset
      4m 8s
    8. Demonstrating a mixed reset
      4m 7s
    9. Demonstrating a hard reset
      5m 8s
    10. Removing untracked files
      3m 3s
  9. 27m 22s
    1. Using .gitignore files
      8m 23s
    2. Understanding what to ignore
      4m 47s
    3. Ignoring files globally
      4m 49s
    4. Ignoring tracked files
      5m 26s
    5. Tracking empty directories
      3m 57s
  10. 26m 51s
    1. Referencing commits
      4m 52s
    2. Exploring tree listings
      3m 46s
    3. Getting more from the commit log
      7m 38s
    4. Viewing commits
      4m 4s
    5. Comparing commits
      6m 31s
  11. 39m 35s
    1. Branching overview
      4m 56s
    2. Viewing and creating branches
      2m 57s
    3. Switching branches
      2m 58s
    4. Creating and switching branches
      4m 53s
    5. Switching branches with uncommitted changes
      3m 26s
    6. Comparing branches
      4m 28s
    7. Renaming branches
      2m 28s
    8. Deleting branches
      4m 18s
    9. Configuring the command prompt to show the branch
      9m 11s
  12. 28m 32s
    1. Merging code
      3m 11s
    2. Using fast-forward merge vs. true merge
      6m 49s
    3. Merging conflicts
      7m 26s
    4. Resolving merge conflicts
      7m 5s
    5. Exploring strategies to reduce merge conflicts
      4m 1s
  13. 14m 34s
    1. Saving changes in the stash
      4m 5s
    2. Viewing stashed changes
      2m 39s
    3. Retrieving stashed changes
      4m 24s
    4. Deleting stashed changes
      3m 26s
  14. 1h 5m
    1. Using local and remote repositories
      6m 38s
    2. Setting up a GitHub account
      5m 39s
    3. Adding a remote repository
      4m 0s
    4. Creating a remote branch
      4m 3s
    5. Cloning a remote repository
      4m 26s
    6. Tracking remote branches
      4m 5s
    7. Pushing changes to a remote repository
      5m 8s
    8. Fetching changes from a remote repository
      5m 47s
    9. Merging in fetched changes
      4m 50s
    10. Checking out remote branches
      3m 22s
    11. Pushing to an updated remote branch
      2m 6s
    12. Deleting a remote branch
      3m 8s
    13. Enabling collaboration
      3m 40s
    14. A collaboration workflow
      8m 43s
  15. 16m 23s
    1. Setting up aliases for common commands
      5m 14s
    2. Using SSH keys for remote login
      2m 56s
    3. Exploring integrated development environments
      1m 4s
    4. Exploring graphical user interfaces
      4m 32s
    5. Understanding Git hosting
      2m 37s
  16. 55s
    1. Goodbye
      55s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.