Free cookie consent management tool by TermsFeed Generator undo git merge the easy way and then undo the undoing! (video) | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
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

git branch workflow: 7 easy steps (video)
Nov 27, 2022 programming

git branch workflow: 7 easy steps (video)

Transcription Hello This is amir Today we are going to talk about git branching workflow I will go through it step by step and help you understand the process ...

9 Min Read Read More
How to use Git Alias to increase your productivity (video)
Nov 27, 2022 programming

How to use Git Alias to increase your productivity (video)

Today we are going to talk about Git Alias and how to increase your productivity by adding shorter aliases for commands that we use everyday. ...

8 Min Read Read More
how to use git restore and completely ignore the changes: 3 scenarios (video)
Nov 27, 2022 programming

how to use git restore and completely ignore the changes: 3 scenarios (video)

we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore ...

9 Min Read Read More
5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)
Nov 27, 2022 programming

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)

5 commands to interact with and online git repository ...

13 Min Read Read More
the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...