0
0
Gitdevops~30 mins

git reset soft vs mixed vs hard - Hands-On Comparison

Choose your learning style9 modes available
Understanding git reset: soft vs mixed vs hard
📖 Scenario: You are working on a project using Git for version control. Sometimes you want to undo changes but keep some parts of your work safe. Git offers different ways to reset your work: soft, mixed, and hard. Each one affects your files and commits differently.
🎯 Goal: Learn how to use git reset --soft, git reset --mixed, and git reset --hard to undo commits in different ways.
📋 What You'll Learn
Understand what a Git commit is
Know how to check the current commit history
Learn how to reset commits with different options
💡 Why This Matters
🌍 Real World
Developers often need to undo commits without losing work. Knowing how to use different reset options helps manage code safely.
💼 Career
Understanding git reset options is essential for software developers, DevOps engineers, and anyone working with version control to fix mistakes and manage code history.
Progress0 / 4 steps
1
Set up a Git repository with three commits
Initialize a Git repository and create three commits named commit1, commit2, and commit3 by creating files file1.txt, file2.txt, and file3.txt respectively. Use git init, git add, and git commit -m commands exactly as shown.
Git
Need a hint?

Use git init to start the repo. Then create files and commit them one by one with the exact commit messages.

2
Set a variable for the commit before the last one
Create a variable called target_commit that stores the commit hash of the second commit (the one with message commit2) using git rev-parse HEAD~1.
Git
Need a hint?

Use git rev-parse HEAD~1 to get the commit before the last one and save it in target_commit.

3
Reset the last commit using git reset --soft
Use git reset --soft $target_commit to move the HEAD to the second commit but keep the changes from the last commit staged.
Git
Need a hint?

Use git reset --soft with the variable target_commit to undo the last commit but keep changes staged.

4
Show the current Git status after soft reset
Run git status to display the current state of the repository after the soft reset.
Git
Need a hint?

Use git status to see that the last commit's changes are still staged after the soft reset.