7

I have committed and pushed some files to remote feature 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 (which is an existing file in in the master) was not supposed to be pushed (like for e.g. executables). How do I remove this file from the merge request, so that when the MR is merged into master, the unwanted file is not merged (as if it was never there in the MR). 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?

Update1:

With the above commands, I could see the file mentioned as deleted in the above commit. But then when I merge the updated MR into master, the file is deleted from master too.

Update2:

Removed sentences causing confusion and updated the title

ontherocks
  • 1,747
  • 5
  • 26
  • 43

3 Answers3

9

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.

xy2
  • 484
  • 2
  • 9
  • In my feature/test branch I had pushed only one file `README.md`. I removed the file following your commands. I can see under `Changes` in the merge request `README.md deleted`. So now if I Merge with `Squash commits when merge request is accepted` into the master branch, I should see no changes going in the master branch...right? – ontherocks Dec 08 '21 at 17:23
  • Yes, you will see a single commit with the final result of all changes made to your branch. – xy2 Dec 08 '21 at 19:09
  • @ontherocks if you're testing this with only 1 file change and removing that 1 file, you should see no changes, and the MR should have no file changes (which you should be able to see before you complete it). If you complete it and squash, it should do nothing (or possibly create an empty commit with no changes). But if you *don't* squash, you'd be adding two commits to the target branch with a total of no changes- the readme file would still be in the history as an add followed by a delete. – TTT Dec 08 '21 at 21:24
  • @TTT Unfortunately, after merging into the master, the `README.md` file from master was gone :( – ontherocks Dec 10 '21 at 19:48
  • @ontherocks ok- that's because the original question had us barking up the wrong tree. :D The update makes it clearer now. – TTT Dec 10 '21 at 21:23
1

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:

  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.

TTT
  • 22,611
  • 8
  • 63
  • 69
  • Reading the other question for option #2, what I could gather is `git reset --soft HEAD^` followed by `git commit -c ORIG_HEAD` followed by `git push --force-with-lease` and then merge the updated MR into master – ontherocks Dec 10 '21 at 20:12
  • @ontherocks that's close, but you'd still need to unstage the file in question after a soft reset, and then re-commit. This only works if it's the most recent commit, and if so, amending it may be slightly easier. I just updated the answer to reflect your question update. – TTT Dec 10 '21 at 21:21
  • So putting it in simple words, there is no way to remove a tracked file from MR. Just need to undo the changes (i.e. make it identical to the one in master) and let it be part of MR. Then when you merge it into master, the file is essentially replacing itself in the master. – ontherocks Dec 11 '21 at 03:57
1

Access your branch from the web interface and then you can view the files and folders, click on the file you want to delete and hit the Delete button to remove the file from the branch, the changes will be automatically reflected in the merge request. enter image description here

Chikku Jacob
  • 2,114
  • 1
  • 18
  • 33
  • Which branch are you talking about? If its the feature branch, I don't want the file removed from feature branch. I just want it to be removed from the MR. – ontherocks Aug 02 '23 at 15:03