How to re-download whole repository in Git

10,586

Solution 1

No, there is no way to tell git to change all your files for you. Git simply stores the files. But you can clone your repo, update your files locally, and then push the changes back.

From a unix-style shell you could use the commonly available tool dos2unix for easy conversions. Something like this might fly:

git clone bitbucket:path/to/repo foo
cd foo
find . -type f -name \*.sh -exec dos2unix {} {} \;
git add .
git commit -m 'convert line endings on .sh files'
git push

If you can't find dos2unix, you can duplicate the functionality with a number of other tools, for which answers are readily available already here or via your favourite search engine.


UPDATE

If what you're really asking is "how do I refresh a local copy of a repo, overwriting mis-matched files", then I believe the following may work:

git fetch --all
git reset --hard origin/master

Or if you're on a different branch:

git reset --hard origin/branchname

The git fetch re-downloads the latest from remote without a merge or rebase (which would be done if you were to git pull).

The git reset will reset the master branch to whataver was just fetched. The --hard option changes all the files in your working tree to match the files in origin/master (or whatever branch you specify).

NOTE: This overwrites local files. Make sure to stash any local changes, as this will overwrite anything that hasn't yet been pushed. Files that are not in the repo will be left untouched, but anything that is in the repo will be overwritten with whatever is fetched, and local (unpushed) commits will be lost.

Solution 2

I would go for grep + xargs + sed

grep -rlI "\r\n" * | xargs -I{} sed -i.bak -e 's/\r//g' {}

grep will list non binary file that have \r\n and sed will remove the \r

I didn't try, so it may need a bit of tweeking!

Share:
10,586
Michal Krasny
Author by

Michal Krasny

Things are either already broken or haven't been finished yet. All other situations are just rare exceptions to this rule.

Updated on June 04, 2022

Comments

  • Michal Krasny
    Michal Krasny almost 2 years

    We are team of users, who work on Windows, our Remote is in Bitbucket (Linux/UNIX) and our application is being deployed to Linux machine. We didn't pay attention to line endings, until one day we found out, that .sh scripts on our laptops have CRLF line endings. We decided to set core.autocrlf to false, so there will be no differences between line endings on Remote and line endings on our Windows laptops. However, this option does not change CRLF in our local source code.

    Is there any way, how to tell Git to update all files so CRLF will be changed to LF as it is on Remote? Some kind of re-download, that would download even unchanged files.

  • ghoti
    ghoti about 8 years
    Note: only works for GNU sed. Not sure what version of sed the OP has installed in his Windows box. This may also break if any of the filenames have special characters like newlines or spaces.
  • Michal Krasny
    Michal Krasny about 8 years
    Thanks for the answer. However I don't need to change the files and push them to remote, they are already with correct line endings on remote. The only problem is, that these files are on currently cloned local repositories with CRLF. I asked if there is an option that would download them with LF after core.autocrlf is set to false.
  • ghoti
    ghoti about 8 years
    @Michal - ah, I see what you mean. Updated my answer with what I think you're looking for.