标签云

微信群

扫码加入我们

WeChat QR Code


According to the output of git status your second command git reset --hard HEAD failed. You didn’t paste it’s output, though. → Incomplete question.

2018年08月16日25分34秒

You are mixing two issues here: 1) how to reset a local branch to the point where the remote is and 2) how to clear your staging area (and possibly the working directory), so that git status says nothing to commit, working directory clean. – Please specify!

2018年08月15日25分34秒

RobertSiemer That's exactly what I want to. How to reset my local repo to be like the last commit on the remote repo but also I want to clean up my local repo, I don't want to have the changes that I have now and aren't in the remote repo. I just want my local repo to be exactly the same as in the remote. Can you tell me how to do that?

2018年08月16日25分34秒

Thank you for your answer. You said 'Note that the first example assumes that the remote repo's name is "origin" and that the branch named "master" in the remote repo matches the branch in your local repo.' How can I double check my remote repo's name and my branch name to be sure before I execute 'git reset --hard'? Thanks again.

2018年08月16日25分34秒

If you didn't explicitly name the remote, then it's name is likely just "origin" (the default). You can use "git remote" to get a list of all remote names. You can then use "git remote <name>" to see which branches push/pull with each other (e.g. if your "master" branch was cloned from "master" in the remote named "origin", then you'll get a line that says "master merges with remote master").

2018年08月16日25分34秒

"it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular" Why is that?

2018年08月16日25分34秒

Just after fetching, I believe you can do git reset FETCH_HEAD --hard instead, as well, that's the same meaning.

2018年08月16日25分34秒

Too bad I can only upvote this once. I have come to this exact answer like 20 times. Probably more.

2018年08月15日25分34秒

also, git clean -d -f if there are untracked directories present.

2018年08月16日25分34秒

also git clean -fdx

2018年08月16日25分34秒

If you want exact copy of remote branch you have to follow by git clean -ffdx. Note that thare are two f.

2018年08月16日25分34秒

The git clean -f was the essential piece I needed. Thanks!

2018年08月15日25分34秒

be careful using the clean command. it can delete ignored files from other branches.

2018年08月16日25分34秒

This seems like a better answer than the accepted one, because it dynamically resets to the current upstream branch rather than always a static one such as origin/master

2018年08月16日25分34秒

There was an incorrect suggestion in my answer that Dan caught earlier. I edited it away, since I don't want to lead anyone astray. As to the origin/master or origin/HEAD stuff, I expect that depends on whether or not you actually do a fetch first. If you just cloned origin, and it had no other branches, which I find to be quite common, then it should reset it fine. But of course, Dan is right.

2018年08月16日25分34秒

Warning: "git clean -x -d -f" is irreversible and you may loose files and data in .gitignore

2018年08月16日25分34秒

best answer; thanks.

2018年08月16日25分34秒

The {upstream} syntax requires upstream to be set which happens by default if you git checkout <branchname>. – Otherwise replace it with origin/<branchname>.

2018年08月15日25分34秒

Add -x to git clean to remove everything not in the commit (i.e. even files ignored with the .gitignore mechanism).

2018年08月15日25分34秒

you might want to use "git remote" to get the name of the remote. In certain cases, it won't be "origin"

2018年08月15日25分34秒

You should read question once again, there is nothing on affecting remote, but setting to same as remote, so you shouldn't do anything with remote, and this helped in my case and non of above.

2018年08月15日25分34秒

If you want to set it to the same as remote, you should at least do a fetch at some point don't you agree?

2018年08月16日25分34秒

you should at least try this or read docs: kernel.org/pub/software/scm/git/docs/git-checkout.html

2018年08月15日25分34秒

There is no other branch ;-) I think that extra step is something you should edit it into your answer, and if you do I'll remove the downvote (can't right now because it's locked)

2018年08月16日25分34秒

So create a temporary one.

2018年08月16日25分34秒

Can anyone tell me whats wrong with this, because lot of time I use this to reset my local branch with remote.

2018年08月15日25分34秒

Top answer already includes your answer.

2018年08月15日25分34秒

ADS