134

I have develop & master branches, my develop branch is messy now and i would like to reset it and make it as a copy of my master. i'm not sure if merging the master into develop will make both of them identical. after trying to merge i got many conflicts i solved them using:

git checkout develop
git merge origin/master
//got many conflicts
git checkout . --theirs

is this enough for develop branch to be an identical copy to master ?

Thanks

3
  • It sounds like you are looking to delete the branch?
    – AturSams
    Apr 12, 2017 at 14:58
  • Setting develop as the new default branch (a few clicks with Github's web interface) is the most reversible solution.
    – mirekphd
    Dec 18, 2019 at 9:49
  • what is preventing you of checking out a new branch from master?
    – Mehdi
    Mar 23, 2021 at 17:38

6 Answers 6

202

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
5
  • 36
    This is the method that worked for me...otherwise you get a warning about paths can not be hard reset.
    – cgseller
    Apr 11, 2018 at 19:48
  • 23
    And if git push fails and updates were rejected because the tip of your current branch is behind its remote counterpart, then git push -f.
    – Abdull
    Jul 27, 2020 at 8:20
  • 1
    And if you get something like: Protected branch update failed for refs/heads/<your branch> You'll have to update/remove the Branch protection rules on github for that branch. github-->settings-->branches
    – ssoward
    Aug 19, 2020 at 17:14
  • 1
    Also, you can use the SHA of any commit to reset the branch to that state: git reset --hard <SHA>
    – saza
    Feb 14, 2023 at 21:22
  • Is the reset recursive in nature? What will happen to child branch of the develop branch? And if the child branch won't get reset, will it's base branch change to master or will remain develop?
    – Chaitanya
    Nov 22, 2023 at 11:01
151

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
9
  • 1
    my develop branch is used by other developers, so if i reset --hard how can i push that to other developers ?
    – trrrrrrm
    Aug 26, 2013 at 4:45
  • 2
    @ra_htial They'd need to reset to it as well. You probably don't want to use the first two options if you're sharing the branch with others, unless they also know to reset and don't have any pending work.
    – Amber
    Aug 26, 2013 at 4:45
  • Yes we all agreed to reset it, then we should all reset it and then start our work and push as usual ?
    – trrrrrrm
    Aug 26, 2013 at 4:46
  • 4
    found myself in this unfortunate situation. I used the last solution, but it doesn't remove files that weren't in master. To do that I had to first do git diff-tree -r --diff-filter=D develop master to find the files, then remove them either one-by-one or by copying their paths into a file and running cat file | xargs rm
    – Chris
    Apr 11, 2018 at 13:35
  • 3
    You should be doing what's in the next best answer, i.e. once on develop branch do: git reset --hard master Otherwise you get an error message
    – jerpint
    Apr 4, 2021 at 17:35
3

If all else fails, you can delete your current branch and just directly recreate it from master.

git branch -D develop
git checkout master
git checkout -b develop
1
  • I tried this in web interface ran into two issues: 1. it really do not delete the branch and marks it archive 2. Once I did this in cmd interface it said my stage is diverged. Now every time I try stage builds i have to clone to a different location and checkout stage. which is very annoying
    – WPFKK
    Jan 27, 2022 at 7:04
3

For example making staging branch identical to develop, one could simply recreate the pointer:

First make sure your local develop branch is up to date with origin/develop by running: git checkout develop && git pull origin develop

Then checkout your target branch (in this case staging) git checkout staging

And finally but not least reset target branch git reset --hard develop

Push changes by brute force (git will complain because local pointer have different address) git push -f

And that would be pretty much it!

2

Reset Last Commit

git reset HEAD~ 
1
  • 2
    This assumes develop was branched out from master and is 1 commit ahead of master. The OP had a general question of "i would like to reset it and make it as a copy of my master". May 12, 2021 at 23:28
-3

I'm a bit late to the party, however, after merging my dev branch into the master and pushing to the server, I do the following:

git fetch

git checkout development

git merge origin/master

git push

The the dev branch is one commit ahead of the master branch, and other developers don't have to worry about all commits in the development branch being reset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.