Explore Courses Blog Tutorials Interview Questions
+6 votes
1 view
in DevOps and Agile by (19.4k points)
edited by

How do I force an overwrite of local files on a git pull?

The scenario is following:

  • A team member is modifying the templates for a website we are working on

  • They are adding some images to the images directory (but forgets to add them under source control)

  • They are sending the images by mail, later, to me

  • I'm adding the images under the source control and pushing them to GitHub together with other changes

  • They cannot pull updates from GitHub because Git doesn't want to overwrite their files.

This is the error I'm getting:

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

How do I force Git to overwrite them? The person is a designer - usually I resolve all the conflicts by hand, so the server has the most recent version that they just needs to update on their computer.

2 Answers

+7 votes
by (27.5k points)
edited by

#WARNING: Performing git clean will delete all your untracked files or untracked directories and this can't be undone. So I'd suggest you to consider using git clean -n (--dry-run) flag first. This command will show you what will be deleted without actually deleting anything:

git clean -n -f -d

Now, follow the steps given below:
#WARNING: This can't be undone.

git reset --hard HEAD
git clean -f -d
git pull

+5 votes
by (29.3k points)

For this question you could use:

git fetch --all

git reset --hard origin/master

OR If you are on some other branch:

git reset --hard origin/<branch_name>

These commands will reset the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master.

If you need to maintain current changes you could use:

git checkout master

git branch new-branch-to-save-current-commits

git fetch --all

git reset --hard origin/master

With this, your local changes will be saved in a new branch named new-branch-to-save-current-commits.

If the changes are uncommitted then you could use:

 however, uncommitted changes will be lost. Make sure to stash and commit anything you need using:

git stash

and if you need to retrieve these changes then you could use:

git stash pop

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories