Home > other >  How to force a git branch to be ahead of master branch when actually it is behind?
How to force a git branch to be ahead of master branch when actually it is behind?

Time:12-31

How can I make my develop branch ahead of master branch?

What happened was:

  1. There is no git repo
  2. I found some old code, i.e. version 1.0
  3. I started working on it and changed some behavior, i.e. version 1.1
  4. I decided that I should create a git repo before things get messy, and I did
  5. I pushed version 1.1 to the repo as origin/master as the initial commit
  6. But what I really wanted was to create a tag for version 1.0,
  7. So I also created origin/develop with version 1.1
  8. Then I checked out origin/master, deleted my changes, commited and tagged it version 1.0 again.
  9. I git push the tag and changes to master
  10. Now my master is ahead of develop
  11. But actually I want develop to be ahead of master

I tried to git pull and rebase, but both changed my develop version 1.1 into master version 1.0.

What should I do?

CodePudding user response:

A git log --decorate --graph --oneline --all --branches in your local repository should show you something like:

x commit for 1.0 (HEAD, master, origin/master)
|
x commit for 1.1 (develop, origin/develop)

You can therefore reset your develop branch to master, and cherry-pick your old develop commit, on top of master.

git switch -C develop master
git cherry-pick origin/develop
git push -f -u origin develop

(assuming here there was only only commit on develop)

The new log should show develop ahead of master now:

x commit for 1.1 (HEAD, develop, origin/develop)
|
x commit for 1.0 (master, origin/master)
  •  Tags:  
  • git
  • Related