MyTetra Share
Делитесь знаниями!
Reset local repository branch to be just like remote repository HEAD
Время создания: 16.05.2018 18:12
Текстовые метки: git reset local master to remote
Раздел: Git
Запись: Velonski/mytetra-database/master/base/15264763414omb9tfisu/text.html на raw.githubusercontent.com

How do I reset my local branch to be just like the branch on the remote repository?


I did:


git reset --hard HEAD

But when I run a git status,


On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: java/com/mycompany/TestContacts.java

modified: java/com/mycompany/TestParser.java

Can you please tell me why I have these 'modified'? I haven't touched these files? If I did, I want to remove those.


git undo

shareimprove this question

edited Feb 19 '16 at 18:18


isherwood

34.9k976105

asked Oct 27 '09 at 0:27


hap497

43.5k367285

5

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. – Robert Siemer Jan 8 '16 at 11:06

1

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! – Robert Siemer Jan 8 '16 at 11:09

5

@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? – Alex Ventura Jan 26 '16 at 5:38

add a comment

18 Answers

active oldest votes

up vote

4178

down vote

accepted

Setting your branch to exactly match the remote branch can be done in two steps:


git fetch origin

git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:


git commit -a -m "Saving my work, just in case"

git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).


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 currently checked-out branch in your local repo.


BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).


shareimprove this answer

edited Sep 19 '11 at 15:59

answered Oct 27 '09 at 1:44


Dan Moulding

123k158291

1

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. – hap497 Oct 27 '09 at 2:57

15

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"). – Dan Moulding Oct 27 '09 at 13:51

5

"it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular" Why is that? – LeeGee Mar 20 '13 at 11:00

17

Just after fetching, I believe you can do git reset FETCH_HEAD --hard instead, as well, that's the same meaning. – Jean Apr 30 '13 at 21:21

3

Too bad I can only upvote this once. I have come to this exact answer like 20 times. Probably more. – frosty Sep 6 '17 at 23:07

show 16 more comments

up vote

229

down vote

I needed to do (the solution in the accepted answer):


git fetch origin

git reset --hard origin/master

Followed by:


git clean -f

to remove local files


To see what files will be removed (without actually removing them):


git clean -n -f

shareimprove this answer

edited Jun 22 '17 at 17:59

answered Dec 27 '14 at 6:20


Akavall

34.3k25116163

45

also, git clean -d -f if there are untracked directories present. – garg Jan 23 '15 at 18:09

29

also git clean -fdx – Swapnil Kotwal Sep 28 '15 at 6:32

12

If you want exact copy of remote branch you have to follow by git clean -ffdx. Note that thare are two f. – Trismegistos Feb 22 '16 at 14:51

3

The git clean -f was the essential piece I needed. Thanks! – user1167442 Apr 26 '16 at 22:51

10

be careful using the clean command. it can delete ignored files from other branches. – mikoop Jan 9 '17 at 8:23

show 4 more comments

 
MyTetra Share v.0.59
Яндекс индекс цитирования