Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What is Git Undo Last Commit? How to Rewrite History — Version History, That Is

DZone's Guide to

What is Git Undo Last Commit? How to Rewrite History — Version History, That Is

Need to get rid of your most recent commit in Git? Scared of messing your branch up? Check out this post to eliminate that fear and handle the situation with confidence!

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

Git is, without a doubt, an MVP in the development world. If you host your repos in Git, knowing the tricks of the trade is essential. In this post, we’ll take a look at how to use undo last commit to scrap your most recent committed changes that you don’t want or need to tweak. For more Git tips, check out this post on repo size maintenance and this post on checking out remote branches.

Definition of Git Undo Last Commit

Git undo last commit is used to scrap the most recent record of changes made to a file in a distributed version control system (DVCS) without losing the changes themselves. The changes can be retained so the user can edit them and re-commit them.

How Git Undo Last Commit Works

To understand how git undo last commit works, the user needs to understand some definitions:

  • Git: A DVCS that has a reputation for being a fast and easy way to manage file versions in a collaborative environment. Git tracks
    • Who made changes to files.
    • When they made changes to files.
    • Why they made changes to files.
    • What was changed in the files.
  • Command line interface: A text-based user interface for a computer operating system. What you type is what you get.
  • Commit: A git command that records a permanent (except when undo last commit is used) record of a file at that time.
  • Tree (or branch): The directory that contains a list of file names.
  • HEAD: The representation of the current commit in a branch.
  • Working tree: Where files are currently being worked upon. The files need to be registered in the index to perform a commit.
  • $: The dollar sign signals the start of a line in a command line interface.

Learn more definitions for working with git commands.

Almost anything that has been committed in git can be recovered — even if commits were deleted or overwritten. But if changes weren’t committed and lost, they are unlikely to be recovered.

To make git undo last commit, the user can use SmartGit, a graphical git that runs on Linux, Mac OS X (10.7 or newer) and Windows (Vista or newer). Choose the menu command Local|Undo Last Commit to undo that permanent record. The changes won’t be lost, so it will be possible to edit the changes and re-commit them.

Also, git-extras is a collection of git utilities that create aliases to commands needed to work with revision histories. For example, git undo last commit can be shortened to git undo.

To achieve the undo last commit, several options are using the command line. Some users appreciate the command line over graphic interfaces like some drivers appreciate standard transmission over the automatic transmission.

Git Undo Last Commit

Screenshot via StackOverflow.

There are different ways to undo the last commit using the command line — which makes the lack of graphical interface a feature, not a bug.

  • $ git reset –soft HEAD^ This will undo the last commit and save the changes. The soft means the reset doesn’t change the index file or working tree. This gives users the ability to edit changes and re-commit them.
  • $ git reset –hard HEAD~1 This will undo the last commit and reset the index and working tree. Any changes are gone. If more than one commit needs to be undone, $ git reset –hard HEAD~2 will undo the last two commits. Note that reset will undo all commits that came after the one returned to.
  • $ git revert commit-hash This creates a new commit that is the last commit. Hash is a synonym for the unique identifier of an object usually represented by a 40 character hexadecimal string.
  • $ git revert HEAD~2 Similar to the command above, this command creates a new commit that is the third last commit in the HEAD.
  • $ git checkout filename The command will undo all uncommitted changes the specific filename. Use a “.” instead of a filename to wipe out all uncommitted changes.
  • $ git commit –amend Sometimes only a commit’s message needs to be edited. This command will let users change the message associated with the last commit.
  • $ git rm filename Remove a file after it was added to an index.

Best Practices for Using Git Undo Last Commit

A git undo last commit command is not terribly complicated. Nonetheless, probably the best way to undo the last commit is not to need to do go back and revise history. So commit to using version control best practices:

  • Commit related changes: Each commit should have related, small changes to help collaborators understand changes and undo any problems. Put different changes and fixes in different commits.
  • Commit often: Constantly committing changes is a way to ensure the changes represented are small. It also makes it easier to share.
  • Don’t commit partially-completed work: Split the work into logical chunks that can be committed when each chunk is finished.
  • Test before committing: The work isn’t complete unless it works. Test it before sharing with collaborators.
  • Write informative commit messages: Start with a short summary — 50 characters is a good limit. Then explain the changes by detailing what was changed and why it was changed.
  • Avoid using version control as a backup system: Don’t use git to store backup files.
  • Use branches: Branches help separate different lines of development. Use branches to organize commits logically.
  • Agree on a pre-defined Workflow: Make sure everyone understands who is responsible for what areas of development. There are many ways to collaborate. Pick one and stick to it.

To learn more about Git undo last commit, visit the following resources and tutorials:

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
git ,git commands ,commit ,git reset ,integration

Published at DZone with permission of Angela Stringfellow, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}