How to Reset and Restore in Git

Abdul Jabbar Feb 02, 2024
  1. Git restore Command
  2. Git reset Command
  3. Difference Between git reset and git restore
How to Reset and Restore in Git

Git has a few commands that do things with individual files, but they are quite rare. These commands are git add, git rm, git mv, git checkout, git reset, git restore, and their respective opposites.

Git is clever, though; it remembers where we’ve got the work that we’ve fetched so that it can fetch it again later from the next fetch marker. It means that we can have a Git repository that contains only the commits that we’ve created locally. If we’ve already fetched the same commits from another repository, Git can avoid fetching them again.

Here, in this article, we will discuss Git’s two significant commands:

  1. git restore
  2. git reset

Git restore Command

Git restore command is used to restore files in the working tree from the index or another commit. This command does not update the current branch. It can also restore files in the index from another commit. It is very useful if we want to use the code from a commit different from the commit on the current branch. The restore command helps to discard uncommitted local changes in the local branch.

Git restore can be used in three different situations, depending on the current situation, whether we like to revert work in the working copy or the index, or both.

git restore [--worktree] <file>

The above command will overwrite <file> in our working copy along with the contents in our index file. Or we can say it reverts our changes in the working copy. Whether we specify it does not matter because it is suggested if we don’t say otherwise.

git restore --staged <file>

The above command will overwrite <file> in our index file with the present HEAD from the local repository. Or we can say the uncommitted changes recently staged content. It won’t be wrong if we say it is indeed equal to the old git reset HEAD <file>.

git restore --staged --worktree --source HEAD <file>

To overwrite both, as mentioned above, the working copy and the index with the present HEAD, we use the above command. This version will be able to do both: revert our working copy to HEAD and unstaged recently staged work from the current local branch.

Git reset Command

Git reset is about updating our branch, moving the tip to add or remove commits from the branch. This operation changes the commit history. Git reset is used to restore the index, overlapping with Git restore. If a file is changed, either by using a manual edit or by a git add method, git reset does not handle it well.

The file is copied to the new commit and then marked as deleted in the old commit. It gives a warning that the file appears to have been removed from the working copy. Git reset also does not handle renames well. It does not mean to delete a file and add a file with the same name, but it does.

The reset command cannot be used to move files in the working copy. The files are not only copied to a different commit; they are removed from the working copy and restored as new files in the target commit. When we have made unwanted changes to a file, it is not necessary to delete the file. We can undo all changes to a file, but it does not undo the file itself.

Difference Between git reset and git restore

The restore command helps us to unstage or discard uncommitted local changes. It can be used for restoring files in the working tree from the index or another commit. This command does not update our branch. It can also be helpful to restore files in the index from another commit.

Git reset is used for updating the current branch. It can also be helpful to restore the index, intersecting with git restore. Git reset is used for resetting the index to discard changes in the working tree. This command will not update our branch. It is mostly used with the --soft option, which only resets the index and leaves the working tree unchanged. It is useful if we have staged changes that we do not want to discard.

Git reset will complete successfully if the working directory is clean (no changes to be committed), while the git restore will fail if the working directory is clean. Both can affect the HEAD. However, Git restore will only affect the HEAD indirectly through the staging area. Git reset can directly work with the index and the HEAD. Both can affect the HEAD. Git restore will only affect the index and the HEAD indirectly through the staging area.

Git reset can be used to modify the local repository only if we have not pushed anything yet. If we have pushed to the remote server, git-reset will modify the staging area and working copy, but not the repository. It is useful if we want to undo local changes but do not want to reintroduce them again.

Git restore is the opposite as it can only be used to modify our repository, not the staging area or local working copy. It will not affect any commits we have pushed.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Reset

Related Article - Git Restore