How to undo "git fetch" for Bitbucket
Home » BLOG » Web development » How to undo “git fetch” for Bitbucket

How to undo “git fetch” for Bitbucket

category:  Web development

I have a situation where I fetch the changes from the remote repo I have been working with. The result is, that I download all changes from the remote master and remote development branch from the remote repo into my current local branch. However, normally the team will work on the development branch only. That means someone accidentally merges the changes into the master branch. Before I merge the changes to my current local branch by using “git merge“, I want to undo the “git fetch” that comes from the master branch first.

How the git fetch command works

A quick summary of how the “git fetch” command works. By default, the “git fetch” command downloads the changes from all branches and tags which are collectively called “refs” from a project (a remote repo) to the local machine. However, the existing local code is not overwritten unless you use the “git merge” command.

Note that, you can use the “git pull” command if you want to fetch and merge at once.

I run the `git fetch origin` command

I normally run the `git fetch origin` command which will download the changes from all branches from the remote repo. However, I should run the `git fetch origin development` command in order to fetch the changes from the development branch only. As I mentioned above, normally the team merges the changes into the development branch. But this time, when I run the `git fetch origin` command I download the changes from the master branch as well. And I don’t want these changes from the master branch. So I want to undo the git fetch I just did.

Undo “git fetch” command

git log

First, we will check which ref we want to update by running the “git log” command. In my case, I run the `git log -g origin/master` command. The -g option means “instead of walking the commit ancestry chain, walk reflog entries from the most recent one to older ones.” I add -g option because I want to undo the changes that I just fetch by the `git fetch origin` command. I also add “origin/master” which is the remote master branch that I want to undo the fetching. Below is the result from the `git log -g origin/master` command.

git update-ref

I want to undo the last fetching I just did. On the screenshot above, I want to update the “origin/HEAD” point to “refs/remotes/origin/master@{1}“. So the command I need to run is shown below.

$ git update-ref refs/remotes/origin/master refs/remotes/origin/master@{1}

Now runs the “git log -g origin/master” command again to see the changes. See the screenshot below.

As you can see in the screenshot after running the “update-ref” command. You can see the “origin/HEAD” point to the ref before the last fetching for the remote master branch now.

Next, I can run the “git merge origin/development” command in order to merge the changes from the development branch into my current local branch. And that’s it for today.