简体   繁体   中英

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

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, ie version 1.0
  3. I started working on it and changed some behavior, ie 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?

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)

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