0

I want git to sync and overwrite all files that are on the local machine which are also present in the repository. I don't want git to remove any local changes to the same directory though so e.g.:

repo files:

folder_a/ contains: file_a, file_b, file_c

local files:

folder_a/ contains: file_a, file_b, file_c and file_d

Now I want git to replace all files locally with those from repo, but don't touch file_d.

2
  • have you added file_d ot git index or is it untracked?
    – pratZ
    Commented Mar 22, 2015 at 19:21
  • it is untracked. This file should also never be uploaded.
    – Bent
    Commented Mar 22, 2015 at 19:22

3 Answers 3

1

To make your local branch look exactly like the remote,

git fetch origin

Then, in the branch, you want to overwrite,

git reset --hard origin/<branch-name>

You will not lose the untracked files.

UPDATE:

Assuming the files were added to the index. You can recover your deleted files (if the garbage collection has not happened). Just run,

git fsck --cache --unreachable $(git for-each-ref --format="%(objectname)")

This will show the list of all the unreachable objects.

Checking object directories: 100% (256/256), done.
Checking objects: 100% (195/195), done.
unreachable blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391

Create files back from all the listed blobs,

git show e69de2 > filname
2
  • This makes me loose all untracked files, they are deleted
    – Bent
    Commented Mar 22, 2015 at 19:46
  • That is not possible. I hope you got me right when i said untracked. I didn't mean unstaged. Untracked files are those which are not present in the git index. So, there is no point for someone to lose "untracked" files with git reset --hard
    – pratZ
    Commented Mar 22, 2015 at 20:04
0

If you just want overwrite local changes of files that are present in repository, but keep new files untouched. You can use git checkout for it.

Here all local changes in files tracked by git will be overwrite with your last commit:

git checkout -- .

You can discard changes in just one file too, passing its name for git checkout:

git checkout -- file_a
1
  • Thank you, I wan't to update older files and configs with those from the repo but I don't want to loose save files that are generated by the program. I hope this makes it clearer. From what I see, your answer would overwrite all local changes.
    – Bent
    Commented Mar 22, 2015 at 19:07
0

First do a

git stash 

then a

git pull --rebase

then do a git stash pop

This will give you merge conflicts,accept the remote files for the cases you want,your new files will be preserved.

Not the answer you're looking for? Browse other questions tagged or ask your own question.