Skip to content

Resetting your local repository

abhikp edited this page Aug 4, 2011 · 4 revisions

You might run into a situation where your local branch has completely diverged from the remote branch and you need to sync back up. Git provides you two options: keep unique commits from the local repository as unstaged changes or completely discard them.

```
% git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 6 and 9 different commit(s) each, respectively.
```
  1. Run git fetch to update the "cache" of all remote repositories.

    % git fetch
    

Option 1 - Reset to the remote branch and keep unique commits.

```
% git reset origin/master
% git status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working
#   directory)
#
#   deleted:    INSTALL
#   deleted:    a.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   foo
no changes added to commit (use "git add" and/or "git commit -a")
```

Your commit log will now match `origin/master` and the diff between your local branch and `origin/master` is left intact as unstaged changes. This allows your to merge in any changes from previous commits that you want to keep.

Option 2 - Reset to the remote branch and toss out any unique commits.

```
% git reset --hard origin/master
% git status
# On branch master
nothing to commit (working directory clean)
```

Clone this wiki locally