git - Reset local repository branch to be just like remote repository HEAD

ID : 66

viewed : 513

Tags : gitundogit





Top 5 Answer for git - Reset local repository branch to be just like remote repository HEAD

vote vote

99

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

vote vote

85

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 
vote vote

76

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 
vote vote

61

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.

vote vote

54

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

Top 3 video Explaining git - Reset local repository branch to be just like remote repository HEAD







Related QUESTION?