-1

I am facing this issue because of my own negligence.

Scenario:

I have two branches master and branch1. From start they don't have any .gitignore file (that is my big fault). Now the status of branch1 is

This branch is 12 commits ahead, 4 commits behind master.

Whenever I pull master into branch1, I have conflicts in many files which should be in .gitignore file (say *.unwantedFileExtensions). I tried my best to solve the conflicts but when ever I try to build the progam it throws exceptions/errors.

After that I explicitly added .gitignore file into both branches to ignore all *.unwantedFileExtensions files, still I face same conflict files while merging.

I am pretty much stuck here. Do anyone know how to solve this issue ?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Junaid
  • 2,572
  • 6
  • 41
  • 77
  • Let me know why my question is down-voted ? – Junaid May 05 '18 at 05:07
  • 4
    You have to also remove the files. They are already tracked by git, so just putting them in .gitignore is not enough. Put them there, remove them, commit both branches, and then merge. – Martin G May 05 '18 at 05:08

2 Answers2

1

Checkout each branch, add the files to ignore in .gitignore, also delete these files. Then stage and commit .gitignore and the removed files in each branch.

After that you can merge the two branches and there should no longer conflicts for the unwanted files.

Thus:

git checkout master
rm -rf *.unwantedFileExtensions
echo "*.unwantedFileExtensions" >> .gitignore
git add .gitignore *.unwantedFileExtensions
git commit -m "removed unwantedFileExtensions"

git checkout branch1
# repeat the steps above from branch master for this branch
git merge master
milbrandt
  • 1,438
  • 2
  • 15
  • 20
0

gitignore "specifies intentionally untracked files to ignore."

Your files are already tracked; gitignore doesn't work retroactively. It only ignores new files.


First pull in the conflicts, merge them as you see fit, and then commit the merge.

  • git pull
  • Fix up files to your liking based on conflicts (open in text editor of your choice)
  • Add and commit the merged files git commit -a

Now that we're up to date with master, let's make git forget about those ignored files. Git will think those files are removed but they'll still be available locally. We'll then commit these files no longer being tracked.

  • commit/stash any changes to non-ignored files
  • clear the index/staging area (completely) git rm -r --cached .
  • add everything back (files in .gitignore will be ignored!) git add .
  • Now the removal of those files from git's index (but not from our working directory) needs to be committed git commit -m "Fix untracked files"

Now branch1 is up to date with master and ignores the files properly while still retaining them locally.

JBallin
  • 8,481
  • 4
  • 46
  • 51