How to revert a Git commit without losing changes?

GitGitBeginner
Practice Now

Introduction

Git is a powerful version control system that allows developers to manage their codebase effectively. However, there may be times when you need to revert a commit without losing the changes you've made. This tutorial will guide you through the process of reverting a Git commit while preserving your valuable work.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git(("`Git`")) -.-> git/DataManagementGroup(["`Data Management`"]) git/BranchManagementGroup -.-> git/reflog("`Log Ref Changes`") git/BasicOperationsGroup -.-> git/commit("`Create Commit`") git/DataManagementGroup -.-> git/restore("`Revert Files`") git/DataManagementGroup -.-> git/reset("`Undo Changes`") git/BranchManagementGroup -.-> git/rebase("`Reapply Commits`") subgraph Lab Skills git/reflog -.-> lab-415168{{"`How to revert a Git commit without losing changes?`"}} git/commit -.-> lab-415168{{"`How to revert a Git commit without losing changes?`"}} git/restore -.-> lab-415168{{"`How to revert a Git commit without losing changes?`"}} git/reset -.-> lab-415168{{"`How to revert a Git commit without losing changes?`"}} git/rebase -.-> lab-415168{{"`How to revert a Git commit without losing changes?`"}} end

Understanding Git Commits

Git is a distributed version control system that allows developers to track changes in their codebase over time. At the heart of Git are commits, which represent snapshots of your project at a specific point in time.

What is a Git Commit?

A Git commit is a collection of changes made to one or more files in your repository. When you make changes to your code and want to save those changes, you create a new commit. Each commit has a unique identifier, called a commit hash, which allows you to reference and track the changes made in that specific commit.

Anatomy of a Git Commit

A Git commit typically consists of the following elements:

  1. Commit Message: A brief description of the changes made in the commit, usually written in the imperative mood (e.g., "Add new feature", "Fix bug in login process").
  2. Author: The person who made the changes and created the commit.
  3. Timestamp: The date and time when the commit was created.
  4. Commit Hash: The unique identifier for the commit, usually a long string of letters and numbers.
  5. Parent Commit(s): The previous commit(s) that this commit is based on.
  6. Changes: The specific files that were added, modified, or deleted in this commit.

Viewing Commit History

You can view the commit history of your Git repository using the git log command. This will show you a list of all the commits in reverse chronological order, with the most recent commit at the top.

git log

This will display the commit hash, author, date, and commit message for each commit in your repository.

Reverting Commits in Git

Reverting a commit in Git is the process of undoing the changes introduced by a specific commit. This is useful when you want to undo a commit that has already been pushed to a remote repository or shared with other team members.

Reverting a Commit

To revert a commit, you can use the git revert command followed by the commit hash or the branch name that you want to revert.

git revert <commit-hash>

This will create a new commit that undoes the changes made in the specified commit. The new commit will have a different commit hash, but it will be linked to the original commit as its parent.

Reverting Multiple Commits

You can also revert multiple commits at once by providing a range of commit hashes or branch names.

git revert <start-commit-hash>..<end-commit-hash>

This will create a new commit that undoes the changes made in all the commits between the specified start and end commits.

Reverting a Merge Commit

If you need to revert a merge commit, you can use the --mainline option to specify which parent commit you want to revert to.

git revert -m 1 <merge-commit-hash>

The -m 1 option tells Git to revert the changes from the first parent of the merge commit, which is typically the branch you merged into.

Viewing Reverted Commits

After reverting a commit, you can view the commit history to see the new commit that undoes the changes.

git log

This will show you the reverted commit, as well as the new commit that undoes the changes.

Preserving Changes During Revert

When you revert a commit, Git will undo the changes introduced by that commit. However, if you have made additional changes to the same files since the reverted commit, you may want to preserve those changes. Git provides several options to help you do this.

Using git revert --no-commit

The --no-commit option allows you to revert the changes without creating a new commit immediately. This gives you the opportunity to review the changes and decide how to proceed.

git revert --no-commit <commit-hash>

After running this command, you can review the changes and decide whether to create a new commit or discard the reverted changes.

Using git reset

If you want to completely discard the reverted changes and keep your current working directory, you can use the git reset command.

git reset --hard <commit-hash>

This will reset your working directory to the specified commit, effectively undoing the revert operation.

Resolving Conflicts

If the reverted changes conflict with your current changes, Git will ask you to resolve the conflicts manually. You can do this by editing the conflicting files, choosing which changes to keep, and then adding the resolved files to the staging area.

git add <conflicting-files>
git commit -m "Resolve conflicts after revert"

After resolving the conflicts, you can proceed with the revert operation.

Preserving Specific Changes

If you want to preserve specific changes from the reverted commit, you can use the git cherry-pick command to selectively apply those changes to your current branch.

git cherry-pick <commit-hash>

This will apply the changes from the specified commit to your current branch, allowing you to keep the changes you want while reverting the rest.

By using these techniques, you can effectively revert commits while preserving the changes you want to keep in your working directory.

Summary

By the end of this tutorial, you will have a comprehensive understanding of how to revert a Git commit without losing your changes. You'll learn the essential techniques to undo unwanted commits while keeping your hard-earned progress intact. This knowledge will empower you to confidently manage your Git repository and maintain a clean, organized codebase.

Other Git Tutorials you may like