Undoing Things in Git

Undoing Things in Git

1. Fixing the last commit

If you've committed changes and later realize you've forgotten to include something, you have the option to make additional changes and amend the previous commit without creating an extra commit by using the "--amend" option.

git commit --amend

This command will open a text editor allowing you to modify or retain the existing commit message:

If you prefer not to alter the commit message, you can also include the "--no-edit" option

git commit --amend --no-edit

Note

git commit --amend comes in handy when you need to modify the message of your last commit.

Key Rule: Never amend a commit that has been published

  • Avoid amending commits that have been pushed to remote repositories because amending creates a new commit, replacing the old one. If you are working in collaboration, it can confuse other people, since their new work might be based on the original commit.

2. Unstaging a staged file

To unstage a file that you've previously added to the staging area in Git, you can use the following command:

git restore --staged <filename>

This command will remove the specified file from the staging area, effectively "unstaging" it, while keeping the changes in your working directory intact.

3. Unmodifying a modified file

To discard the changes made to a modified file and revert it to its previous state in Git, you can use the following command:

git restore <filename>

This command will effectively unmodify the specified file, returning it to the state it was in at the last committed version. Be cautious when using this command, as it permanently discards any unsaved changes in the file.

4. Reverting a file to a particular commit

In situations where you've previously committed a particular file but now wish to revert it to its state as it existed in a prior commit, you can effectively achieve this by utilizing the following command:

git checkout [commit_id] -- path/to/file

5. Undoing committed changes

  • git revert

  • git reset

At times, there arises a need to undo a particular commit in your Git history, maybe you realize the changes you made were incorrect. This can occur when you realize that the changes made were incorrect. In such cases, you can use the git revert command. This command does not replace the original commit, instead, it creates a new commit that effectively undoes the changes introduced by the targeted commit.

For instance, if the original commit added a word to a specific location, the reverting commit will remove that exact word, essentially restoring the code to its prior state.

git revert [commit_id]

Another helpful command is git reset. Git reset resets the current HEAD branch to the commit you specify. It provides three options:-

  1. git reset --hard: This command resets your working directory, staging area, and commits to a specified commit or branch. It effectively discards all changes and moves your HEAD pointer to the target commit. Use this with caution, as it irreversibly deletes changes.

     git reset --hard [commit_id]
    
  2. git reset --soft: This command resets your commits but keeps your changes staged in the index. It moves your HEAD pointer to the target commit, allowing you to make further adjustments before committing again.

     git reset --soft [commit_id]
    
  3. git reset --mixed (default): This command resets your commits and unstages the changes, placing them back into your working directory. It moves your HEAD pointer to the target commit while keeping your changes available for you to review and modify before committing.

     git reset --mixed [commit_id]
    

git reset --hard deletes the local changes, you can use the --keep option if you don't want to lose the local changes.

git reset --hard --keep [commit_id]

That's all for this blog post,

Thanks, for reading my post, if you like it, do consider following me.

Also, do follow my twitter/X page:- https://twitter.com/DevTalesShrey