Home > Back-end >  How do I remove a file from merge request
How do I remove a file from merge request

Time:12-09

I have committed and pushed some files to remote branch and created a merge request. I used

git commit -a -m "blah blah"
git push

So it pushed all the modified files. One of the files was not supposed to be pushed (like for e.g. executables). How do I remove this file from the merge request? I understand that I need to put it into .gitignore so that this doesn't occur in the future. But for now I need to remove the file from the current merge request. I found this page mentioning the following commands

git rm {filename} --cached
git commit -m "[...]"
git push

I tried the commands, but do not see the file removed in the merge request in Gitlab. Is this the right way to do it?

CodePudding user response:

What version of Gitlab are you using?

Just tested with latest version (14.5.2):

git checkout -b feature-123
git add package.json package-lock.json
git commit -m 'Commit with extra file.'
git push --set-upstream origin feature-123

Created MR, it shows two files changed

Now, from the same branch:

git rm --cached package-lock.json
git commit -m 'Removing extra file.'
git push

MR now shows only one modified file.

But if this MR is accepted without squash, the git history will contain adding and removing an extra file.

If you have committed a very large file, it may be better to ask the maintainer to squash your changes before merging. Or delete MR and create a new one with a single commit.

CodePudding user response:

A Merge Request (also called a Pull Request in other SCM tools) is a formal way to code review and merge changes from a source branch (yours) into a target branch (usually a shared branch such as main, master, develop, etc.)

Given that, there are multiple ways to remove a file from a Merge Request, such as:

  1. As you proposed, you can add a new commit to your source branch which effectively undoes the changes to the files you no longer wish to include. This could mean undoing the changes to existing tracked files, or deleting untracked files. The downside of adding an additional commit to remove the file(s), as pointed out in xy2's answer, is unless you squash when you complete the MR, the unwanted files will remain in the history.
  2. Typically the better way is to remove the file from the commit(s) that contained them. This is described in great detail in this question: Remove files from Git commit. After re-writing the commits on your local branch to no longer include the files you wish to remove, you will need to force push your branch again: git push --force-with-lease. (Note --force-with-lease is usually a good default to use over --force.) After pushing your branch your MR should be automatically updated with the latest version of your branch which excludes the files.

Side Note: Even though I would personally lean towards option #2, your described attempt at option #1 should have worked, conceptually. I suspect you didn't do exactly what you think you did to your branch.

  • Related