标签云

微信群

扫码加入我们

WeChat QR Code


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

2018年10月22日41分51秒

git reset --hard origin/branch_to_overwrite

2018年10月22日41分51秒

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

2018年10月22日41分51秒

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`

2018年10月22日41分51秒

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.

2018年10月22日41分51秒

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.

2018年10月22日41分51秒

This worked for me and my local files were NOT deleted.

2018年10月22日41分51秒

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

2018年10月22日41分51秒

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

2018年10月22日41分51秒

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

1970年01月01日00分03秒

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.

2018年10月22日41分51秒

instead of merging using 'git pull', try git fetch --all followed by 'git reset --hard origin/master'

2018年10月22日41分51秒

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

2018年10月22日41分51秒

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!

2018年10月22日41分51秒

Awesome... Ran this against my dotfiles repo... In my home directory. Good that I didn't really have anything important there...

2018年10月22日41分51秒

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.

2018年10月22日41分51秒

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

2018年10月22日41分51秒

crizCraig unless they are added in .gitignore

2018年10月22日41分51秒

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

2018年10月22日41分51秒

This is the best answer I've seen so far. I haven't tried it, but unlike other answers, this doesn't attempt to nuke all your untracked files, which is very dangerous for obvious reasons.

2018年10月22日41分51秒

Ditto - this worked for me when doing a very large merge (GitHub pull request) where I just wanted to accept it all on top of what I had. Good answer! In my case the last two commands were: 1) get fetch other-repo; 2) git merge -s recursive -X theirs other-repo/master

2018年10月22日41分51秒

Question: What is -X? What is 'theirs'?

2018年10月22日41分51秒

This will overwrite any conflicts with the repositories files and not your local ones, correct?

2018年10月22日41分51秒

"-X" is an option name, and "theirs" is the value for that option. You're choosing to use "their" changes, instead of "your" changes if there is a conflict.

2018年10月22日41分51秒

Your answer is just what you needed for your rep. I must ask, does this also remove all untracked files?

2018年10月22日41分51秒

Yeah, most of my rep is coming from here :) This will also remove all untracked files. Something I had forgotten and was painfully reminded of just 2 days ago...

2018年10月22日41分51秒

See the comments on this other answer: stackoverflow.com/a/8888015/2151700

2018年10月22日41分51秒

This did not remove my untracked files; which is actually what I'd expect. Is there a reason it might for some people and not for others?

2018年10月22日41分51秒

Untracked files are not affect4ed by git reset. If you want them to be removed as well, do git add . first, before git reset --hard

2018年10月22日41分51秒

I tried using "git clean" to solve the same issue, but it did not resolve it. git status says "Your branch and 'origin/master' have diverged, # and have 2 and 9 different commit(s) each, respectively." and git pull says something similar to what you have above.

2018年10月22日41分51秒

git clean is a rather blunt instrument, and could throw away a lot of things that you may want to keep. Better to remove or rename the files that git is complaining about until the pull succeeds.

2018年10月22日41分51秒

I do not think this works in general. Isn't there a way to do basically a git clone remote via a forced git pull?

2018年10月22日41分51秒

mathick: git fetch origin && git reset --hard origin/master

2018年10月22日41分51秒

Is git clean the best answer here? Seems like removing files isn't necessarily what the OP wants. They asked for 'an overwrite of local files' not deletion.

2018年10月22日41分51秒

No I don't think so. Stashing just moves uncommitted files out of the way. The above also moves (stashes) files that git does not track. This prevents files that have been added to the remote, which have not yet pulled down to your machine - but which you have created (!) - to be pulled down. All without destroying the uncommitted work. Hope that makes sense?

2018年10月22日41分51秒

If you don't have 1.7.6, you can mimic --include-untracked simply by temporarily git add-ing your entire repo, then immediately stashing it.

2018年10月23日41分51秒

I agree with Hedgehog. If you do the popular answers here, you are more than likely going to find you've inadvertently killed a lot of stuff that you didn't really want to lose.

2018年10月22日41分51秒

I had other untracked files--besides the one the merge/pull wanted to overwrite, so this solution worked best. git stash apply brought back all my untracked files with the exception (rightly) of the ones that the merge had already created: "already exists, no checkout." Worked perfectly.

2018年10月22日41分51秒

This is the cleanest answer, and should be the accepted one. To save some typing you can use the short form: git stash -u.

2018年10月22日41分51秒

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. See my suggestion.

2018年10月22日41分51秒

Though that answer might not fit exactly the description, it still saved me from the frustration of git twiddling with the carriage returns (event with autocrlf false). When git reset --hard HEAD does not leave you with "no" modified files, these "-f" flags are quite helpful. Thanks a bunch.

2018年10月22日41分51秒

This was what ultimately worked for me as I had force pushed my branch to the origin repo and kept getting merge conflicts when trying to pull it to my remote repo..

2018年10月22日41分51秒

Hi, actually this is a trick for a work around but really effective. Because some conflicts may happen just in few commits then reverting 5 commits will make sure no conflicts with remote code.

2018年10月22日41分51秒

Using "git merge origin/master" as the last line (like you say in your note) instead of "git pull" will be faster as you've already pulled down any changes from the git repo.

2018年10月22日41分51秒

Yeah of course, git merge origin/master will be faster and probably even safer. Since if someone pushed new changes during the removal of of files of this script (which is not likely to happen, but possible), the whole pull could fail. The only reason I put pull in there is because someone might not be working on the master branch, but some other branch and I wanted the script to be universal.

2018年10月22日41分51秒

If you have locally created files like option files, put them in .gitignore.

2018年10月23日41分51秒

Same here. Sometimes only the very hard solution works, it happens often that only reset and clean are not enough somehow...

2018年10月22日41分51秒

use git clean with caution

2018年10月22日41分51秒

This will overwrite modified files (files that were previously checked in) and it will remove untracked files (files that have never been checked in). Exactly what I was looking for, thanks!

2018年10月22日41分51秒

I suspect the third line git reset --hard HEAD may be redundant; my local man page (2.6.3) say that reset in the second line git reset --hard origin/master "defaults to HEAD in all forms."

2018年10月22日41分51秒

arichards I think your suspect is right but if second line will not work(by any reason) third line work well to reset. This solution doesn't need to be optimized. I just summarized other answers. That's all. Thank you for your comment. :)

2018年10月22日41分51秒

Using "git merge origin/master" as the last line (like you say in your note) instead of "git pull" will be faster as you've already pulled down any changes from the git repo.

2018年10月22日41分51秒

The checkout of modified files is needed, so this works 100% of times. I updated my script with that a long time ago, but forgot to update here as well. I also use it a little differently than you. I checkout files which have any type of modification, not just M, so it works all the time.

2018年10月22日41分51秒

This answer is also nice because it works regardless of which branch you are on!

2018年10月22日41分51秒

"git branch -D master" delete the branch. so be careful with it. I prefer to use "git checkout origin/master -b <new branch name>" which create a new branch with a new name and you done need 3,4 lines. Also recommended to use "git clean -f" as well.

2018年10月22日41分51秒

I personally found this to be most useful. It then keeps your working tree so you can check it in again. For my issue, I had the same files deleted as being added so it was stuck. Weird, I know.

2018年10月22日41分51秒