Reset local repository branch to be just like remote repository HEAD

GitUndo

Git Problem Overview


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 Solutions


Solution 1 - Git

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).

Solution 2 - Git

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

Solution 3 - Git

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

git reset --hard @{u}

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).

Next, as needed, use git clean to remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull

Solution 4 - Git

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.

Solution 5 - Git

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

Warning: git clean -x -d -f is irreversible and you may lose files and data (e.g. things you have ignored using .gitignore).

Solution 6 - Git

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

Solution 7 - Git

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”.

Solution 8 - Git

Provided that the remote repository is origin, and that you're interested in branch_name:

git fetch origin
git reset --hard origin/<branch_name>

Also, you go for reset the current branch of origin to HEAD.

git fetch origin
git reset --hard origin/HEAD

How it works:

git fetch origin downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the <branch_name> branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/branch_name.

Solution 9 - Git

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

Solution 10 - Git

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

Solution 11 - Git

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

#!/bin/bash
# reset the current repository
# WF 2012-10-15
# see https://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

Solution 12 - Git

The answer

git clean -d -f

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

Solution 13 - Git

If you had a problem as me, that you have already committed 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 careful!

Solution 14 - Git

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).

Solution 15 - Git

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.

Solution 16 - Git

Only 3 commands will make it work

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

Solution 17 - Git

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.

Solution 18 - Git

No amount of reset and cleaning seemed to have any effect on untracked and modified files in my local git repo (I tried all the options above). My only solution to this was to rm the local repo and re-clone it from the remote.

Fortunately I didn't have any other branches I cared about.

xkcd: Git

Solution 19 - Git

The only solution that works in all cases that I've seen is to delete and reclone. Maybe there's another way, but obviously this way leaves no chance of old state being left there, so I prefer it. Bash one-liner you can set as a macro if you often mess things up in git:

REPO_PATH=$(pwd) && GIT_URL=$(git config --get remote.origin.url) && cd .. && rm -rf $REPO_PATH && git clone --recursive $GIT_URL $REPO_PATH && cd $REPO_PATH
  • assumes your .git files aren't corrupt

Solution 20 - Git

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

Solution 21 - Git

  • 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.

Solution 22 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhap497View Question on Stackoverflow
Solution 1 - GitDan MouldingView Answer on Stackoverflow
Solution 2 - GitAkavallView Answer on Stackoverflow
Solution 3 - GitAsclepiusView Answer on Stackoverflow
Solution 4 - GitMikael OhlsonView Answer on Stackoverflow
Solution 5 - GitChristopher SmithView Answer on Stackoverflow
Solution 6 - GitJamsheerView Answer on Stackoverflow
Solution 7 - GitRobert SiemerView Answer on Stackoverflow
Solution 8 - GiteigenharshaView Answer on Stackoverflow
Solution 9 - GitAndrew TullochView Answer on Stackoverflow
Solution 10 - Gituser2846569View Answer on Stackoverflow
Solution 11 - GitWolfgang FahlView Answer on Stackoverflow
Solution 12 - GitAnaelView Answer on Stackoverflow
Solution 13 - GitKarolView Answer on Stackoverflow
Solution 14 - GitRainer BlomeView Answer on Stackoverflow
Solution 15 - GitJames RayView Answer on Stackoverflow
Solution 16 - GitDeep NirmalView Answer on Stackoverflow
Solution 17 - GitEmil SitView Answer on Stackoverflow
Solution 18 - GitMartinView Answer on Stackoverflow
Solution 19 - GitsudoView Answer on Stackoverflow
Solution 20 - GitGrastveitView Answer on Stackoverflow
Solution 21 - GitIndhujaView Answer on Stackoverflow
Solution 22 - GitMicah WalterView Answer on Stackoverflow