Git
git reset
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~1
This 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.txt
This allows you to reorganize what to commit without losing any code in the working tree.
Exercise 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.
Show example 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 -s
Test yourself
-
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
- Pushing staged files directly to the remote server
- Creating a new merge commit that joins two different branches
Show answer
Moving HEAD and the current branch pointer to a specified commit — git reset moves HEAD and the current branch pointer back to a specified commit target in history.
-
Unstage a file
git reset ____
Show answer
git reset README.md
-
git reset fundamentals
- 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
Next lesson: soft mixed hard