Home > Net >  Git: Branch back in time (and switch head)
Git: Branch back in time (and switch head)

Time:11-14

Last week, I started a new project that uses an Opensource library. I discovered a bug in that library, forked it on Github, fixed the bug, and submitted a PR. I received some comments and did more commits to update my PR:

|-my commit 3  <- master (PR#1)
|-my commit 2
|-my commit 1
|-commit x     <- remote:master
...

While waiting for my PR to be merged, I discovered another bug, created a bug report, branched at commit x, committed a fix and created another PR:

|-my commit 3     <- master (PR#1)
|-my commit 2
|-my commit 1
| |-my commit 4   <- fix_bug_2 (PR#2)
|/
|-commit x        <- remote:master
...

So I should probably better have branched before doing my first commit. Is there any way I can get fix it, so that I can easily track upstream while creating more PRs for new bugs I find without messing up my existing PRs? It should look like this:

|-my commit 3     <- fix_bug_1 (PR#1)
|-my commit 2
|-my commit 1
| |-my commit 4   <- fix_bug_2 (PR#2)
|/
|-commit x        <- master, remote:master
...

Something like:

  • create new branch "fix_bug_1" at commit x
  • switch master with fix_bug_1
  • make sure PR#1 still contains the same commits as before

Or should I simply wait until PR#1 gets merged and from now on always remember not to work on head?

CodePudding user response:

To create a branch named fix_bug_1 at "the same commit as master" :

git branch fix_bug_1 master

To forcibly move master to where origin/master is :

git branch -f master origin/master

As far as I know, there is no way in github to change the "source branch" of a PR, you would have to create a new PR to track your new branch.

  • Related