Some background:
Current situation.
Priority got changed and I had worked on another feature on same develop branch. I should have created a different branch but I continued to add that new feature of top of my current changes and checked in. This means my develop branch has all my 2 weeks changes + this one new feature.
What do I want to do ?
I want to get the exact copy of upstream/develop onto my current (I do not want to create another branch) develop branch and apply only my latest change to it and push it.
Apply my old two weeks work on top of this change on same branch. This I'll be pushing couple of days later.
Can somebody help me with git commands and explain what's happening at back ground ?
Things I tried. git reset --hard This tell me my head is detached.
git rebase -i upstream/branch. I think this is going to apply all my changes but I only want last change I did.
I imagine the commit graph corresponding to your situation to look like this:
A--B--C--X [develop]
/
o--o--o <-- develop from couple of weeks ago
\
O--O [upstream/develop]
A--B--C
is your two weeks work and X
is your latest new feature.
Now you do the following:
$ git branch twoweeks C
You need to figure out the hash of commit C
and use it in the command, or alternatively, use a graphical repository browser to create the branch on that commit.
The branch is only temporary and will be removed again at the end, but we need it now to retain the two weeks work after the next step is done.
Result:
X [develop]
/
A--B--C [twoweeks]
/
o--o--o--O--O [upstream/develop]
$ git rebase --onto upstream/develop twoweeks develop
Result:
A--B--C [twoweeks]
/
o--o--o--O--O [upstream/develop]
\
X [develop]
$ git rebase develop twoweeks
Result:
o--o--o--O--O [upstream/develop]
\
X [develop]
\
A--B--C [twoweeks]
Push the new feature
$ git checkout develop
$ git push
Result:
o--o--o--O--O--X [upstream/develop] [develop]
\
A--B--C [twoweeks]
Fast-forward local develop branch and remove temporary branch
$ git checkout develop
$ git merge --ff-only twoweeks
(using --ff-only
as sanity check, if it fails you did something wrong)
$ git branch -d twoweeks
Result:
o--o--o--O--O--X [upstream/develop]
\
A--B--C [develop]