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: 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, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.
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).
Answer from Dan Moulding on Stack OverflowSetting 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: 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, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.
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).
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
How do I change the URI (URL) for a remote Git repository? - Stack Overflow
git - Resetting remote to a certain commit - Stack Overflow
[Git] How to 'git reset' a remote branch?
Need to reset git branch to origin version - Stack Overflow
First, view the existing remotes to verify which URL is currently set:
git remote -v
Then, you can set it with:
git remote set-url origin <NEW_GIT_URL_HERE>
See git help remote. You also can edit .git/config and change the URLs there.
You're not in any danger of losing history unless you do something very silly (and if you're worried, just make a copy of your repo, since your repo is your history.)
Updating the (push) entry:
When running git remote -v you'll usually see 2 entries that look like this:
origin git@github.com:username/repo_name.git (fetch)
origin git@github.com:username/repo_name.git (push)
You can optionally update the (push) URI by running:
git remote set-url --push origin <NEW_GIT_URL_HERE>
After updating, you'll have 2 different URIs for the 2 entries so you'll be fetching/pulling from one source and pushing to another source:
origin git@github.com:username/repo_name.git (fetch)
origin git@github.com:another_username/another_repo_name.git (push)
git remote -v
# View existing remotes
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
git remote set-url origin https://github.com/user/repo2.git
# Change the 'origin' remote's URL
git remote -v
# Verify new remote URL
# origin https://github.com/user/repo2.git (fetch)
# origin https://github.com/user/repo2.git (push)
Changing a remote's URL
Assuming that your branch is called master both here and remotely, and that your remote is called origin you could do:
git reset --hard <commit-hash>
git push -f origin master
However, you should avoid doing this if anyone else is working with your remote repository and has pulled your changes. In that case, it would be better to revert the commits that you don't want, then push as normal.
Update: you've explained below that other people have pulled the changes that you've pushed, so it's better to create a new commit that reverts all of those changes. There's a nice explanation of your options for doing this in this answer from Jakub Narębski. Which one is most convenient depends on how many commits you want to revert, and which method makes most sense to you.
Since from your question it's clear that you have already used git reset --hard to reset your master branch, you may need to start by using git reset --hard ORIG_HEAD to move your branch back to where it was before. (As always with git reset --hard, make sure that git status is clean, that you're on the right branch and that you're aware of git reflog as a tool to recover apparently lost commits.) You should also check that ORIG_HEAD points to the right commit, with git show ORIG_HEAD.
Troubleshooting:
If you get a message like "! [remote rejected] a60f7d85 -> master (pre-receive hook declined)"
then you have to allow branch history rewriting for the specific branch. In BitBucket for example it said "Rewriting branch history is not allowed". There is a checkbox named Allow rewriting branch history which you have to check.
Most other answers – including the accepted one – will result in unnecessary loss of local state.
Local changes are not inherently required to change a remote. You may need to apply local corrections in addition to resetting a remote, but that's a lower priority if your problem is simply that you need to quickly put the remote back to how it was before your push, or you pushed the wrong thing. This method (like any other forced push) has the potential to ruin your remote if you choose the wrong commit, but even then you can usually find the correct one and try again.
You must have the desired commit somewhere in your local repo that the remote should match.
Do not do any local resetting, checking out, or branch switching.
Use
git logto find the commit you want to the remote to be at. Usegit log -pto see changes, orgit log --graph --all --oneline --decorateto see a compact tree.If you cannot find the commit, try checking
git reflogwhich will show the state history of your repo. You may be able to recover the commit from its hash, e.g.:git branch <name for branch of rescued commit> <hash of rescued commit>Copy the commit's hash, tag, or (if it's the tip) its branch name.
Run a command like:
git push --force <remote> <commit-ish>:<the remote branch>e.g.
git push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:mainor
git push --force staging v2.4.0b2:releases
If the forced push fails, it's likely disabled by the remote. This may be worked around by temporarily changing one or both of receive.denyNonFastForwards and receive.denyDeletes. If your remote is hosted on a service without shell access, it probably has settings you can change to allow forced pushes.
I use a convenient alias (git go, mnemonic "graph oneline") for viewing history, which can be added like so:
git config --global alias.go 'log --graph --oneline --all --decorate'
Say I have a local branch and I make change A, then push it so both the local and remote has change A. I then realize I don't want change A as there was an error in it. I can use git reset to reset my local branch to the commit before change A, but then if I do that and try to push, it doesn't work. This makes sense because technically the local branch is behind the remote branch. And a solution to this is to just make a "revert change A" commit, but I don't even want my commit history to show these two commits since they cancel each other out
Is there a way to do "git reset" a remote branch?
If you haven't pushed to origin yet, you can reset your branch to the upstream branch with:
git checkout mybranch
git reset --hard origin/mybranch
(Make sure that you reference your latest commit in a separate branch, like you mention in your question)
Note that just after the reset, mybranch@{1} refers to the old commit, before reset.
But if you had already pushed, see "Create git branch, and revert original to upstream state" for other options.
With Git 2.23 (August 2019), that would be one command: git switch.
Namely: git switch -C mybranch origin/mybranch
Example
C:\Users\vonc\git\git>git switch -C master origin/master
Reset branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.
Your branch is up to date with 'origin/master'.
That restores the index and working tree, like a git reset --hard would.
As commented by Brad Herman, a reset --hard would remove any new file or reset modified file to HEAD.
Actually, to be sure you start from a "clean slate", a git clean -f -d after the reset would ensure a working tree exactly identical to the branch you just reset to.
This blog post suggests those aliases (for master branch only, but you can adapt/extend those):
[alias] resetorigin = !git fetch origin && git reset --hard origin/master && git clean -f -d resetupstream = !git fetch upstream && git reset --hard upstream/master && git clean -f -dThen you can type:
git resetupstreamor
git resetorigin
There is a slightly easier way to do this:
git reset --hard @{u}
@{u} is a shortcut for whatever your tracking branch is, so if you're on master and that tracks origin/master, @{u} points to origin/master.
The advantage of using this is that you don't have to remember (or type out) the full name of your tracking branch. You can also make an alias:
git-reset-origin="git reset --hard @{u}"
which will work regardless of the branch you're currently on.
Git supports this command:
git checkout -B master origin/master
Check out the origin/master branch and then reset master branch there.
UPDATE:
Or you can use new switch command for that
git switch -C master origin/master
As KindDragon's answer mentions, you can recreate master directly at origin/master with:
git checkout -B master origin/master
The git checkout man page mentions:
If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of
$ git branch -f <branch> [<start point>]
$ git checkout <branch>
Since Git 2.23+ (August 2019), since git checkout is too confusing, the new (still experimental) command is git switch:
git switch -C master origin/master
That is:
-C <new-branch> --force-create <new-branch>Similar to
--createexcept that if<new-branch>already exists, it will be reset to<start-point>.
This is a convenient shortcut for:$ git branch -f <new-branch> $ git switch <new-branch>
Originally suggested:
Something like:
$ git checkout master
# remember where the master was referencing to
$ git branch previous_master
# Reset master back to origin/master
$ git reset --hard origin/master
with step 2 being optional.