How to Remove Modified or Changed Files from a Git Pull Request

Tremaine Eto
Feb 4 · 3 min read
Image for post
Image for post
Original photo by Yancy Min on Unsplash; illustration by Tremaine Eto.

When you’re making pull requests in Bitbucket, GitHub, GitLab, SourceForge, or other Git servers as a services, then often you’ll run into the scenario where unwanted modified files make it into your PR.

Now, the reasons for why you want to remove it are plentiful. It could simply be that you accidentally ran $ git add on the file or on its directory and added it accidentally. It could be that the file has only formatting changes or newlines or spaces that don’t really need to make it in the target branch. It could be that you changed your mind on including the modified file in this particular pull request.

Whatever the reason, the ability to pull the file out of the pull request without removing it entirely from your project is extremely useful and one that you’ll be reaching for again and again — I know I do.

In the spirit of keeping pull requests limited to just important changes and making them more digestible for reviewers, read on for some simple steps on how to achieve this.

Make sure you’re on the source branch

If you just made the pull request, then there’s a likely chance that you’re already on the source branch, and thus you won’t need to perform this step at all.

Once you’re on the source branch, then you can move on to the next step.

Overwrite the file you wish to remove from the pull request

What you can now do is overwrite that file with a different branch. You can do it from the master branch, for instance, to restore it to the state that it once was. To do so, run the following command:

Now, if you run $ git diff, you’ll see that the original file has been restored locally and your unwanted changes from before have been overwritten.

Commit and push to remove the changes in the pull request

Caveats

For one, this will erase your changes since you’re overwriting them in the “Overwrite the file you wish to remove from the pull request” step.

Secondly, this change is tracked in the git history and leaves the previous commit there as well, and some use cases would like that entirely scrubbed.

If these aren’t too much of an issue, though, then I find this is one of the most straightforward and quick ways to clean up a PR of commit(s) you didn’t intend to make it.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store