Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset local repository branch to be just like remote repository HEAD

Tags:

git

undo

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.

like image 717
hap497 Avatar asked Oct 27 '09 00:10

hap497


People also ask

How do I reset my local github repository?

Find the commit hash of the commit you want to reset to with git log . Perform the local hard reset by running git reset --hard <commit-hash> .

What does reset git branch do?

Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

How do I reset my default branch?

Under your repository name, click Settings. In the "Code and automation" section of the sidebar, click Branches. Under "Default branch", to the right of the default branch name, click . Use the drop-down, then click a branch name.


1 Answers

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

like image 126
Dan Moulding Avatar answered Sep 23 '22 14:09

Dan Moulding