UNDO Mistakes with GIT #1

Bennison J
YavarTechWorks
Published in
4 min readJan 5, 2024
UNDO Mistakes with GIT #1

Hey everyone! ๐ŸŒŸ Letโ€™s dive into undoing mistakes in GIT with two key commands: git restore and git reset. No worries if you accidentally messed up with git! ๐Ÿ˜Š๐Ÿคทโ€โ™‚๏ธ๐Ÿ”ง๐Ÿ”„๐ŸŒ๐Ÿ’ก

Restoring the Changes

  • By using the command git restore we can do two main things. one is discord changes from the current working tree, and the other one is restoring the file from a specific path.

Discord changes from the current working tree

git restore <filename>
  • This command allows us to revert the changes that were made to the current working tree. If we run this command without any special options, it will simply revert the untracked changes.
  • If we need to revert the staged changes also, we can proceed with the following command
git restore --staged <filename>
  • The above command will only restore the staged changes, because of the option we provided --staged . So it will simply unstage the staged changes. and move that changes to the working directory without losing the changes.
  • If we need to revert the specified part, we need to use the option patch --path or -p .
git restore --patch <filename>
  • This above command allows us to interactively restore specific changes to the file. If we need to restore specific changes to the file then we can proceed with this.
  • The above command allows us to select specific changes from a file to restore or discord to its last commit state.
  • If we need to restore the entire working directory, you can simply use the following git command.
git restore .

NOTE: In all git restore commands, if you didnโ€™t use the option--staged it will only revert the untracked changes.

Restore from a specific commit

  • Here I want to talk a little bit more about git restore command, We need to remember one thing, if we donโ€™t specify any source option in the git restore command, it will simply restore a file, a patch, or a working tree to the last commit.
  • If we need to restore a file to a specific commit or state, we need to proceed the git restore command with the option --source with the value of the commit hash.
git restore --source <commit-hash> filename
  • We can do all things as I said above, we can restore specific files, patches, or an entire working directory when using the option --staged

We can restore a file using the command git checkout

git checkout .
git checkout <filename>
git checkout --patch <filename>
  • Here the option patch used to select changes within a file to revert or discord.

Note: The commands git checkout will restore a working directory or file it to state it has in the last commit of the current branch.

Fixing the last commit message

  • Donโ€™t panic, if you accidentally wrote the wrong commit message to a commit. we can easily change that commit message by using the git commit command with the option --amend.
git commit --amend -m "commit message"

NOTE: Avoid changing the commit message after pushing to a remote repository to maintain history consistency and prevent confusion between collaborators (As this rewrites the git log, it would confuse).

Reverting git commit

  • If you need to revert the working directory to a specific commit, that is possible.
  • To revert a commit in GIT, especially if it is a commit in the middle of the commit history, we can use the git revert command. This will create a new commit that undos the changes made by the specified commit.
git revert <commit-hash>

For example, here the following is your commit history before reverting

A -- B -- C -- D  (HEAD)

And, Now you decided to rever the commit to C .

git revert C

The above command will create a commit that undos the modification made the commit C .

Note: When we run the commandgit revert A, Git will create a new commit that reverses the changes introduced in commit A only. It won't revert changes from commits after commit A (commits B, C, and D in our example).

Resetting to the old commit

  • If you need to delete everything before a specific commit, we can achieve this by using the following command.
git reset <commit-hash>
  • We can run this command with three modes.
  • Soft Reset: Moves the HEAD pointer to a new commit, keeping the changes staged (this means, all the commits that you reverted will be available in the staged area).
git reset --soft <commit-hash>
  • Mixed Reset: Resets the HEAD pointer to a new commit and unstages the changes, but preserves them in the working directory (this means, all the commits that you reverted will be available in the working directory as untracked contents).
git reset --mixed <commit-history>
  • Hard Reset: Resets the HEAD pointer to a new commit and discards all changes in the working directory and staging area (this means, all the commits that reverted wonโ€™t be available either in the staged or untracked area โ€” the commit that we reverted permanently reverted).
git reset --hard <commit-history>
  • If you did not use any of the options from these, git reset will use the option --mixed as the default one.

NOTE: git reset is powerful, but at the same time it is risky as it rewrites the commit history (When you use git reset, it erases the history of commits that came after the one you reset.)

I hope this article was helpful and enjoyable! Looking forward to seeing you for Part Two! ๐ŸŒŸ๐Ÿš€๐Ÿ“š๐Ÿ˜Š๐Ÿ‘‹

--

--

Bennison J
YavarTechWorks

๐Ÿ‘ฉโ€๐Ÿ’ป Software Engineer ๐Ÿš€ UNIX/Linux โ™ฅ๏ธ | JavaScript/Node.js ๐Ÿ”ฅ | SQL ๐Ÿ“Š | Backend Developer ๐Ÿ’ป | Tech Blogger โœ๏ธ | Tech Enthusiast ๐ŸŒŸ |Continuous Learner ๐Ÿ“š