Join us
@5minslearn ă» Aug 23,2022 ă» 7 min read ă» 1058 views
We often come across some weird scenarios (especially my team) while committing our code changes. Some of them are,
How can I bring the file to working area, which I have added to the staging area by mistake?
OMG! I made a commit with production keys in the code but did not push the code yet. Is it possible to delete that commit? (Meanwhile, my tech lead be like đĄ)
Can anyone say how to remove the changes which Iâve pushed already?
I logged onto Production machine and made a change to a file and fixed the production issue immediately đ (Can also be debugging on production đ). Now, I want to remove that change using git cli for the CI/CD to pass. Any Git commands available for this purpose?
Head to the bottom of this page, if you need blind answers for these scenarios. For those who need to understand more, continue from here.
Letâs find answers to all of your such queries with just 3 commands.
1. Checkout
2. Reset
3. Revert
What is Git Checkout?
git checkout
removes your changes which are in the working directory of your project. Remember, neither staged nor committed changes will be removed.
I have added some random text on File1 and File2. We can see the changes with git status
and git diff
commands.
Letâs say we need to remove all the changes from File1 file and bring it back to the original state. Letâs run git checkout
command on File1 and see the changes.
âOkay. Thatâs cool. Removing changes from 1 file is fine. How can I remove all the changes made in all files in our repo? â, Naras from my team raised the question.
Oh! Thatâs simple. Just add a .
(dot) after checkout
command to remove changes from all files in our repo.
Git released restore
command in itâs recent release as an alternative to checkout
command
What is Git Reset?
git reset
command moves the changes from staging area to working area.
I made changes to File2 and staged it.
Running git reset
on File2 will bring the changes to the working directory
reset
is a powerful command in git which can perform unbelievable actions based on the parameters passed. One of the powerful feature is itâs ability to remove a commit from repository provided some conditions.
Letâs explore them below
Types of Git Reset
Git Reset is divided into 3 types. They are,
Mixed Reset
The Mixed reset command brings back the committed changes to the working directory phase.
The syntax of mixed reset command is,
n
represents the number of commits to be removed (from the end).
Letâs remove the last 1 commit from our code.
As I ran git reset HEAD~1
, the last one commit on the repo has been removed and those changes are brought to the working directory.
Soft Reset
The Soft reset command is similar to mixed reset. This command brings back the committed changes to the staging area.
The syntax of soft reset command is,
n
represents the number of commits to be removed (from the end).
Letâs remove the last 1 commit from our code.
As I ran git reset HEAD~1 --soft
, the last one commit on the repo has been removed and those changes are brought to the staging area.
Hard Reset
The Hard reset command is bit dangerous command. This command deletes the commits from git.
The syntax of hard reset command is,
n
represents the number of commits to be deleted (from the end).
Letâs remove the last 1 commit from our code.
As I ran git reset HEAD~1 --hard
, the last one commit on the repo has been deleted completely from the repo.
âWhy are stressing the word dangerous here. Howâs this command so dangerous when compared with other reset commands? â, intelligent Kumar interrupted with this question.
Well. Thatâs an interesting question.
The reason Iâm stressing that itâs so dangerous is that, this commit has the power to delete all our changes. Remember this does not bring back the changes to either staging or working area. Running this command does not ask for any confirmation from the user.
Think of a scenario where you made a small mistake by adding a â0â next to â~1â in the above command and hit âEnterâ without looking and confirming the command. So, the command you ran would be,
git reset HEAD~10 --hard
Can you imagine how dangerous it is now? You lost 10 of your most valuable commits. Thereâs no way to reverse this process unless you pushed the code to server. So, you have to make all your changes from the beginning again.
I could better explain this with a real scenario that happened between me and Aadhithyanath (One of my colleague).
One day Aadhi approached my help to resolve a bug. I saw there were changes in 10+ files (around 14 files). I asked him to commit those changes before proceeding. He compressed all changes together in a single commit. We started fixing the bug. We fixed one scenario where the bug has happened. But, there are other scenarios too we need to handle. We made a commit. While heading to fix other scenarios we found that our last commit containing the fix for 1st scenario was implemented in the wrong way. So, we planned to remove that commit and go ahead with the correct implementation. I ran the following commit,
git reset HEAD~2 --hard
Aadhi suddenly shouted âOH MY GOD!!!!â
âWhat happened? â, I asked him.
âYou deleted all my changes đ â, replied Aadhi.
âWhat are you saying? â, I was asking him by looking at the terminal what Iâve executed. Do you notice that ~2
?
Yes. I deleted the last 2 commits. To our bad luck, we deleted the commit which Aadhi has compressed his changes altogether in a single commit. It was 4 hours of his hard work. Adding up to our bad luck, we havenât pushed that code to the remote repo. This happens often to those who type faster. Iâm one among them đ.
Do you understand how dangerous it is?
Thatâs the reason, I stressed the word dangerous here. Do not execute this command unless youâre 200% sure of whatâs will be the result of it.
I remember you told,
One of the powerful feature is itâs ability to remove a commit from repository provided some conditions.
âWhat are those conditions from the above line?â, asked Udhaya, a developer cum marketer from our team.
Oh! I forgot to mention about that important condition. Thanks for reminding Udhaya.
The only one condition you need to keep in mind is that, reset
command should not be run with the commits pushed to the remote repo.
âWhy?â, I hope this strikes your mind.
This main purpose of reset command is to make changes to your commit. So, when you make a change on the commit / delete a commit which you already pushed to remote repo, the hash of that commit will change. This will not let you to push your changes further to the remote repo unless you force push it.
Note: Doing a force push in a repo is not a recommended way unless it is an unavoidable case
Because, git pushes the changes to remote by comparing itâs tip (tip is called the last / latest commit of the branch in a repo)
What is Git Revert?
Revert command removes the changes made on a commit, by creating a new commit. The new commit will contain the changes thatâs the reverse of old commit.
git revert
is the preferred command whenever you have to make the changes to the commit which youâve already pushed.
The syntax of revert command is,
git revert <commit_hash>
commit_hash
is the hash of the commit which we want to revert.
We have reverted the top commit from our repo and you can see, a new commit has been created above the old commit saying itâs the revert of that commit.
âCan you please explain one real time scenario for each of the above concepts?â, asked Krish, one of the leading developer in our firm.
Sure.
git checkout
to restore that filegit reset HEAD~1 --mixed
to bring the commit to the working directorygit reset HEAD~1 --soft
which will bring all the changes to the staging areagit reset HEAD~1 --hard
to completely remove that commitgit revert <commit_hash>
to reverse the changes
Raman (little mischievous ) from my team asked me the following,
âRaising a question is simple, but returning the satisfying answer is the toughest job. Can you say the appropriate commands to use for the scenarios you mentioned at beginning?â.
âThatâs the evaluation of your understanding folks. Can you all say the command for each scenario?â, I replied.
To my surprise, everyone has voted the right command for each scenario. Felt happy on seeing the success of my teaching in real time. Thank you folks.
Answers for my valuable readers & followers,
git reset <path_to_file>
git reset HEAD~1 --hard
git revert <commit_hash>
git checkout <file_name>
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.