Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
2  
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 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 at 11:09
1  
@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 at 5:38

14 Answers 14

up vote 2621 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).

share|improve this answer
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
7  
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
9  
Just after fetching, I believe you can do git reset FETCH_HEAD --hard instead, as well, that's the same meaning. – undashes Apr 30 '13 at 21:21
    
Added a comment about doing a clean with the "-x" flag to clear out ignored files. – Christopher Smith Jul 23 '15 at 17:48

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

share|improve this answer
24  
also, git clean -d -f if there are untracked directories present. – garg Jan 23 '15 at 18:09
15  
also git clean -fdx – Swapnil Sep 28 '15 at 6:32
5  
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 at 14:51
2  
The git clean -f was the essential piece I needed. Thanks! – user1167442 Apr 26 at 22:51
    
I have the same problem, unfortunately, none of these commands worked for me. git status still reports a list of modified files. – Toast Sep 29 at 20:37

git reset --hard HEAD actually only resets to the last committed state. In this case HEAD refers to the HEAD of your branch.

If you have several commits, this won't work..

What you probably want to do, is reset to the head of origin or whatever you remote repository is called. I'd probably just do something like

git reset --hard origin/HEAD

Be careful though. Hard resets cannot easily be undone. It is better to do as Dan suggests, and branch off a copy of your changes before resetting.

share|improve this answer
2  
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. – Mikael Ohlson Oct 27 '09 at 9:58
1  
Nice to see some humbleness on Stack Overflow and credit being given where due. Kudos, you're setting a good example. – GrayedFox Aug 4 at 15:03

First, reset to the previously fetched HEAD of the corresponding upstream branch:

git reset --hard @{upstream}

The advantage of specifying @{upstream} or its abbreviated form @{u} is that the name of the remote repo and branch don't have to be explicitly specified.

Next, get the latest changes:

git pull
share|improve this answer
6  
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 – Jon z Sep 22 '15 at 15:10

All of the above suggests are right, but often to really reset your project, you also need to remove even files that are in your .gitignore.

To get the moral equivalent of erasing your project directory and re-cloning from the remote is:

git fetch
git reset --hard
git clean -x -d -f
share|improve this answer
2  
Warning: "git clean -x -d -f" is irreversible and you may loose files and data in .gitignore – Akarsh Satija Feb 16 at 6:20
    
best answer; thanks. – Bob Baxley Aug 1 at 3:27

This is something I face regularly, & I've generalised the script Wolfgang provided above to work with any branch

I also added an "are you sure" prompt, & some feedback output

#!/bin/bash
# reset the current repository
# WF 2012-10-15
# AT 2012-11-09
# see http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head
timestamp=`date "+%Y-%m-%d-%H_%M_%S"`
branchname=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
read -p "Reset branch $branchname to origin (y/n)? "
[ "$REPLY" != "y" ] || 
echo "about to auto-commit any changes"
git commit -a -m "auto commit at $timestamp"
if [ $? -eq 0 ]
then
  echo "Creating backup auto-save branch: auto-save-$branchname-at-$timestamp"
  git branch "auto-save-$branchname-at-$timestamp" 
fi
echo "now resetting to origin/$branchname"
git fetch origin
git reset --hard origin/$branchname
share|improve this answer
3  
you might want to use "git remote" to get the name of the remote. In certain cases, it won't be "origin" – Yurik Oct 7 '14 at 22:54

The question mixes two issues here:

  1. how to reset a local branch to the point where the remote is
  2. how to clear your staging area (and possibly the working directory), so that git status says nothing to commit, working directory clean.

The one-stop-answer is:

  1. git fetch --prune (optional) Updates the local snapshot of the remote repo. Further commands are local only.
    git reset --hard @{upstream}Puts the local branch pointer to where the snapshot of the remote is, as well as set the index and the working directory to the files of that commit.
  2. git clean -d --force Removes untracked files and directories which hinder git to say “working directory clean”.
share|improve this answer
    
The @{upstream} syntax requires upstream to be set which happens by default if you git checkout <branchname>. – Otherwise replace it with origin/<branchname>. – Robert Siemer Jan 31 at 1:31
    
Add -x to git clean to remove everything not in the commit (i.e. even files ignored with the .gitignore mechanism). – Robert Siemer Jan 31 at 1:34

Here is a script that automates what the most popular answer suggests ... See http://stackoverflow.com/a/13308579/1497139 for an improved version that supports branches

#!/bin/bash
# reset the current repository
# WF 2012-10-15
# see http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head
timestamp=`date "+%Y-%m-%d-%H_%M_%S"`
git commit -a -m "auto commit at $timestamp"
if [ $? -eq 0 ]
then
  git branch "auto-save-at-$timestamp" 
fi
git fetch origin
git reset --hard origin/master
share|improve this answer

I did:

git branch -D master
git checkout master

to totally reset branch


note, you should checkout to another branch to be able to delete required branch

share|improve this answer
5  
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. – user2846569 May 14 '14 at 10:43
    
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? – Tim Castelijns May 14 '14 at 10:50
2  
you should at least try this or read docs: kernel.org/pub/software/scm/git/docs/git-checkout.html – user2846569 May 14 '14 at 11:04
    
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) – Tim Castelijns May 14 '14 at 11:15
    
So create a temporary one. – qaisjp Jul 14 '14 at 14:29

If you had a problem as me, that you have already commited some changes, but now, for any reason you want to get rid of it, the quickest way is to use git reset like this:

git reset --hard HEAD~2

I had 2 not needed commits, hence the number 2. You can change it to your own number of commits to reset.

So answering your question - if you're 5 commits ahead of remote repository HEAD, you should run this command:

git reset --hard HEAD~5

Notice that you will lose the changes you've made, so be carefull!

share|improve this answer

If you want to go back to the HEAD state for both the working directory and the index, then you should git reset --hard HEAD, rather than to HEAD^. (This may have been a typo, just like the single versus double dash for --hard.)

As for your specific question as to why those files appear in the status as modified, it looks like perhaps you did a soft reset instead of a hard reset. This will cause the files that were changed in the HEAD commit to appear as if they were staged, which is likely what you are seeing here.

share|improve this answer

Previous answers assume that the branch to be reset is the current branch (checked out). In comments, OP hap497 clarified that the branch is indeed checked out, but this is not explicitly required by the original question. Since there is at least one "duplicate" question, Reset branch completely to repository state, which does not assume that the branch is checked out, here's an alternative:

If branch "mybranch" is not currently checked out, to reset it to remote branch "myremote/mybranch"'s head, you can use this low-level command:

git update-ref refs/heads/mybranch myremote/mybranch

This method leaves the checked out branch as it is, and the working tree untouched. It simply moves mybranch's head to another commit, whatever is given as the second argument. This is especially helpful if multiple branches need to be updated to new remote heads.

Use caution when doing this, though, and use gitk or a similar tool to double check source and destination. If you accidentally do this on the current branch (and git will not keep you from this), you may become confused, because the new branch content does not match the working tree, which did not change (to fix, update the branch again, to where it was before).

share|improve this answer

If you don't mind saving your local changes, yet still want to update your repository to match origin/HEAD, you can simply stash your local changes and then pull:

git stash
git pull
share|improve this answer

If you want the current changes to be used later then stash the changes other wise you can use these two commands,

git fetch --all
git reset --hard origin/master
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.