Reset local repository branch to be just like remote repository HEAD

Viewed 5055360

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

I tried:

git reset --hard HEAD

But git status claims I have modified files:

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
24 Answers

Use the commands below. These commands will remove all untracked files from local git too

git fetch origin
git reset --hard origin/master
git clean -d -f

The answer

git clean -d -f

was underrated (-d to remove directories). Thanks!

This is what I use often:

git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;

Note that it is good practice not to make changes to your local master/develop branch, but instead checkout to another branch for any change, with the branch name prepended by the type of change, e.g. feat/, chore/, fix/, etc. Thus you only need to pull changes, not push any changes from master. Same thing for other branches that others contribute to. So the above should only be used if you have happened to commit changes to a branch that others have committed to, and need to reset. Otherwise in future avoid pushing to a branch that others push to, instead checkout and push to the said branch via the checked out branch.

If you want to reset your local branch to the latest commit in the upstream branch, what works for me so far is:

Check your remotes, make sure your upstream and origin are what you expect, if not as expected then use git remote add upstream <insert URL>, e.g. of the original GitHub repo that you forked from, and/or git remote add origin <insert URL of the forked GitHub repo>.

git remote --verbose

git checkout develop;
git commit -m "Saving work.";
git branch saved-work;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force

On GitHub, you can also checkout the branch with the same name as the local one, in order to save the work there, although this isn't necessary if origin develop has the same changes as the local saved-work branch. I'm using the develop branch as an example, but it can be any existing branch name.

git add .
git commit -m "Reset to upstream/develop"
git push --force origin develop

Then if you need to merge these changes with another branch while where there are any conflicts, preserving the changes in develop, use:

git merge -s recursive -X theirs develop

While use

git merge -s recursive -X ours develop

to preserve branch_name's conflicting changes. Otherwise use a mergetool with git mergetool.

With all the changes together:

git commit -m "Saving work.";
git branch saved-work;
git checkout develop;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
git add .;
git commit -m "Reset to upstream/develop";
git push --force origin develop;
git checkout branch_name;
git merge develop;

Note that instead of upstream/develop you could use a commit hash, other branch name, etc. Use a CLI tool such as Oh My Zsh to check that your branch is green indicating that there is nothing to commit and the working directory is clean (which is confirmed or also verifiable by git status). Note that this may actually add commits compared to upstream develop if there is anything automatically added by a commit, e.g. UML diagrams, license headers, etc., so in that case, you could then pull the changes on origin develop to upstream develop, if needed.

Only 3 commands will make it work

git fetch origin
git reset --hard origin/HEAD
git clean -f

You can fetch the origin and reset to solve the problem

 git fetch origin
 git reset --hard origin/master

You can save your changes before doing the reset like below,

git stash

And after resetting, if you want that changes back, you can simply run,

git stash apply

The top rated answer here did not reset my local code as expected.

  1. nowadays, master is usually main
  2. it doesn't do anything with untracked files you may have lying around

Instead:

  1. check the name of your default remote branch (this is not a git thing so check in GitHub) then replace main or master in step 4 below with this

  2. save current stuff git stash -u

  3. update from remote git fetch origin

  4. reset to remote default branch (but see step 1 above) git reset --hard origin/main

Have you forgotten to create a feature-branch and have committed directly on master by mistake?

You can create the feature branch now and set master back without affecting the worktree (local filesystem) to avoid triggering builds, tests and trouble with file-locks:

git checkout -b feature-branch
git branch -f master origin/master
  • Throwing error because it has uncomitted changes.
  1. So, You can use git stash
  • This saves uncomitted changes away for later use, and then reverts them from your working copy.
  • If you want these changes again, you can use git stash apply
  1. Then You can use git pull
  • This takes the recent code from remote repo.
Related