简体   繁体   中英

Unable to overwrite Git master branch with dev branch

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM