In this lecture we are going to see how to rollback.
We'll see each and every command how to roll back.
But before that, let's ask ChatGPT how to rollback Git.
Rollback commands.
Okay.
So it is saying you can use a combination of git reset, git revert and git checkout.
And it is showing you the command and the information about it.
Don't worry, we are going to do this and learn it.
But I wanted to show you ChatGPT in case you forget the command or you forgot the the method.
Then you can anytime use ChatGPT and ask it or you can give specific command also and ask it to explain.
So you can use git reset.
You can get use git reset hyphen hyphen.
R.
We're going to use both of this.
We're going to also use git revert and git checkout.
What does that mean?
We know git checkout switches between branches, but there's also one more nice use case of git checkout
for rollback.
We'll be seeing that as well.
And actually a few more ways that we will be seeing in this lecture.
But any time you feel that you're not doing it right, just check with ChatGPT.
Is your command correct or not?
Ask questions?
Wiggles.
Explain git reset hyphen f and hard with an example.
As you see, it's explaining you A, B, C, d.
This is your branch.
Or the commit history.
Sorry.
Right.
But all this you will understand when you actually do it right.
In case you don't remember, you can come to ChatGPT and confirm it once again.
Because when you are doing versioning, you need to be absolutely sure what you're doing.
You have to because you have to push it and share it with your fellow DevOps engineers and developers
as well.
And sure, we can roll back whatever mistakes we do, but I think the ChatGPT is really a very good
tool to confirm the commands that we are running and we are doing it right or wrong.
Right.
So let's let's do it ourself and see how we can roll back the changes.
Okay, now we are going to see how to roll back.
And there are different different levels where we can roll back.
First, let's edit any file.
And I'll just put some content over here.
Okay.
I've put some content over here.
Okay, Now I want to roll back this before even I get into the staging area so I can say git checkout
and I can give the file name.
All went back to how it was.
So git checkout and the file name is going to just roll back if you made the changes have and have not
gone into staging or commit area.
Okay, let's do it once again.
Okay.
Again, few more lines.
Actually, I'm taking things from some existing playbook.
You can put any content you want.
Okay.
You can see git status.
There is also one more command.
Git.
Diff.
Okay.
Git diff can show you the difference in the file that you have changed.
Right.
So Jupiter one point file.
It is saying that there are these lines that were added right now, if I'm good, I can continue.
Or if I'm not, I can say git checkout and I can roll back.
Okay, so let's say I'm good.
All right.
So you can do git status, right?
Which says there is a change.
And then you can say git diff where you can see what exactly is the change.
If you're good, you're going to say git add, index it oops space and you can then again check git
status.
And you can say git diff, but just git diff command is not going to work.
Now you have to give an option.
Git diff hyphen hyphen cashed.
So it's going to do the check.
The difference, the file that was staged.
So if you have already staged it, you want to see the difference from the current commit and the next
that is going to happen.
Git diff hyphen.
Hyphen cashed.
All right.
If you want to go back from this level, you can say git restore.
Hyphen hyphen staged and you can give the file name.
Okay.
Now let's check git status which is modified.
Git diff.
All right.
So back before we are staging.
Right at every level you can roll back first level git checkout and the file name when you staged then
git restore.
Git restore hyphen f1 staged and the file name.
Okay.
That is from the staging area.
But what if you have already committed?
So now let's again do git add.
And say git.
Commit now.
Git commit hyphen m.
Okay.
Now git status.
Everything is good.
Git diff.
Nothing.
Git diff cached nothing.
Things are already committed so git log hyphen hyphen one line get commit id and then you can check
the difference between the current commit that you did.
Git diff.
You can give the previous commit.
Dot dot and the current commit.
And then you can see the difference.
Now, if you want to roll back from here, if you want to go back to your previous comment, you can
just say git revert.
Head.
So head is that pointer which is currently pointing to this commit ID.
And if I say git revert head so it's going to go to the previous commit or I can even mention the commit
ID git head and I can mention the commit and at that level the content will go.
It will also ask me a message for the reverting like the commit message.
So now roll back.
Okay.
Get Jupiter one dot.
See, there is nothing.
But now if you do git log, hyphen, hyphen, one line, you should actually see a new commit.
Okay.
That is for revert, which is as similar.
The content here is as similar as this one.
Right.
So we are here.
We made some changes.
We came at this place, then we wanted to roll back over here, but it created a new commit.
So when you revert it is also going to store the history of what changes you made.
But if you don't want to store any history whatsoever, then you can use git reset command.
Git reset hyphen hyphen hard and you give the commit ID.
Now here you go back to that particular commit ID and all the commit ID After that, all the commit
history after that is removed, the data and all the history is removed.
So this is a more direct way of rollback, right?
Git revert is a softer way where you are storing the history also.
Okay.
So let me run the history command when I said history.
Okay, let's talk about now reverting and rollback.
So we made changes to this file.
And if we want to roll back before I stage, I can use git checkout and the file path.
Okay.
Can roll back.
But if you have already staged the change.
Now you can compare with git diff.
If you already staged, you can say git diff hyphen f and cached.
If you want to restore from after you have already staged and then you want to restore, then you can
say git, restore hyphen staged and the file name.
But if you have already committed and you want to roll back, then you can use git revert or git reset.
So based on your requirement, what you want to do.
Right, But make sure you use git diff command to really check what are the differences between two
commits.
Also, you want to see something is not working.
Now you just made some code change.
Things are breaking.
You want to see the difference from the previous commit.
Use git diff.
It's a very useful command.