127

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

trrrrrrm
  • 11,362
  • 25
  • 85
  • 130
  • It sounds like you are looking to [delete the branch](http://stackoverflow.com/questions/2003505/how-to-delete-a-git-branch-both-locally-and-remotely)? – AturSams Apr 12 '17 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 '19 at 09:49
  • what is preventing you of checking out a new branch from master? – Mehdi Mar 23 '21 at 17:38

6 Answers6

189

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
Alon Barad
  • 1,491
  • 1
  • 13
  • 26
Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
  • 36
    This is the method that worked for me...otherwise you get a warning about paths can not be hard reset. – cgseller Apr 11 '18 at 19:48
  • 22
    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 '20 at 08:20
  • 1
    And if you get something like: Protected branch update failed for refs/heads/ You'll have to update/remove the Branch protection rules on github for that branch. github-->settings-->branches – ssoward Aug 19 '20 at 17:14
  • Also, you can use the SHA of any commit to reset the branch to that state: `git reset --hard ` – saza Feb 14 '23 at 21:22
144

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
Ritesh
  • 497
  • 4
  • 15
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 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 '13 at 04: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 '13 at 04: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 '13 at 04: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 '18 at 13:35
  • I tried the 3rd approach. It worked like a charm except new files were not removed. I removed manually. Thanks @Amber – venugopal Oct 10 '19 at 18:28
  • This one-liner cleans up removed files that were never in master: `git diff-tree -r --diff-filter=D development master --name-only | xargs git rm --ignore-unmatch` – kolypto Oct 22 '20 at 23:27
  • This recipe worked like a charm! Brilliant! but ... subsequent merging only works for new feature branches. Old features branches will have weird conflicts – kolypto Oct 27 '20 at 11:12
  • 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 04 '21 at 17:35
  • will we also overwrite develops upstream branch? – noob7 Jun 10 '22 at 07:07
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
Gibolt
  • 42,564
  • 15
  • 187
  • 127
  • 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 '22 at 07: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!

d1jhoni1b
  • 7,497
  • 1
  • 51
  • 37
2

Reset Last Commit

git reset HEAD~ 
Sanjay Sikdar
  • 435
  • 4
  • 10
  • 1
    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`*". – Gino Mempin May 12 '21 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.

Shea Lavington
  • 436
  • 2
  • 7