undo git merge the easy way and then undo the undoing! (video)

undo git merge the easy way and then undo the undoing! (video)

Last Updated on Nov 27, 2022

https://youtu.be/qa2aTaMul8E

Transcription

Hello

This is amir

Today we are going to talk about how to undo git merge and then we talk about how to undo the undoing

Preparation

So let’s get started

Here i have a project with one file called 1.txt

In this file in the master branch it says hello amir

But then I wanted to change the name so I created a new branch and changed the name to json

It’s finalized and I want to merge it to the master branch

if you want to learn about git branching workflow I have another video, you can check that out

First we go to the master branch

And then we merge the other branch

The name has changed

And we can see the logs by running the command

Git log

Great.

Undo git Merge

Now for any reason I have decided that oh I think amir is better than the name json for my project so I want to undo the merge

Here it’s a very simple example but in a real project, that branch might have taken days to build. It might have been a new feature and now we have decided that we don’t want the feature and we want to undo the merge

We can do that in 2 easy steps

Step one (git log)

First check the log to copy the head right before that merge

git log

Step Two (git reset)

Then run this command

git reset --hard HEAD

Now as you can see the name is back to amir

Great

When shouldn’t we undo the merge

Before we continue I want you to know that before undoing the merge, make sure that you haven’t pushed the merged project to a public repository where other developers are also working on the project. If you have pushed it then undoing the merge is not recommended and you should find another way.

Unde the Undoing!

now it’s been a while and I thought about it and I realized the name json was better.

Now I want that back

I want the feature

So I have to unde the undoing of git merge

I want to go back to the state were the project was merged and name was json

And in order to do that

We should use the same command

Git reset --hard HEAD

But what is the address of the head we want to reset to?

it has already deleted the commits from the other branch

So what should we do?

Instead of running

git log

We should run

git reflog

In git reflog nothing is really deleted

We can see its history here

And as you can see here is the one that we merged then we undo that merged

We want to go back to that merged history

So we’ll get this as the address of our head

Now we run the command again

Git reset --hard HEAD

And let’s check if the name is back to json

It is

perfect.

Key Takeawys

Just a quick recap

  • We talked about undoing a git merge Using git reset command
  • And then we also checked git reflog to be able to undo the undoing and go back to the history were our project was still merged

Final Words

And finally

if you liked what you learned please like and subscribe

That would be a great motivation to do more videos

If you have any suggestions or questions please leave a comment below and I try to answer them as soon as possible

That’s it

Have a wonderful day.

You can watch the original video on youtube.

Category: programming

Tags: #git

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Courses