2019年10月30日 星期三

Working while waiting for pending PR

I'm assuming you want to start the new user_story_2 branch on top of the work you've done in user_story_1. Here's the workflow I use in this sort of scenario:
  1. Open Pull Request for user_story_1:
      * (user_story_1)
      *
     /
    * (master)
    *
    *
    
  2. Create new branch user_story_2 based on user_story_1:
    $ git checkout -b user_story_2 user_story_1
      * (user_story_1, user_story_2)
      *
     /
    * (master)
    *
    *
    
  3. Work on the new branch:
      * (user_story_2)
      *      
      * (user_story_1)
      *
     /
    * (master)
    *
    *
    
  4. Pull Request gets merged:
      * (user_story_2)
      *      
    * | (master)
    |\|
    | * (user_story_1)
    | *
    |/
    *
    *
    *
    
  5. Delete old branch:
      * (user_story_2)
      *      
    * | (master)
    |\|
    | *
    | *
    |/
    *
    *
    *
    
  6. Rebase new branch onto master:
      * (user_story_2)
      *      
     /
    * (master)
    |\
    | *
    | *
    |/
    *
    *
    *
    
  • 5
    What happens if the first branch is rejected? – Narayon Dec 21 '16 at 10:46
  • 4
    I'd usually rebase user_story_2 onto mastergit rebase --onto master user_story_1 user_story_2 -- might result in conflicts if the two branches aren't completely independent. – alextercete Dec 22 '16 at 16:36 
  • I will give this a try – anquegi Jul 26 '18 at 15:01
  • 1
    For step 6, using interactive rebasing makes things easier. Try git rebase -i master, and it should show you a list of commits on user_story_2 including the earlier commits from user_story_1. Remove the pick lines for commits from user_story_1, and complete the rebase. – Athyuttam Eleti Feb 25 at 18:00
  • What if we need to update, add some commit in user_story_1? I guess after that we checkout to user_story_2 and rebase with user_story_1 ? – truongnm Jun 19 at 9:32
  • That's right (assuming user_story_1 hasn't been merged to master yet). – alextercete Jun 25 at 8:07
  • Yes of course! :)))) – truongnm Jun 26 at 10:57
  • What if I wanted to do the reverse i.e. user_story_1 would depend on user_story_2? Let me explain: I forked a project and created a PR from my develop branch to theirs. They requested a few changes and I made it, but then wanted more changes outside the scope of the PR that I made, specifically to refactor some of their core classes so that my PR fits better in the project. So now I have to make a new PR2 which creates a few generic classes and then make a new commit to my PR1 to incorporate those new generic classes. How would I go about doing these? – Saifur Rahman Mohsin yesterday

from : https://stackoverflow.com/questions/35790561/working-while-waiting-for-pending-pr

2019年9月6日 星期五

Undo a Git merge that hasn't been pushed yet

With git reflog check which commit is one prior the merge (git reflog will be a better option than git log). Then you can reset it using:
git reset --hard commit_sha
There's also another way:
git reset --hard HEAD~1
It will get you back 1 commit.
Be aware that any modified and uncommitted/unstashed files will be reset to their unmodified state. To keep them either stash changes away or see --merge option below.

As @Velmont suggested below in his answer, in this direct case using:
git reset --hard ORIG_HEAD
might yield better results, as it should preserve your changes. ORIG_HEAD will point to a commit directly before merge has occurred, so you don't have to hunt for it yourself.

A further tip is to use the --merge switch instead of --hard since it doesn't reset files unnecessarily:
git reset --merge ORIG_HEAD
--merge
Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added).

from : https://stackoverflow.com/questions/2389361/undo-a-git-merge-that-hasnt-been-pushed-yet

How to revert a merge commit that's already pushed to remote branch?

The -m option specifies the parent number. This is because a merge commit has more than one parent, and Git does not know automatically which parent was the mainline, and which parent was the branch you want to un-merge.
When you view a merge commit in the output of git log, you will see its parents listed on the line that begins with Merge:
commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: Ben James <ben@example.com>
Date:   Wed Aug 17 22:49:41 2011 +0100

Merge branch 'gh-pages'

Conflicts:
    README
In this situation, git revert 8f937c6 -m 1 will get you the tree as it was in 8989ee0, and git revert -m 2 will reinstate the tree as it was in 7c6b236.
To better understand the parent IDs, you can run:
git log 8989ee0 
and
git log 7c6b236

from : https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch