186

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 would be appreciated.

TheChymera
  • 17,004
  • 14
  • 56
  • 86
maximus ツ
  • 7,949
  • 3
  • 25
  • 54
  • Possible duplicate http://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy – ziu Sep 11 '12 at 09:01
  • @ziu, I want to remove files that are already present in my repository, checked in by git merge or by some other developer – maximus ツ Sep 11 '12 at 09:16
  • 1
    I missed the part about checked-in files. However, the .orig files are written by mergetool. It sounds to me as a non-clean conflict resolution. – ziu Sep 11 '12 at 09:41
  • 1
    Do you need to completely remove the file from the whole history? – linquize Sep 11 '12 at 09:42
  • 1
    check this out http://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history – ziu Sep 11 '12 at 09:47

11 Answers11

312

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
Julian
  • 20,008
  • 17
  • 77
  • 108
TheChymera
  • 17,004
  • 14
  • 56
  • 86
  • 14
    If you get an predicate error use: find . -name '*.orig' | xargs rm -f – Neil May 14 '15 at 13:08
  • Just as a warning, using 'find -delete' can be dangerous. Please check that the directory (in this case '.') is correct to make sure that you are only deleting what you want to delete – kingledion Feb 26 '17 at 18:21
  • What about Windows? – Panzercrisis May 17 '18 at 13:50
  • 3
    @Panzercrisis if you are using Windows you will probably already have a terminal emulator via which you interface with Git. I would recommend "gitforwindows" which contains BASH with all other commonly needed tools. – TheChymera May 22 '18 at 13:22
  • 1
    Don't accidentally run `find . '*.orig' -delete`... it deletes your whole repository; ask me how I know – Kellen Stuart Apr 06 '22 at 16:11
  • 1
    I google for and come back to this answer at least once a month, whenever I've just done a big messy merge and have a bunch of .orig's laying about. – kris May 11 '22 at 02:25
163

Try git clean more info you can find here or here

Amar
  • 6,874
  • 4
  • 25
  • 23
  • 1
    I can clean untracked files, but what about removing such files that are already in the repository. – maximus ツ Sep 11 '12 at 09:15
  • you have to first remove file from repository `git rm ` and then commit.after this add *.orig file into git ignore. – Amar Sep 11 '12 at 09:18
  • Thanks @Amar, its working, but problem now is that git shows these files are deleted and I don't want to commit this. – maximus ツ Sep 11 '12 at 09:35
  • 4
    I think this is a better solution than the one marked as answer. – Orgmir Mar 26 '14 at 11:52
  • 60
    `git clean -i` is really nice. it will give you a list of files that will be deleted and give you options on how to deal with the list (filter the list, being able to delete everything on the list, and some other options). – PPPaul Aug 04 '14 at 10:18
  • 2
    You can use git clean with patterns to help delete only files the *.orig files: `git clean -f -e '*.*' -e '!*.orig'` There is only an exclude pattern, but it has a little support for negative patterns. So we can exclude all files `*.*`, and then negate the exclusion of just the `*.orig` files. – leorleor Jan 29 '15 at 20:58
  • @PPPaul That's a great tip, thanks! … one thing it didn't do was to consider removing a modified ".orig" file that I had. I needed to get rid of that one a different way. Is there some other flag I can add that will do this? – Benjohn Oct 20 '15 at 14:32
  • clean was created for this, but it also removes all the untracked files (including the ones you just created but didn't add yet). This just cost me an hour's worth of new files :/ but this might be because my usage pattern (doing my changes in an IDE and then calling: `git add . && git commit`) is non-standard. – Marcin Dec 15 '15 at 02:04
  • you need git clean -f – Yohanim Sep 01 '20 at 15:42
129

you can do:

git config --global mergetool.keepBackup false

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

Community
  • 1
  • 1
rowan
  • 1,293
  • 1
  • 8
  • 6
14

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

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
8

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
Loken Makwana
  • 3,788
  • 2
  • 21
  • 14
  • 5
    Though he certainly can do this, it's not the right thing to do here :) – Sergio Tulentsev Sep 11 '12 at 09:03
  • 1
    I thinks it's a good idea as it keeps the backup but doesn't interfere staged files – akardon May 24 '16 at 02:47
  • @akazemis - you'd think so, but when you are grepping for code you see twice as many search results as you want (which gives you a false perception of what is in your latest code). It's so rare that I'd rather just live with the danger of not having orig files. It's better just to commit regularly. – Sridhar Sarnobat Apr 13 '18 at 00:03
  • @SridharSarnobat You can ignore those files during grepping – Smart Manoj May 01 '22 at 12:19
3

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

linquize
  • 19,828
  • 10
  • 59
  • 83
  • @linquizebut commit of git rm Gemfile.lock.orig will go in last commit using amend, right? is there any other way to not to have any commit for such file? – maximus ツ Sep 11 '12 at 10:45
2

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/
Ueffes
  • 168
  • 12
  • 2
    Make sure you've added files using git add that you need before running this command. I accidentally ran this command and it removed all the files (which I wanted) that were not added to git – SanjoS30 Aug 13 '21 at 20:07
1

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
Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
0

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.

fiorentinoing
  • 948
  • 12
  • 20
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Nov 09 '18 at 09:46
0

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.

swift2geek
  • 1,697
  • 1
  • 20
  • 27
-4

git rm <file>

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

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

veritas1
  • 8,740
  • 6
  • 27
  • 37
  • `git rm ` Just list them. – veritas1 Sep 11 '12 at 09:20
  • Then you need to list all the files, or use a tool like find to pass them to git rm, or use a shell glob and loop over them. Git does not duplicate this standard Unix command-line functionality. – mabraham Dec 03 '13 at 06:41
  • 1
    orig files are not added to the repository, so there's no need to use `git rm`, just use `rm` instead – Edson Medina Nov 07 '14 at 14:35