1

I have Git master and dev branches. I inadvertently did something wrong during the process of trying to merge my dev branch into my master branch and it now appears that master is messed up. I know my dev branch is OK and I don't want anything to happen to it so I'm trying to replace my master branch with my dev branch. It would be nice if I could retain my master commit log entries but I'm OK with losing them so long as I can retain everything in dev. I'm trying to implement the solutions described in this Stackoverflow question but they're not working. Here's what I did:

$ git checkout dev
$ git branch -f master dev
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 4 commits and can be fast-forward.
(use "git pull" to update your local branch)

If I then do "git pull", the master branch no longer contains dev's changes and Git tells me my branch is up-to-date with 'origin/master'. What do I do now? How is this overwriting master with dev? Master doesn't contain the dev changes. If I now do "git merge --no-ff dev", Git says "Already up-to-date."

I also tried to implement the second answer by doing the following:

$ git checkout dev
$ git merge -s ours --no-commit master
Already up-to-date.
% git commit
On branch dev
Your branch is up-to-date with 'origin/dev'.
nothing to commit, working tree clean
$ git checkout master
Your branch is behind 'origin/master' by 4 commits and can be fast-forward.
(use "git pull" to update your local branch)

As you can see, this takes me right back to where I was when I tried to implement the first solution. I'm totally confused. How do I overwrite my master branch with my dev branch?

Community
  • 1
  • 1
Jim
  • 13,430
  • 26
  • 104
  • 155
  • Read about git reflog. Git is pretty flexible in how you can recover to different "actions". – Seth Flowers Mar 06 '17 at 18:06
  • You could use a graphical repository browser like `gitk` to get an overview of the situation. `gitk` also lets you move a branch pointer to any commit you like by checking out that branch first and then right-clicking on the desired commit and choosing `Reset to here`. – mkrieger1 Mar 06 '17 at 18:09
  • Get help from "A DOG": `git log --all --decorate --oneline --graph` (**a**ll **d**ecorate **o**neline **g**raph = adog). Or, use a graphical browser, as @mkrieger1 suggested. It's clear that part of the problem is that your upstream repository (`origin/*`) has commits you are trying to "edit away" in history terms, but it's not immediately clear what the rest of your situation is. – torek Mar 06 '17 at 20:20
  • @torek Pretty output but it's not very understandable. Hard to believe this is the best VCS tool we have in 2017. – Jim Mar 07 '17 at 17:18
  • @Dave: if you add a sufficient snippet of the result (preferably as cut and paste text, not as an image) to your question, someone can probably help more. The problem itself is fundamentally somewhat hard because it's *distributed:* the immediate issue is not in *your* repository, it's in *someone else's*. – torek Mar 07 '17 at 18:11

1 Answers1

0

It would be nice if I could retain my master commit log entries

I would create a new branch at your current master to keep track of all of the history.

  1. git checkout master
  2. git branch old-master

I'm trying to replace my master branch with my dev branch

To do this simply, I would just hard reset your master branch to your dev branch.

  1. git checkout master
  2. git reset --hard dev

This will quite literally just move the head of your master branch to the head of your dev branch.

This will keep your old master history at old-master without messing up anything. This will also not touch your dev branch.

g19fanatic
  • 10,567
  • 6
  • 33
  • 63
  • But when I do "git reset --hard dev", Git says "Your branch is behind origin/master by 5 commits, and can be fast-forwarded." Then it says to do "git pull" to update your local branch. But if I do "git pull" this just pulls down the old master code and overwrites the changes that were brought in from the dev branch when I did the hard reset. Again, I'm caught in an endless loop. I can't seem to bring dev's changes into master and then push the new version of master with these changes to the remote master repo. What is going on? Why doesn't this work? I just don't understand. – Jim Mar 07 '17 at 21:43
  • When you do a hard reset, you're changing history. If you want to propagate that change in history, you need to do a `git push --force` as step 3 above in the bottom directions. That will erase masters remote history and instead force it to be where it currently is. My steps will still 'keep' the old master's history because we branched there to keep it alive. `master` will be where dev is (on remote w/ a forced push) and `old-master` will be where master used to be. – g19fanatic Mar 07 '17 at 21:46
  • @Dave: this is what I was referring to when I said that the problem is in *someone else's* repository. Given that the above worked, it's the case that `origin/master` pointed to the commits that you were editing away. The `git pull` command means, in effect, `git fetch && git merge`: the `fetch` step says "get me the other repo's commits" and the `git merge` step means "now **add them to my branch too**". Your `git reset` drops them, but your `git merge` from `git pull` puts them back. – torek Mar 08 '17 at 06:28
  • Both local and origin are my repositories. There wasn't anyone else's repositories involved. I have a better understanding of what was going on now. Thanks for your comments. – Jim Mar 08 '17 at 18:29