How to delete .orig files after merge from git repository?

Git

Git Problem Overview


Some how .orig files are checked in my git repository during merge, which are now displayed in modified and un-tracked sector. But I don't want this files anymore in my repository. How to do that.

	modified:   Gemfile.lock.orig
	#	modified:   Gemfile.orig
	#	modified:   app/assets/images/bg_required.png.orig
	#	modified:   app/assets/javascripts/application.js.orig
	
	etc...

Any help will be appreciated.

Git Solutions


Solution 1 - Git

Best solution in this case is to keep it simple and get rid of those files independently of git:

cd /your/repo/directory
find . -name '*.orig' -delete 

Alternatively, in Windows/PowerShell, you can run the following command

cd \your\repo\directory
Get-ChildItem -Recurse -Filter '*.orig' | Remove-Item

Solution 2 - Git

Try git clean more info you can find here or here

Solution 3 - Git

you can do:

git config --global mergetool.keepBackup false

For more info, refer to to https://stackoverflow.com/questions/1251681/git-mergetool-generates-unwanted-orig-files

Solution 4 - Git

Unfortunately git clean doesn't work for me because I have *.orig added to my global gitignore file, so they're ignored from clean as well. Even running git clean -x is no good because I don't want all of my ignored files getting deleted. git clean -i is helpful, but really I don't want to have to review each and every file.

However, we can use an exclude pattern and negate the match. Preview with this command:

git clean -e '!*.orig' --dry-run

Then when you're confident, pass the force flag to really delete them:

git clean -e '!*.orig' -f

Based on leorleor's comment

Solution 5 - Git

You can ignore files using .gitignore

Just put *.orig in the list like shown here

https://help.github.com/articles/ignoring-files

for deleting current files you can create shell script and run from project folder like below

for file in `find *.orig -type f -print`
do
   echo "Deleting file $file"
   git rm $file -f       
done

Solution 6 - Git

git rm Gemfile.lock.orig and the rest of the files you don't need. git commit --amend

To completely remove those blobs, git gc --prune=0 --aggressive

Solution 7 - Git

For me git clean -f removed all *.oig files. And keeps the ones that were already there before.

This is what it (almost) looked like after the merge:

$ git status
On branch feature/xyz
Your branch is ahead of 'origin/feature/xyz' by 568 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .idea/codeStyles/
    .idea/misc.xml.orig

.idea/codeStyles/ was already there before the merge as an untracked file. .idea/misc.xml.orig (and a lot more) had to be removed.

=> Using git clean -f:

$ git clean -f
Removing .idea/misc.xml.orig

resulted in:

$ git status
On branch feature/xyz
Your branch is ahead of 'origin/feature/xyz' by 568 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .idea/codeStyles/

Solution 8 - Git

you can simply do git status -s | grep "\.orig$" | awk '{print $2}' | xargs -n1 -r echo rm

TL;DR;

git status -s allows you to get all modified/missing files in respect of the index

grep "\.orig$" filter out files, keeping the ending by ".orig"

awk '{print $2}' lists file name (skipping git information e.g. A, M, ??)

xargs -n1 -r echo rm prints remove commands on every arguments (-n1: one at a time and -r: skip when empty) just copy&paste the commands double-checking for files to remove.

Solution 9 - Git

You can automate this process with pre-commit hook. To do so, just go inside you project folder, and search for hidden folder .git. Inside it you will find hooks directory, open it and cr8 a file named pre-commit with the next contains:

echo "Looking for .orig files to remove"
pwd=$(pwd)
find . -name '*.orig' -delete 
echo "Done removing .orig files"

Now it will cleanup orig files always before you committed something, so no necessary to keep it in gitignore.

Solution 10 - Git

For those of you on a windows machine, you can execute this command in PowerShell.

ls -r *.orig | rm -f

Or if you prefer verbose PowerShell

Get-ChildItem -Recurse *.orig | Remove-Item -Force

Solution 11 - Git

git rm <file>

http://git-scm.com/docs/git-rm

Also add files/directories you want to ignore to your .gitignore file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmaximus ツView Question on Stackoverflow
Solution 1 - GitTheChymeraView Answer on Stackoverflow
Solution 2 - GitAmarView Answer on Stackoverflow
Solution 3 - GitrowanView Answer on Stackoverflow
Solution 4 - GitJeff PuckettView Answer on Stackoverflow
Solution 5 - GitLoken MakwanaView Answer on Stackoverflow
Solution 6 - GitlinquizeView Answer on Stackoverflow
Solution 7 - GitUeffesView Answer on Stackoverflow
Solution 8 - GitfiorentinoingView Answer on Stackoverflow
Solution 9 - Gitswift2geekView Answer on Stackoverflow
Solution 10 - GitKellen StuartView Answer on Stackoverflow
Solution 11 - Gitveritas1View Answer on Stackoverflow