🐙
Git Developer Guide
About
  • Overview
  • Scope of this book
  • Table of Content
  • 🐢Introduction to Version Control
    • What is Version Control?
    • Overview of git and it's benefits
    • Setting up Git on Different Platforms
  • 🍼Git Fundamentals
    • Initialising a new Git repository
    • Understanding the Git Workflow
    • Committing Changes and Writing Good Commit Messages
    • Viewing and Navigating Commit History
    • Git Basics - Practice Scenarios
      • Initialising a Git Repository
      • Committing Changes
      • Exploring Commit History
      • Amending and Undoing Commits
  • 🦕Working With Git
    • What is Git Branch?
    • Creating and Switching Between Branches
    • Merging Branches and Resolving Conflicts
    • Best Practices for Branch Management
    • Git Workflows
    • Git Log
    • Git Stash
    • Working with Git - Practice Scenarios
      • Creating and Switching Between Branches
      • Merging Branches and Resolving Conflicts
      • Branching Strategies in a Team Project
      • Rolling Back to a Previous Version
      • Experimenting with Feature Branches
      • Working with Stash
  • 🤝Working with Remote Repositories
    • Cloning a Repository from Remote
    • Pushing and Pulling Changes to and from Remote Repositories
    • Collaborative Workflows - Forking, Branching, and Pull Requests
    • Resolving Conflicts in a Collaborative Environment
    • Collaborating with Git - Practice Scenarios
      • Cloning a Remote Repository
      • Pushing and Pulling Changes
      • Collaborative Workflow with Forking and Pull Requests
      • Resolving Conflicts in a Pull Request
  • 🏆Advanced Git Features
    • Aliases and Custom Configurations
    • Working with Tags and Releases
    • Rewriting Commit History with Interactive Rebase
    • Utilising Git Hooks for Automation
    • Advanced Git Features - Practice Scenarios
      • Creating Custom Git Aliases
      • Working with Tags and Releases
      • Rewriting Commit History with Interactive Rebase
      • Using Git Hooks for Automated Testing
  • 😎Git in Real-World
    • Managing a Project with Multiple Contributors
    • Integrating Git with Continuous Integration, Continuous Deployment (CI, CD)
    • Versioning Assets with Git LFS (Large File Storage)
    • Deploying a Web Application using Git
    • Git In Real World - Practice Scenarios
      • Managing a Project with Multiple Contributors
      • Integrating Git with CICD Pipelines
      • Versioning Assets with Git LFS
      • Deploying a Web Application using Git
  • Git Troubleshooting
    • Common Mistakes and Pitfalls When Using Git
    • Undoing Changes with Git - Reverting and Resetting
    • Recovering Lost Commits or Branches
    • Dealing with Repository Corruption or Other Issues
  • Git Best Practices and Tips
    • Creating efficient git workflows: writing clean code for faster reviews
    • The importance of clean code in collaborative development
    • Significance of consistent naming conventions & coding Standards
    • Good code documentation for better git workflows
    • Writing meaningful git commit messages
    • Atomic commits in git & it's benefits for software teams
    • Structuring code & managing dependencies for better git workflows
    • Git branching strategies for software teams
  • Conclusion & Next Steps
    • Recap of Key Concepts and Commands
    • Further Resources for Expanding Git Knowledge
    • Encouragement and Tips for Continued Learning and Practice
  • License Considerations
Powered by GitBook
On this page

Was this helpful?

  1. Git Fundamentals
  2. Git Basics - Practice Scenarios

Amending and Undoing Commits

When commits aren't right, what do we do?

Scenario

Let's assume you're a developer trying to correct a typo or adding missed changes to an ongoing solution. Here's how you can do it:

Step 1 - Make the necessary changes to your files

  • Update the files with the required modifications.

  • Use any text editor or IDE of your choice to make the changes.

Step 2 - Stage the changes

Use the following command to stage the modified files:

git add <file1> <file2> ...

Step 3: Amend the commit

  • To incorporate the changes into the most recent commit, use the following command:

git commit --amend
  • This will open a text editor where you can modify the commit message.

  • Save and close the editor to finalize the amended commit.

Amending a commit creates a new commit with a new commit ID. Therefore, it is recommended to only amend commits that have not been pushed to a remote repository.

Reverting to a Previous Commit

Reverting a commit is useful when you want to undo the changes introduced by a specific commit without removing it from the commit history. There are two primary methods to achieve this: using git revert and using git reset. Let's explore both approaches:

Using git revert

The git revert command creates a new commit that undoes the changes made in a previous commit while keeping the commit history intact.

Step 1: Identify the commit to revert.

  • Use git log to view the commit history and identify the commit hash of the commit you want to revert.

Step 2: Revert the commit.

  • Execute the following command to revert the identified commit:

git revert <commit-hash>
  • Git will create a new commit that undoes the changes introduced by the specified commit.

Using git reset

The git reset command allows you to reset the branch pointer to a previous commit, effectively removing commits from the commit history. This method should be used with caution, as it modifies the commit history.

Step 1: Identify the commit to reset to.

  • Use git log to find the commit hash of the commit you want to reset to.

Step 2: Reset the branch.

  • Execute the following command to reset the branch to the specified commit:

git reset --hard <commit-hash>

This will remove the commits after the specified commit and reset the branch pointer.

Note: When using git reset, be cautious as it discards commits permanently.

It is not recommended to use git reset on commits that have been pushed to a remote repository.

PreviousExploring Commit HistoryNextWhat is Git Branch?

Last updated 1 year ago

Was this helpful?

🍼