If you want the two branches to be the same then
// from Develop and assuming your master is up to date with origin/master
git reset --hard master
Answer from Tim Jarvis on Stack OverflowIf you want the two branches to be the same then
// from Develop and assuming your master is up to date with origin/master
git reset --hard master
If you want to make develop be identical to master, the simplest way is just to recreate the pointer:
git branch -f develop master
Or, if you already have develop checked out:
git reset --hard master
Note however that both of these options will get rid of any history that develop had which wasn't in master. If that isn't okay, you could preserve it by instead creating a commit that mirrored master's latest state:
git checkout develop
git merge --no-commit master
git checkout --theirs master .
git commit
Reset feature branch to master
How to reset your main branch to a recently known working version
How do I reset a git branch to a given previous commit and fix the detached HEAD? - Unix & Linux Stack Exchange
I made a bunch of changes on the master branch of a repo with two other team members. The changes ended up being completely scratched. How do I revert back to the original master code without hitting control-z a million times?
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.
HEAD is where your workspace is currently in the tree of git commits; detached means that it doesn't correspond to a branch. To fix this, you should create a new branch with git checkout -b branch (replacing branch with the name you want to give your new branch).
If you want to drop the commits following the one you reset to, you can delete the master branch and re-create it:
git branch -D master
git checkout -b master
If you're working on a repository which is pushed elsewhere you'll need to do more work to fix things up, possibly forcing a push (and telling every one else to re-clone their workspace). If you have shared state, you should really create a revert commit; take a look at git revert (starting from master and reverting all the commits starting with the one following c70e611).
HEAD detached at c70e611
This is because when you did the git reset --hard, you were not on any branch at that time. You had a detached HEAD, and that detached head got moved with the git reset --hard command, along with a rewrite of your working tree to that state.
If you want some branch foo to be c70611, then:
git checkout foo
git reset --hard c70611
If this is considered to be a good state to push to foo's upstream then just git push <remote-name> foo.
There is a more direct way to force foo to c70611 without checking it out to be the current branch. Namely, you can rewrite what foo points to using the git update-ref command.
Before doing any of the above, I would pause and try to see how I had ended up in a detached state without noticing. Perhaps it was an unfinished rebase or whatever. Step one is to review the last few entries in the git reflog to help jog your memory.