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

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


Solution 1:[1]

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:[2]

Try git clean more info you can find here or here

Solution 3:[3]

you can do:

git config --global mergetool.keepBackup false

For more info, refer to to Git mergetool generates unwanted .orig files

Solution 4:[4]

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:[5]

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:[6]

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:[7]

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:[8]

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:[9]

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:[10]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julian
Solution 2 Amar
Solution 3 Community
Solution 4 Jeff Puckett
Solution 5
Solution 6 linquize
Solution 7 Ueffes
Solution 8
Solution 9 swift2geek
Solution 10