git reset
- Git course
- Intermediate
- Lesson 41/82
- 4 min
Learn to move HEAD with git reset.
Moving HEAD with git reset
The git reset command is used to move the current branch and HEAD pointer to an earlier or specified commit. When committing, Git advances the branch pointer; reset pulls this pointer backwards to reposition history. This allows one or more recent commits to be undone from your local history.
# Move HEAD to the previous commit:
git reset HEAD~1This is the fundamental way to fix mistakes in your local repository.
Unstaging files: git reset <dosya>
When passed a file path, git reset does not move the HEAD pointer; it simply unstages that file from the staging area. This is the exact inverse of git add and does not touch file content in the working directory. You can safely unstage a file accidentally added.
git add dosya.txt
# Unstage the file:
git reset dosya.txtThis allows you to reorganize what to commit without losing any code in the working tree.
Practice Unstage file using reset
Unstage the staged app.py file with git reset -q app.py and print the short status with git status -s.
Starter code
git init -q -b main
git config user.name "Ali"
git config user.email "ali@example.com"
echo "v1" > app.py
git add app.py
git commit -qm "Ilk commit"
echo "yeni kod" > app.py
git add app.py
# app.py dosyasini sahneden cikar ve kisa durumu basSample solution
git init -q -b main
git config user.name "Ali"
git config user.email "ali@example.com"
echo "v1" > app.py
git add app.py
git commit -qm "Ilk commit"
echo "yeni kod" > app.py
git add app.py
git reset -q app.py
git status -sExpected output
M app.py
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the fundamental function of the git reset command?
- Synchronizing remote branches with the local repository
- Moving HEAD and the current branch pointer to a specified commit Correct answer
- Pushing staged files directly to the remote server
- Creating a new merge commit that joins two different branches
Explanation: git reset moves HEAD and the current branch pointer back to a specified commit target in history.
2 Unstage a file
git reset ____
git reset README.md
Explanation: The git reset <file> command unstages the changes for the specified file while keeping them in the working tree.
3 git reset fundamentals
| Term | Meaning |
|---|---|
git reset <commit> |
Moves HEAD to the specified commit |
git reset <dosya> |
Unstages the specified file |
HEAD hareketi |
Pulls branch pointer backwards |
Unstage işlemi |
Does not touch working directory code |