Lesson 17 of 60 – Git Reset
28%

Git Reset

The git reset command is used to move the current branch reference to another commit and can also change the staging area or working tree depending on the option used.

Note: Git reset can have significant effects on your repository. Always understand whether you are using --soft, --mixed, or --hard before running the command.

1. What is git reset?

git reset is a command used to move the current branch reference to another commit and adjust the staging area and working tree according to the selected mode.

git reset

It is commonly used when you want to undo or reorganize local commits or remove changes from the staging area.

2. Why Use git reset?

Git reset can be useful when you need to move the current branch backward in local history or change what is staged.

  • Unstage files
  • Move a branch to an earlier commit
  • Undo local commits
  • Keep working-directory changes while moving the branch
  • Discard local changes when used with care

3. Basic Syntax

A common form of the command is:

git reset [options] [commit]

The result depends on the selected mode and the commit specified.

4. Git Reset and HEAD

Git reset commonly moves the current branch reference to a specified commit.

git reset HEAD~1

Here, HEAD~1 means the parent of the current commit.

5. Understanding HEAD~1

The expression HEAD~1 refers to one commit before the current HEAD.

HEAD
 ↓
Commit C
 ↓
Commit B

HEAD~1 = Commit B

Similarly, HEAD~2 refers to two commits before HEAD along the first-parent ancestry.

6. Three Main Reset Modes

The three commonly discussed reset modes are:

  • --soft
  • --mixed
  • --hard

They differ mainly in what happens to the staging area and working tree.

7. git reset --soft

The --soft mode moves the current branch to the specified commit but keeps the changes staged.

git reset --soft HEAD~1

The changes from the removed commit remain in the staging area.

8. Example of --soft

Commit A → Commit B → Commit C
                         ↑
                        HEAD

Run:

git reset --soft HEAD~1

The branch moves back to Commit B, while the changes represented by Commit C remain staged.

9. git reset --mixed

The --mixed mode moves the current branch and resets the staging area, while leaving working-tree changes intact.

git reset --mixed HEAD~1

This is the default mode when no mode is specified.

10. git reset Without an Option

When no mode is specified, Git uses the mixed mode:

git reset HEAD~1

This moves the branch back and resets the staging area while keeping working-tree changes.

11. git reset --hard

The --hard mode resets the current branch, staging area and working tree to the specified commit.

git reset --hard HEAD~1
Warning: The --hard option can discard uncommitted working-tree changes. Use it only when you are certain that those changes are not needed.

12. Comparing Reset Modes

Mode Branch Staging Area Working Tree
--soft Reset Kept Kept
--mixed Reset Reset Kept
--hard Reset Reset Reset

13. Resetting to a Specific Commit

You can reset to a specific commit using its commit identifier:

git reset --mixed abc1234

The branch moves to the specified commit.

14. Finding a Commit Before Reset

Use git log to find commit identifiers:

git log --oneline

For example:

8f32abc Update contact page
5d21ef4 Add contact page
1a72c90 Initial commit

15. Resetting to an Earlier Commit

git reset --mixed 5d21ef4

This moves the current branch to the specified commit and uses the mixed mode to reset the staging area while preserving working-tree changes.

16. Using git reset to Unstage a File

Git reset can also be used to unstage a file:

git reset HEAD index.html

This removes the file from the staging area while keeping the working-tree changes.

17. Modern Alternative for Unstaging

A modern alternative for unstaging a file is:

git restore --staged index.html

This command makes the intention clearer by specifically describing a restore operation on the staging area.

18. Resetting the Last Commit

Suppose the latest commit should be removed from the current branch. You can move the branch back one commit:

git reset --soft HEAD~1

The changes from the removed commit remain staged and can be committed again with a different message or after further editing.

19. Resetting the Last Commit with Mixed Mode

git reset HEAD~1

Because mixed mode is the default, this moves the branch back one commit and unstages the changes while keeping the working files.

20. Resetting the Last Commit with Hard Mode

git reset --hard HEAD~1

This moves the branch back one commit and resets the index and working tree to match that commit.

Warning: Uncommitted changes can be lost when using --hard.

21. Git Reset and Local History

Reset is commonly used for changing local history. For example:

Commit A → Commit B → Commit C

After:

git reset --soft HEAD~1

the current branch points back to Commit B, while the changes from Commit C remain staged.

22. Reset Does Not Delete Files in Mixed Mode

With the default mixed mode, reset changes the branch reference and staging area but leaves working-tree files unchanged.

git reset HEAD~1

You can then inspect the files and decide what to commit next.

23. Checking Status After Reset

After using reset, check the repository state:

git status

This helps you understand which changes are staged or unstaged.

24. Reset and Remote Repositories

Reset changes the local branch reference. If commits have already been shared with others through a remote repository, rewriting that history can create coordination problems.

For shared history, consider whether a command such as git revert is more appropriate.

25. Git Reset vs Git Revert

git reset moves the current branch reference and can rewrite local history.

git revert creates a new commit that reverses the effect of an earlier commit.

git reset
git revert

The two commands solve different problems.

26. Common git reset Mistakes

  • Using --hard without checking uncommitted changes.
  • Resetting the wrong commit.
  • Confusing reset with revert.
  • Using reset on shared history without understanding the consequences.
  • Forgetting to inspect git status after reset.
  • Assuming reset automatically updates a remote repository.

27. Useful git reset Commands

git reset HEAD index.html
git reset HEAD~1
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git reset --mixed abc1234
git log --oneline

28. Complete Reset Workflow

# View history
git log --oneline

# Check current state
git status

# Move back one commit but keep changes staged
git reset --soft HEAD~1

# Check state
git status

Always inspect your repository before and after a reset operation.

29. Practical Example

git init

touch index.html
git add index.html
git commit -m "Create homepage"

touch about.html
git add about.html
git commit -m "Create about page"

git log --oneline

git reset --soft HEAD~1

git status

The latest commit is removed from the current branch history while its changes remain staged.

30. Summary of git reset

The git reset command can move the current branch to another commit and control what happens to the staging area and working tree.

git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1

Remember:

  • --soft: branch moves, staged changes are kept.
  • --mixed: branch and staging area are reset, working tree is kept.
  • --hard: branch, staging area and working tree are reset.

Use reset carefully, especially when working with commits that have already been shared with others.

📌 Key Points

  • git reset can move the current branch to another commit.
  • --soft keeps changes staged.
  • --mixed resets the staging area but keeps working-tree changes.
  • Mixed mode is the default reset mode.
  • --hard resets the branch, staging area and working tree.
  • git reset HEAD filename can unstage a file.
  • HEAD~1 refers to the parent of the current commit.
  • Use git log --oneline to find commit identifiers.
  • Reset can rewrite local commit history.
  • Be especially careful with git reset --hard.
  • For already-shared history, understand the difference between reset and revert.

🧠 Quick Quiz

Question: Which git reset option resets the branch, staging area, and working tree to the specified commit?