Is there a way to do a git pull
that ignores any local file changes without blowing the directory away and having to perform a git clone
?

- 1,547
- 2
- 12
- 30

- 139,374
- 27
- 55
- 71
-
29By "ignores" do you mean "overwrites"? – Cascabel Nov 11 '10 at 17:21
-
5@Cascabel It means to revert all the local changes, uncommit all the local commits, delete all the local new files and directories, undelete all the locally deleted files and directories, etc. In short, just run a command as if `rm -rf local_repo && git clone remote_url`. – Victor Mar 12 '18 at 08:07
-
1Does this answer your question? [How do I force "git pull" to overwrite local files?](https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files) – Henke Dec 04 '20 at 15:21
-
111 years later, and I'm still waiting for a simple, reliable way to do this. I loathe "git" so much... it wastes so much of my time. – Mike Gledhill Mar 23 '22 at 08:41
17 Answers
If you mean you want the pull to overwrite local changes, doing the merge as if the working tree were clean, well, clean the working tree:
git reset --hard
git pull
If there are untracked local files you could use git clean
to remove them.
git clean -f
to remove untracked files-df
to remove untracked files and directories-xdf
to remove untracked or ignored files or directories
If on the other hand you want to keep the local modifications somehow, you'd use stash to hide them away before pulling, then reapply them afterwards:
git stash
git pull
git stash pop
I don't think it makes any sense to literally ignore the changes, though - half of pull is merge, and it needs to merge the committed versions of content with the versions it fetched.
-
5If after `git reset` your files still differ from the remote, read http://stackoverflow.com/questions/1257592/removing-files-saying-old-mode-100755-new-mode-100644-from-unstaged-changes-in – Colonel Panic Aug 30 '12 at 22:31
-
3Git is the strangest thing ever. Git reset --hard done. Then git status: Your branch is ahead by 2 commits. – Shailen Mar 08 '13 at 15:19
-
43@shailenTJ "Local changes" here means uncommitted changes, not local commits. `git reset --hard` affects the former, not the latter. If you want to fully reset to the remote's state, `git reset --hard origin/
` - but often and in this case, those two commits you're ahead of origin by are work you did, not something you want to throw away. – Cascabel Mar 08 '13 at 15:23 -
2So this is the same thing as destroying the local repository and re-downloading, right? I just want to be able to force the pull and overwrite changes for convenience. 99% of the time I get this error message when I've accidentally messed something up locally and just want to start over from the repo. – sudo Dec 15 '13 at 19:26
-
What if you cannot possibly not have a local change vs head? E.g. the repo was made on a case sensitive file system and is cloned on a case insensitive file system and there's 2 files with same name different casing? – xster May 08 '14 at 22:30
-
-
Dangerous, very dangerous commands here! Usually this happens when you still relevant information in the repo. Doing the first group of commands will wipe these changes, doing the second one is like throwing them all into the waste bin and then saying "ohh wait this was relevant information". I would advice to use less-lethal commands like fetch, see Artur's response. – adelriosantiago Oct 03 '17 at 18:34
-
2@adelriosantiago Artur's answer also has a reset --hard. It throws away exactly the same as the part of this answer. And yes, throwing things away is potentially dangerous, but it's what the OP asked to do. Their alternative was deleting the directory and re-cloning. They *wanted* to throw things away. If you want to, say, commit to a local branch and also separately fetch origin's master, great, that's a more common thing to want to do - it's just not what this question was about. – Cascabel Oct 03 '17 at 18:41
-
@Jefromi Ohh, you are right. That "ignores any local file changes without blowing the directory away" is a bit confusing. At first it seems that he wants to keep (ignore) these changes (for which I would recommend a fetch) – adelriosantiago Oct 03 '17 at 19:10
-
Why using stash with pull? Git will notify you in case you have local changes afaik. – Smily Mar 10 '19 at 09:38
-
This is not working when you have local changes committed. In my case I had some commit amends that wanted to rollback, the solution bellow worked for me. – camposer Jun 24 '20 at 10:51
For me the following worked:
(1) First fetch all changes:
$ git fetch --all
(2) Then reset the master:
$ git reset --hard origin/master
Note - For users of github, "master" was replaced with "main" in October 2020. For projects created since then you may need to use "main" instead, like:
$ git reset --hard origin/main
(3) Pull/update:
$ git pull

- 5
- 7

- 12,746
- 4
- 52
- 44
-
8this even works when u have committed ur local changes, but still u want to revert – agsachin Nov 02 '16 at 11:05
-
I can not understand what is the purpose of point 1: what happens if I do only 2 and 3? – Marco Servetto Jul 18 '17 at 01:19
-
5@Marco Servetto: You first fetch all your git changes, but don't apply them yet. Then you reset the master to the last state (updated). If you skip the first step, you will revert changes to the old master (local). From my experience, the way I described it, never causes problems. All other attempts do at the end. – Artur Barseghyan Jul 18 '17 at 07:25
-
1This worked for me, I wanted to ignore all my local changes including recovering deleted files – Neri Jul 30 '18 at 17:02
-
-
2I just did steps 1 and 2, and it seemed sufficient. What is the purpose of step 3? – MarredCheese Feb 08 '20 at 16:03
-
@MarredCheese Yes! I want to know that: the working files have changed after the 2nd command, and the 3rd command says "Already up to date". – mike rodent Feb 24 '20 at 19:12
-
@MarredCheese: That's rather occasional that 3rd step is not needed in your case. I've seen enough cases when it was required. Otherwise you might find yourself working on accidentally outdated master. – Artur Barseghyan Feb 25 '20 at 08:03
-
1
-
1Worked like a charm. Was stuck in a mess, but your answer worked. I wanted to ignore the merge conflicts by keeping only the changes from the remote branch. These steps worked great! Thanks :) – GuruJeya Apr 15 '21 at 17:05
You just want a command which gives exactly the same result as rm -rf local_repo && git clone remote_url
, right? I also want this feature. I wonder why git does not provide such a command (such as git reclone
or git sync
), neither does svn provide such a command (such as svn recheckout
or svn sync
).
Try the following command:
git reset --hard origin/master
git clean -fxd
git pull

- 1,303
- 1
- 10
- 28
-
That's what really works, even when you already have local commits that you want to remove. – Yuri Ghensev May 11 '18 at 17:09
-
10Warning guys!! `git clean -fxd` removes files from `.gitignore` also. – Ramesh Navi Dec 05 '18 at 05:44
-
@RameshNavi Sure. This is exactly what is wanted. What is wanted is to have a faster way to re-clone it, i.e. to delete the entire local repo and then clone it. – Victor Nov 18 '19 at 13:30
The command bellow wont work always. If you do just:
$ git checkout thebranch
Already on 'thebranch'
Your branch and 'origin/thebranch' have diverged,
and have 23 and 7 different commits each, respectively.
$ git reset --hard
HEAD is now at b05f611 Here the commit message bla, bla
$ git pull
Auto-merging thefile1.c
CONFLICT (content): Merge conflict in thefile1.c
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
and so on...
To really start over, downloading thebranch and overwriting all your local changes, just do:
$ git checkout thebranch
$ git reset --hard origin/thebranch
This will work just fine.
$ git checkout thebranch
Already on 'thebranch'
Your branch and 'origin/thebranch' have diverged,
and have 23 and 7 different commits each, respectively.
$ git reset --hard origin/thebranch
HEAD is now at 7639058 Here commit message again...
$ git status
# On branch thebranch
nothing to commit (working directory clean)
$ git checkout thebranch
Already on 'thebranch'

- 11,237
- 9
- 59
- 76
-
4YES. This is what I needed for an ultimate "don't give an F about what's local" approach. Thanks. :) – Adambean Oct 19 '16 at 10:35
-
To **completely** remove all the local changes, you need the command `git clean -fxd` – Victor Mar 05 '21 at 02:16
-
Just be careful with `git-clean - Remove untracked files from the working tree`, as this affects "untracked files", not exactly what OP asked for. But ok, good to be here in the comment, just in case someone wants to also remove untracked files. – DrBeco Mar 07 '21 at 01:46
this worked for me
git fetch --all
git reset --hard origin/master
git pull origin master
with the accepted answer I get conflict errors

- 3,080
- 29
- 42
-
That's very similar to the 2015 answer of Artur Barseghyan... but I'd still like to know what the purpose of the 3rd command is: the working files have changed after the 2nd command, and the 3rd command says "Already up to date" – mike rodent Feb 24 '20 at 19:14
-
that is the only combination that worked on my environment, maybe you are seeing a different thing, for me, I needed the three commands – Pablo Pazos Feb 25 '20 at 20:10
-
Interesting. Might be a version thing. I'm on git 2.7.4. But I've also just seen a new comment from Artur Barseghyan: "Otherwise you might find yourself working on accidentally outdated master." – mike rodent Feb 25 '20 at 20:25
-
maybe, but the only thing I can say is "this worked for me when no other solution did", so it might help others – Pablo Pazos Feb 26 '20 at 00:49
-
To **completely** remove all the local changes, you need the command `git clean -fxd` – Victor Mar 05 '21 at 02:16
I usually do:
git checkout .
git pull
In the project's root folder.

- 257
- 3
- 10
-
1
-
3@UjjwalRoy From the comment on the question, I believe that was the intention. But when I want to save local changes I use `git add -A` `git stash` – Cassiano Franco Jul 14 '20 at 14:20
If you are on Linux:
git fetch
for file in `git diff origin/master..HEAD --name-only`; do rm -f "$file"; done
git pull
The for loop will delete all tracked files which are changed in the local repo, so git pull
will work without any problems.
The nicest thing about this is that only the tracked files will be overwritten by the files in the repo, all other files will be left untouched.

- 18,665
- 21
- 103
- 138

- 4,175
- 1
- 24
- 18
-
-
-
great solution, it worked for me to overwrite a sqlite DB only if there are changes from the repo and keep the locally changed version on the prototype deploy.. thanks! – Roberto Nov 18 '22 at 14:41
Look at git stash to put all of your local changes into a "stash file" and revert to the last commit. At that point, you can apply your stashed changes, or discard them.

- 14,762
- 6
- 59
- 85
If you are on a branch, and want to discard any local changes on the branch and pull the remote branch, but encounter
Your branch and 'origin/<branch_name>' have diverged
,
it can be resolved while staying on the branch by:
git fetch --all
git reset --hard origin/<branch_name>

- 723
- 9
- 15
-
-
this worked, what I like in this answer his - In" git reset --hard origin/
" . Branch name varies so we need to enter the branch name we want to reflect – coder kemp Apr 19 '23 at 18:32
It's late but someone can find this useful.(Worked for me)
- git restore < fileName> or git restore .
- git pull

- 41
- 1
This will fetch the current branch and attempt to do a fast forward to master:
git fetch && git merge --ff-only origin/master

- 45,477
- 28
- 157
- 213
.gitignore
"Adding unwanted files to .gitignore works as long as you have not initially committed them to any branch. "
Also you can run:
git update-index --assume-unchanged filename
https://chamindac.blogspot.com/2017/07/ignoring-visual-studio-2017-created.html

- 17,541
- 8
- 92
- 91

- 11
- 3
Also, it's possible to keep changes from local commits and push them as a new commit. I use these steps when I have a mess in my local commits.
- git reset --soft origin/main
- git stash
- git pull --rebase
- git stash pop

- 61
- 1
- 2
git pull will give error if we change any thing in any files in our local system, So we need to git stash
git pull I got the message - error: Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge. Aborting
git stash Saved working directory and index state WIP on ....
git pull Updating... 10 files changed, 291 insertions(+), 169 deletions(-)

- 11
- 2
after visiting this post at least 10 times in the last year, decided to create an alias for that, just run the following in your terminal:
git config --global alias.startover "!f() { git fetch $1; git reset --hard $2; }; f"
and then the usage:
git startover origin origin/branchNameHere
(or a shorter version, for the people that always name their remote origin:
git config --global alias.startover "!f() { git fetch origin; git reset --hard origin/$1; }; f"
and usage:
git startover branchNameHere
)

- 1,460
- 3
- 17
- 37