Categories
git git-fetch git-pull overwrite version-control

How do I force “git pull” to overwrite local files?

[*]

8801

How do I force an overwrite of local files on a git pull? My local repository contains a file of the same filename as on the server.

error: Untracked working tree file ‘example.txt’ would be overwritten by merge

10

  • 38

    anyone reading this who thinks they might lose files, I’ve been in this position and found Sublime Text’s buffer has saved me – if I’m working on something, then accidentally delete everything by trying to solve a similar problem to this or by using an answer on this question and have had the files open in Sublime (which there’s a good chance of) then the files will still be there is Sublime, either just there, or in the undo history

    Jan 20, 2016 at 8:51


  • 229

    git reset --hard origin/branch_to_overwrite

    Mar 22, 2016 at 8:37

  • 2

    basically, only do a pull from develop after the initial checkout -b. do your work, then push back in.

    – ldgorman

    Aug 22, 2018 at 9:09


  • 3

    Short answer: delete and re-create branch. 1. Delete branch: git branch <branch> -D 2. Reset to a commit before the conflict: git reset <commit> --hard 3. Re-create the branch: git branch <branch> 4. Set tracking to the server: git --set-upstream-to=origin/<branch> <branch> 5. Pull: git pull`

    Sep 24, 2018 at 8:54

  • 4

    To change all CRLF to LF endings, (start clean) git config core.autocrlf false; git ls-files -z | xargs -0 rm; git checkout .

    – Chloe

    Jan 17, 2019 at 2:46


12246

⚠ Important: If you have any local changes, they will be lost (if they’re tracked). Also, with or without --hard option, any local commits that haven’t been pushed will be lost.[*]

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.


First, run a fetch to update all origin/<branch> refs to latest:

git fetch --all

Backup your current branch:

git branch backup-master

Then, you have two options:

git reset --hard origin/master

OR If you are on some other branch:

git reset --hard origin/<branch_name>

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets 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


Maintain current local commits

[*]: It’s worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:

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

After this, all of the old commits will be kept in new-branch-to-save-current-commits.

Uncommitted changes

Uncommitted changes, however (even staged), will be lost. Make sure to stash and commit anything you need. For that you can run the following:

git stash

And then to reapply these uncommitted changes:

git stash pop

35

  • 28

    Watch out! If you have local unpushed commits this will remove them from your branch! This solution keeps untracked files not in the repository intact, but overwrites everything else.

    May 17, 2012 at 8:18


  • 547

    It’s a popular question, so I’d like to clarify on the top comment here. I just executed commands as described in this answer and it hasn’t removed ALL the local files. Only the remotely tracked files were overwritten, and every local file that has been here was left untouched.

    – Red

    Nov 22, 2012 at 10:38

  • 31

    in case you’re pulling from a repo that has its remote branch name different from “master”, use git reset --hard origin/branch-name

    Dec 17, 2013 at 11:17

  • 201

    Given the amount of upvotes to this question and answer, I think that git should incorporate a command like git pull -f

    Aug 26, 2014 at 1:33

  • 16

    Commits that weren’t pushes before the hard reset can be recovered using git reflog, which list all commits, also those without a base. Until you cleanup your local copy using git gc, then all is lost

    – Koen.

    Feb 10, 2015 at 22:24

1197

This will remove all uncommitted changes and then pull:

git reset --hard HEAD
git pull

11

  • 25

    I’ve done this and some local files that were no longer in repo were left on the disk.

    Apr 8, 2011 at 16:00

  • 34

    I do not think that this is correct. the above will perform a merge, not overwrite which was requested in the question: “How to force git to overwrite them?” I do not have the answer, I am currently looking for it.. at the moment I switch to the branch with with the code that I want to keep “git checkout BranchWithCodeToKeep”, then do “git branch -D BranchToOverwrite” and then finally “git checkout -b BranchToOverwrite”. you will now have the exact code from BranchWithCodeToKeep on the branch BranchToOverwrite without having to perform a merge.

    – felbus

    Jul 13, 2011 at 10:11


  • 287

    instead of merging using ‘git pull’, try git fetch –all followed by ‘git reset –hard origin/master’

    Feb 21, 2012 at 14:56

  • 8

    yep, the @lloydmoore solution worked for me. Could do with being an answer rather than just a comment.

    Nov 19, 2012 at 9:54

  • 4

    This will reset the current changes back to the last branch commit pulled. Then git pull merges the changes from the latest branch. This did exactly what I wanted it to do.. Thanks!

    Dec 5, 2014 at 17:42

553

WARNING: git clean deletes all your untracked files/directories and can’t be undone.


Sometimes just clean -f does not help. In case you have untracked DIRECTORIES, -d option also needed:

# WARNING: this can't be undone!

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

WARNING: git clean deletes all your untracked files/directories and can’t be undone.

Consider using -n (--dry-run) flag first. This will show you what will be deleted without actually deleting anything:

git clean -n -f -d

Example output:

Would remove untracked-file-1.txt
Would remove untracked-file-2.txt
Would remove untracked/folder
...

9

  • 1

    You can give git clean a path argument to be more specific and avoid deleting untracked files that aren’t conflicting.

    – joachim

    Oct 18, 2011 at 10:08

  • 7

    I think the scenario description makes it clear that he doesn’t really want to throw away the content. Rather what he wants is to stop git baulking at overwriting the files. @Lauri, this should not have happened to you. Unfortunately people seem to have misread the essence of scenario description – see my suggestion.

    – Hedgehog

    Feb 11, 2012 at 23:05

  • 24

    FINALLY. git clean -f -d is handy when make clean fails to clean everything.

    Jun 23, 2012 at 4:32

  • 8

    @crizCraig unless they are added in .gitignore

    Jun 13, 2013 at 6:58

  • 9

    @earthmeLon, for that you might want git clean -dfx. The -x ignores .gitignore. Typically your build products will be in .gitignore.

    Aug 12, 2015 at 18:28