How to force local files to be overwritten during git pull?

I have the following script:

  • One of the colleagues makes changes to the templates of the website on which we work.
  • He adds several images to the appropriate directory (but forgets to add them to the version control system).
  • Then he sends me these images by mail.
  • I add the images to the version control system and send them to Github along with other changes.
  • An employee cannot update his version of a project from Github, because git does not overwrite its local files.

The error looks like this:

error: Untracked working tree file 'public / images / icon.gif' would be overwritten by merge.

How to make Git overwrite local files? The mentioned employee is a designer, and usually all conflicts are resolved by me manually: I watch the server to have the latest version of all files, that is, my colleague has to update local files on his computer.

Translation of the question “ Force Git to overwrite local files on pull@Jakub Troszok .

  • I add images to the version control system - a bad decision. Blobs should not be placed under the control of source control versions. - aleksandr barakin
  • @alexanderbarakin I would be extremely grateful if you make recommendations in the form of another answer or a separate question with an answer! - Nicolas Chabanovsky
  • association: stackoverflow.com/questions/1125968 - Nicolas Chabanovsky

3 answers 3

Important: Changes made by you locally will be lost. Both with the --hard option and without it, any local commits that are not sent to the server will be lost. [*]

If you have unchecked local files (for example, uploaded by users), they will not be affected.


Most likely, the correct decision in this case would be:

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

Or if you are in another branch:

 git reset --hard origin/your_branch 

Explanation:

The git fetch command loads the latest version of the files from the remote repository without attempting to merge or synchronize anything ( merge or rebase ).

Then git reset designate as the main branch the one you just updated. The option --hard changes all files in your work branch according to the files in origin/master


[*] : It is important to note that it is possible to keep local changes up-to-date if you create a separate branch from master before git reset :

 git checkout master git branch new-branch-to-save-current-commits git fetch --all git reset --hard origin/master 

After that, all old versions will be stored in new-branch-to-save-current-commits . Unconfirmed changes, however (even indexed), will be lost. Therefore, index and add to the local repository everything that you might need.

Translation of the response “ Force Git to overwrite local files on pull@RNA .

  • ... будут утеряны любые локальные коммиты, не отправленные на сервер - a lie. All source commits will remain and be available via git reflog . - ߊߚߤߘ
  • one
    @Arhad ... until they are eaten by git gc (which sometimes starts), because these are "lost" ones that are inaccessible when crawling from tags and branches. There are few guarantees that they will remain. - D-side

The easiest option ("for the designer"):

  1. delete files about which the pull command wrote the above message.
  2. repeat pull command.

    If you take the axiom that an employee will never make friends with git, and you should not always overwrite files that have not been added, then I would advise him to add everything to the ignore list locally.

    To do this, edit it in the local copy in the .git / info / exclude file. Ignored files, like, frayed without question.