Undoing Things [Learn Git Video Course]

preview_player
Показать описание
Learn how to undo things in Git. This video is part of our free 24-part video course on Learning Git on the Command Line.

***************************************

Additional Git tutorials and resources

***************************************

Welcome to our video series on learning version control with Git. In this video, you will learn how to undo things with Git.

Git allows you to undo almost anything. Knowing this should let you sleep a bit easier at night.

Let's start by changing your very last commit. Often after making a commit, you realize that you forgot to add a little change. Or you mistyped the commit message.

The most recent commit can easily be changed in Git. I can correct the commit message, simply by "committing again" with the "amend" flag. The latest commit was simply replaced by our amended, corrected one.

You can also add some more changes to that last commit. Let's say we forgot to add file-C. I simply stage it as normal and then commit again.

The amend feature is great for fixing your very last commit - as long as you keep one rule in mind: never amend a commit that was already published on a remote server. Amend rewrites a commit; and if you publish that commit again, you'll come into trouble.

Git also allows you to undo uncommitted local changes. Sometimes you realize that the changes you just made aren't worth keeping. You'd like to restore the last committed state of a file and start fresh. Type "git checkout", then the revision we'd like to return to - which is HEAD for the last committed state - and at last the filename. This discards any uncommitted changes we've made in that file since we last committed it. Typing "git status" again, we see that it's not listed as modified anymore.

If you had a very bad day, you might want to discard all of the changes you made recently. This is a job for the "reset" command. With "git reset --hard HEAD". We tell Git to restore the complete project at its last committed state. This will discard all local changes.

Be very careful with these two commands - you cannot restore uncommitted changes after you've discarded them.

Finally, you can also undo commits. E.g. when you notice that your changes were wrong or when you introduced a bug.

Using the "git revert" command is one possibility to undo a commit. However, the command doesn't delete any commits. In fact, it even creates a new commit. One that reverts the effects of that other commit, effectively undoing it. For example, if your original commit added a word in a certain place, the reverting commit will remove exactly this word, again.

Let's do this on the command line. "git log" shows us our recent commits. Now, let's say we introduced some bad code with this commit. Let's revert this commit. Simply type "git revert" and the hash of that commit. Another look at "git log" shows us that we indeed have a new commit that reverted the old one. And, with a closer look, we see that it indeed introduced changes that revert that other commit.

A different way to "undo" commits is "git reset". It works by resetting your HEAD branch to an older revision (also called "rolling back" to that older revision).
After this command, your currently checked out branch will be at the named revision. And the files in your working copy will be exactly as they were in that revision.
All the commits that came after this one are effectively undone. They are no longer visible in the history of this branch.

Let's say we want to roll back our working copy to a commit. Type "git reset --hard " and the commit hash. "git log" shows us the new situation: that commit is now the latest one and any newer commits are effectively undone.

One last thing to keep in mind: Both commands, revert and reset, only affect your current HEAD branch. Therefore, always make sure you've checked out the correct branch before playing with them.

SUBSCRIBE to learn more about Git, Tower, and how to become a better developer!

For more free ebooks, cheat sheets, and video tutorials make sure to check out our FREE learning platform!

STAY UP-TO-DATE:

#git #learngit #gittutorial
Рекомендации по теме