Update: from the updated question we now know that you are trying to undo a modification you made to an already tracked file in the commit. Note your statement about .gitignore
is misleading, since .gitignore
is for untracked files. Once you are tracking it, you cannot ignore it by using the .gitignore
file.
One way to achieve your goal is to simply undo the change to the file. If the most recent commit on your branch is the one you need to change, then just amend it. If it isn't, then create a new commit with your change which undoes the previous change, and then interactive rebase your branch and squash this new commit into the previous one. This is described in more detail here. If you've never used it, interactive rebase is a fantastic feature of Git, which I recommend everyone learn, even though it is a little daunting at first.
Original Answer (mostly geared towards how to undo a newly added file, which is no longer relevant after the question update):
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:
- 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.
- 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.