1

I already made a few changes on my commit and finalize my working copy, after that I did git push too.

But, suddenly I found a little problem on my modified files that has been already pushed to bitbucket repo. So, without undoing, I did manually changed my files into default for the first time. This is the workflow.

C1
README.md:
"This is the content of the first readme on README.md"

  1. git add README.md
  2. git commit -m 'initialize README.md'
  3. git push -u origin master
  4. DONE!

C2
after that, I updated my files into:
The content is new on README.md

  1. like above
  2. DONE!

C3 suddenly I made a few changes like these:
This is the content of the first readme on README.md add a few new lines add a few new lines

  1. git add README.md
  2. DONE!

I'm can't do commit on this state or I'll lose my C2 changes. In this case, I wanna git push my C3 and before I can continue my working copy, I wanna merge C2 and C3, but only the "changes". So, in this case, would be like these:
The content is new on README.md add a few new lines add a few new lines

The question why I did this, because I forgot to work on add a few lines first before modifying "the first line".

I don't think git revert would do magic. Thank you very much for your attention! :)

EDIT: git cherry-pick would also overwritten the local changes T___T

1 Answer 1

1

How about you rebase:

Step1: $git rebase -i HEAD^^^

This will take you to your default editor where your last two commits will be listed, swap them.

During rebase your C3 commit will become your 2nd commit C2' and C2 will become C3'.

So effectively these will your commits after resolving conflict during Rebase 2/2:

C1 README.md: This is the content of the first readme on README.md

C2' README.md: This is the content of the first readme on README.md add a few new lines add a few new lines

C3' README.md The content is new on README.md add a few new lines add a few new lines

Now you will have to force push to your upstream.

$git push -f -u origin master

NOTE: It will overwrite your changes, you will have to tell others who have already fetched from you to do a force pull or fetch and merge/rebase.

Hope this answers your question!

Sign up to request clarification or add additional context in comments.

11 Comments

Or if you don't want to rebase..(too many ppl have fetched your changes) then you can drop an branch at C2 and while on C3 do a git merge C2-branch
Thank you for your advice :). Btw, this error occurs when I tried git rebase -i HEAD^^^ "fatal: Needed a single revision invalid upstream HEAD^^^"
Sorry! My Bad! It must be git rebase -i HEAD^^ This is because rebase of first commit in a repository cannot be done. Its needs atleast a single commit for revision. This is the reason I create a 0th commit in every repository of mine, this 0th commit is an empty commit.
For EMPTY_COMMIT method you can check this out: github.com/mudassirazvi/EMPTY_COMMIT.git Its explained why this is necessary!
I see :D I'll remember this, to create 0th commit before any commit for my next development :) Thank you very much sir, this is awesome!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.