An Interest In:
Web News this Week
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
- April 12, 2024
- April 11, 2024
World's Easiest Guide to GitReset
For some reason, I was always afraid of this command.
git reset
Even though I always knew what git reset does, every time I ran it in the console I was scared that something would go wrong until
I made a guide to myself on how to use it.
Let me quickly and clearly explain how git reset works.
Git Lifecycle
To understand git reset we need to learn more about the git lifecycle.
When you make some changes, they have 3 statuses:
- Unstaged
- Staged
- Commit
You created a file and wrote abc
there, these changes have unstaged
status.
Then you do git add myfile
command, your changes go to git "index" (place where the files we're going to commit to the git repository are) and status of your changes become staged
.
Then you do git commit command and status of your changes become commited
.
It's a simplified version of the git lifecycle.
Our setup
We have a master branch with 3 commits: A, B, C.
And file in our working directory file.txt
with such history:
git inittouch file.txtecho -n "a" >> file.txt// file.txtagit add .git commit -m "A"echo -n "b" >> file.txt// file.txtabgit add .git commit -m "B"echo -n "c" >> file.txt// file.txtabcgit add .git commit -m "C"
Initially, we added the letter a
to file.txt and made a commit A.
Then we added the letter b
to file.txt and made a commit B.
Then we added the letter c
to file.txt and made a commit C.
Ok, let's start.
git reset --soft
We are now on commit C. Let's do git reset.
git reset --soft b
What happened?
HEAD
is on commit B
- Changes from commit C (added letter
c
) still in file.txt and has status staged
// file.txtabc
- If you do
git commit -m "C"
right now, you will get identical commit C.
git reset --mixed
We are now on commit C. Let's do git reset.
git reset --mixed b
What happened?
HEAD
is on commit B
- Changes from commit C (added letter
c
) still in file.txt and has status unstaged
// file.txtabc
- If you do
git add file.txt
thengit commit -m "C"
right now, you will get identical commit C
The only difference between --soft
and --mixed
is that changes get different status staged
vs unstaged
git reset --hard
We are now on commit C. Let's do git reset.
git reset --hard b
What happened?
HEAD
is on commit B
- Changes from commit C (added letter c) deleted from
file.txt
// file.txtab
- All uncommitted changes will be deleted from your working directory. So if you'll add any other changes to
file.txt
, don't commit them and dogit reset --hard
they'll be deleted from the working directory.
Summary
In the end...
I hope now you can understand the difference between different git reset commands and will use it without any fear.
If you like this article share it with your friends and follow me on Twitter
Get job interview tips, coding guides, and the latest Frontend insides Join my Newsletter
That's all. Thanks!
Original Link: https://dev.to/bulljsdev/world-s-easiest-guide-to-git-reset-47mi
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To