GIT re-merge files from dev to master
My master GIT branch seams to have some 开发者_JS百科errors thus I'd like to recheck, re-merge or possibly clone my dev branch over the master branch so the master branch would be a copy of dev.
How can I do that? Thx.
If the problem is simply that your checked-out files don't match up with the branch, just use git reset
normally:
git reset --hard HEAD
That should be all you need. However, if you still want to overwrite master with dev, read on.
If you want to overwrite your master branch with the contents of your dev branch, use git reset
like so:
$ git checkout master
$ git reset --hard dev
And then if you want to push this somewhere else:
$ git push origin master
Note that if your dev branch doesn't fast-forward from your master branch (which I'm guessing it won't, since you said that your master branch has some screwed up stuff in it), you'll need to add the --force
flag to the push to overwrite it on a remote:
$ git push origin master --force
Note, however, that this can involve all of the normal caveats of rewriting history a la git rebase
- if anyone else uses this remote, they'll need to deal with the equivalent of an upstream rebase.
To avoid this problem in the future, advise your friend that using --force
is almost never necessary. If they're getting conflicts when they try to git push
, they should git pull
first, resolve the conflicts, and then git push
.
From you comments it seems there are three possible cases:
- The master index contains correct code and the working copy is broken.
- The working copy is fine and the index is now broken.
- Both are broken.
First, back everything up. Then:
In case 1, use git reset --hard HEAD
to throw away the broken working copy.
In case 2, add and commit everything in the working copy.
In case 3, use git reset --hard dev
to throw away both the index and the working copy. Alternatively, git reset --hard SOME_COMMIT_ID_THAT_ISN'T_BROKEN
In all three cases tell your friend to never use --force
unless they really know what they're doing.
Also, it's probably best not to push into a non-bare repository (i.e. one with a working copy). I'd suggest you set up a bare repository somewhere that you both push and pull from rather than pushing directly into a repo that has a working copy. Use git init --bare
to create a bare repo.
Did I mention back everything up? Good. Do it.
Good question. I have wanted to do this before myself and I never did. My best guess might be to try the below. NOTE I'm not sure how well this will work for you since I have not done it myself.
git checkout master
git pull origin dev
git commit -a -m "reverted to dev"
Doing it the above way will likely cause conflicts.
精彩评论