1

I am about to merge my developer branch to my master on gitlab. I commited by mistake a lot of files that I didnt wanna have on my master (they are about 150 files). I have created a merge request, but havent merged the branches yet. Is there any way to not merge all these 150 files and merge only the ones I want? Is there maybe any way to add a .gitignore file while merging? I am new to git and cant really find my way around this problem.

Thanks in advance for your help!

hispanicprogrammer
  • 367
  • 3
  • 6
  • 22

2 Answers2

1

I would recomend making a new branch to try edits first so you don't mistakenly remove something permanently from the branch you have been working on. You can use,

git checkout -b <new-branch>

Any uncommited changes will come with it so you may want to make sure you are up to date before doing so. Then try the following before merging into master.

Check out this answer, you can remove a single file,

To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename

Or remove all files in your updated gitignore,

To untrack every file that is now in your .gitignore:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

jmb2341
  • 198
  • 1
  • 5
  • The changes have been commited, so there is nothing in the staging area. – mfnx Jan 27 '20 at 16:30
  • Thats okay, push your `.gitignore`, then follow the steps to untrack all the files that match in your `.gitignore` setup. When you create the commit it will tell you the files that will be deleted from your repo. They should still remain locally however. – jmb2341 Jan 27 '20 at 16:55
  • And they'll remain in the previous commits on the remote. – mfnx Jan 27 '20 at 17:02
  • You are correct. If there is a security or commit cleanliness issue then my answer is not the way to go. The commits should be removed as you suggest. My answer however will remove the files from the most current commit on the remote branch to be merged into master. – jmb2341 Jan 27 '20 at 17:14
0

As I understand it, you did a commit including a bunch of unwanted files on your developer branch. I will assume you did this in your last commit. You can simply return to the previous commit with the git --soft command:

# we are on branch `developer`
git reset --soft HEAD~1

Now you are back to the previous commit, but still with all the changes in the staging area (thanks to the --soft flag). But, your 150 unwanted files are also stilll in the staging area (use git status to monitor that). So, you did not loose any changes that actually matter, but we need to remove the 150 unwanted files from the staging area.

Now you have several options. You could unstage each one of the 150 files like this:

git reset -- path/to/unwanted/file001

This is not a pleasant job though, especially if you have to repeat this 150 times. You could simply unstage all your files:

git reset

and stage the files you want to commit to the developer branch:

git add path/to/wanted/file01

A third option would be to unstage all the files with git reset, edit your .gitignore file, and finally add the changes to the staging area with:

git add --all

Your .gitignore should then prevent the 150 unwanted files from being added to the staging area.

So, summarizing:

git reset --soft HEAD~1 # or HEAD~N if you created the unwanted files N commits ago
git reset
/* edit your .gitignore file */
git add --all
/* commit the results */
mfnx
  • 2,894
  • 1
  • 12
  • 28
  • Hey thanks for your answer! No all these files were not commited on my previous commit. They have been commited already from the first commit – hispanicprogrammer Jan 27 '20 at 16:08
  • @hispanicprogrammer Then you can do git reset --soft HEAD~N where N is the number of commits you need to go back. – mfnx Jan 27 '20 at 16:26
  • but if they were commited from the first commit then I basically have to start from scratch? – hispanicprogrammer Jan 28 '20 at 08:06
  • @hispanicprogrammer how do you mean start from scratch? just reset and commit. I'll add a summary at the end of the answer. – mfnx Jan 28 '20 at 08:17